Gurobi Python code to print non-binding constraints?
AnsweredHello good people,
How can I correctly write/save the model.lp file to store non-binding constraints as well?
When I write the model.lp file, most constraints are found formulated as expected. However, LHS of non-binding constraint (constraints that do not contain any decision variables) is empty in the .lp file.
part of model.lp file
C_available_resources[m,s16]: deploy[s16,o186,m] + deploy[s16,o217,m] <= 300
C_available_resources[c,s16]: deploy[s16,o186,c] + deploy[s16,o217,c]<= 10
C_available_resources[i,s16]: deploy[s16,o186,i] + deploy[s16,o217,i]<= 30
C_max_distance[s2,o186]: <= 0
C_max_distance[s2,o217]: <= 0
C_max_distance[s18,o186]: <= -1
# a binding constraint (deploy[] is a decision variable
C_available_resources = model2.addConstrs((gp.quicksum(deploy[s, o, r] for o in OilSpills)
<= Availability[s, r] for s in Stations for r in ResourcesD),
name='C_available_resources')
# a non-binding constraint
C_max_distance = model2.addConstrs((Distance[s, o] <= DistanceMax for s in Stations for o in OilSpills),
name='C_max_distance')
-
Hi Tanmoy,
If there are no Gurobi variables in your expression, then it is not really a constraint (nothing is being constrained).
Are you perhaps just wanting to calculate the maximum of the numbers stored in the Distance dictionary? If so you can define DistanceMax like soDistanceMax = max(Distance.values())
and then use it in your model.
- Riley0 -
Well, actually Distance[s, o] is more of a Pairing of index between s & o. Just like Pairing(i,j) example in the following example https://gurobi.github.io/modeling-examples/customer_assignment/customer_assignment.html
But, I want to use it as constraint so that this constraint would be violates sometimes based on certain conditions. So, I used all values as pair index.
0 -
Hi Tanmoy,
If you want a list of pairings (s,o) where Distance[s,o] is less than DistanceMax then you can create this like so
[index for index,distance in Distance.items() if distance <= DistanceMax]
- Riley
0
Please sign in to leave a comment.
Comments
3 comments