how can I get constraints
回答済みHi,
I got some constraints as:
I want to get them by using getConstrByName(), however, this method chooses one arbitrarily. So is there any other way helps to obtain all constraints with the same name?
-
Hi Cass,
So is there any other way helps to obtain all constraints with the same name?
No, there is no way to get all constraints with the same name at once. You could loop over all constraints and save the ones with the name of interest, e.g.,
constrs_of_interest = []
for c in m.getConstrs():
if c.ConstrName == "c1":
constrs_of_interest.append(c)In general, it is not recommended to have multiple constraints with the same name. You could adjust your current code to
m.addConstr(Pf[(brh,t)], gb.GRB.LESS_EQUAL, branchPar[brh]['Limit'],name="c1{0}{1}".format(brh,t))You could then, later loop over \(\texttt{(brh,t)}\) and get all constraints by name.
Or even better, you could save the constraints to a dictionary while constructing them.
mydict = {}
[...]
mydict[brh,t] = m.addConstr(Pf[(brh,t)], gb.GRB.LESS_EQUAL, branchPar[brh]['Limit'],name="c1{0}{1}".format(brh,t))You can then access the constraints via \(\texttt{mydict[brh,t]}\) without having to retrieve them from the model again. Of course this only works, if your dictionary is in the same scope as your model.
Best regards,
Jaromił0 -
Hi Jaromił
Many thanks for this, you are right! It should be formatted in list or dictionary, or it won't work.
Best
Qikun
0
サインインしてコメントを残してください。
コメント
2件のコメント