Append a variable to a list
回答済みhello,
I am trying to append a gurobi varible to a list with this code:
Prod_values = {}
obj_vars = []
Plants = range(7)
for l in range(7):
for p in Demand:
for d in Horizon:
Prod_values = model.addVars(Plants,Demand,Horizon,lb = 0, vtype =GRB.INTEGER, name ='Prod_engrais')
obj_vars.append(Prod_values[(l,p,d)])
but when i print obj_vars, it doesn't return the same values as Prod_values. Could you provide me with some help.
Thank you for reading this post and helping me out!
-
正式なコメント
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 try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi Sanaa,
Please note that your example is not reproducible, because values for Demand and Horizon are missing.
Moreover, you are adding Plants x Demand x Horizon variable in each loop iteration by using the addVars method. If you want to assign a value to a certain dictionary field, you have to access it via \(\texttt{Prod_values[l,p,d] = ...}\)
What you are probably looking for is something like
import gurobipy as gp
from gurobipy import GRB
model = gp.Model()
Prod_values = {}
obj_vars = []
Plants = range(4)
Demand = range(3)
Horizon = range(2)
for l in Plants:
for p in Demand:
for d in Horizon:
Prod_values[l,p,d] = model.addVar(lb = 0, vtype =GRB.INTEGER, name ='Prod_engrais[%d,%d,%d]'%(l,p,d))
obj_vars.append(Prod_values[(l,p,d)])
model.update()
print(Prod_values)
print(obj_vars)Best regards,
Jaromił0
投稿コメントは受け付けていません。
コメント
2件のコメント