How to sum up the values of a gurobi variable
AnsweredHow can I access all values of the variable and sum them up?
Example:
idle_time[5-21a,5-89b] 10 idle_time[5-89b,5-89a] 27 idle_time[5-89a,5-89d] 18 idle_time[5-89d,5-89c] 22 idle_time[5-89c,5-90a] 17
I tried to acess the values with:
idle = [v for v, v in idle_time.items() if v.X > 0.5]
But this gives me this, rather than just the values:
[<gurobi.Var idle_time[5-21a,5-89b] (value 10.0)>, <gurobi.Var idle_time[5-89b,5-89a] (value 27.0)>, <gurobi.Var idle_time[5-89a,5-89d] (value 18.0)>, <gurobi.Var idle_time[5-89d,5-89c] (value 22.0)>, <gurobi.Var idle_time[5-89c,5-90a] (value 17.0)>]
Best regards,
Alice
0
-
Official comment
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?. -
This should give you a list of the values:
idle = [v.X for v in idle_time.values()]You can add \( \texttt{if v.X > 0.5} \) to the list comprehension, though then the list wouldn't include idle times between \( 0 \) and \( 0.5 \) (inclusive).
It looks like you added the \( \texttt{idle_time} \) variables using Model.addVars(). If so, you could alternatively combine tupledict.sum() and LinExpr.getValue() to calculate the sum of idle times:
idle = idle_time.sum().getValue()This approach will sum all of the idle times, including those between \( 0 \) and \( 0.5 \).
0
Post is closed for comments.
Comments
2 comments