how can I retrieve the values for a single variable? The variable is something like d(b,t).
AnsweredHi,
The command model.getVars() shows the values of all the variables. Can I only get the values of a certain variable? Because I would like to make some graphs to show its values varying with time.
The variables are set up as follows: (but I also have many other variables)
l = m.addVars(G,T , name="load")
d = m.addVars(N,T, name="d")
The related constraints are set up like as follows:
for g in G:
for t in T1:
m.addConstr((l[g, t]  l[g, t1] <= gen_ramp_up[g] ), "{} ramp up".format(g,t))
Is there any command that could help to retrieve only the values of "l"?
Thanks so much!

Hi Yiwen,
The variable l is a tupledict that maps a set of indices to Var objects. The values() method of a tupledict returns a list of the corresponding Var objects. Finally, the X attribute of a Var object stores the variable's value at the current solution.
So, after optimizing, you can iterate through each variable in the tupledict and retrieve the variable's solution value. For example:
for v in l.values():
print("{}: {}".format(v.varName, v.X))You could also use a dictionary comprehension to construct a dictionary that maps the variable indices to the corresponding solution values. This can be done by iterating over the tupledict's keyvalue pairs:
vals = { k : v.X for k,v in l.items() }
I hope this helps!
1 
Thanks a lot Eli! :)
0 
how can I get the result in one list?
as my case is:
for v in A.values():
z= (v.X)
print ((z))my result is:
0.0
361.0
709.0
0.0while I need to have them in one list to be able to do further operations on them
0
Please sign in to leave a comment.
Comments
3 comments