使用多个列表创建变量
Creating variables using multiple lists
所以我有三个列表:客户、时间和金额。基于这三个列表,我想创建一个需求变量:客户需求 c in time t.
假设我有以下列表:
customer = ["c1","c1","c1","c2","c2","c2","c3","c3","c3"]
time = [1,2,3, 1,2,3, 1,2,3]
value = [4, 8, 9, 2, 3, 4, 5, 7, 9]
这应该读作:
因此客户“c1”在时间 1 的需求等于 4。客户“C3”在时间 3 的需求等于 9
如何使用 addVars 添加需求作为变量?目前,我有以下内容:
time = [1,2,3, 1,2,3, 1,2,3]
value = [4, 8, 9, 2, 3, 4, 5, 7, 9]
indices = list(zip(customer, time))
demand = model.addVars(indices, obj = value, name = "demand")
但如果我使用以下内容:
for x in demand.values():
print(x)
输出如下:
<gurobi.Var demand[c1,1]>
<gurobi.Var demand[c1,2]>
<gurobi.Var demand[c1,3]>
<gurobi.Var demand[c2,1]>
<gurobi.Var demand[c2,2]>
<gurobi.Var demand[c2,3]>
<gurobi.Var demand[c3,1]>
<gurobi.Var demand[c3,2]>
<gurobi.Var demand[c3,3]>
我希望我能得到列表值的值。我应该如何将需求添加为变量?
您正在将需求添加为 objective 系数。您需要调用 model.getObjective()
来查看这些值(请参阅变量的 getObjective documentation). Alternative, you could query the obj attribute:
for x in demand.values():
print(x.Obj)
所以我有三个列表:客户、时间和金额。基于这三个列表,我想创建一个需求变量:客户需求 c in time t.
假设我有以下列表:
customer = ["c1","c1","c1","c2","c2","c2","c3","c3","c3"]
time = [1,2,3, 1,2,3, 1,2,3]
value = [4, 8, 9, 2, 3, 4, 5, 7, 9]
这应该读作:
因此客户“c1”在时间 1 的需求等于 4。客户“C3”在时间 3 的需求等于 9
如何使用 addVars 添加需求作为变量?目前,我有以下内容:
time = [1,2,3, 1,2,3, 1,2,3]
value = [4, 8, 9, 2, 3, 4, 5, 7, 9]
indices = list(zip(customer, time))
demand = model.addVars(indices, obj = value, name = "demand")
但如果我使用以下内容:
for x in demand.values():
print(x)
输出如下:
<gurobi.Var demand[c1,1]>
<gurobi.Var demand[c1,2]>
<gurobi.Var demand[c1,3]>
<gurobi.Var demand[c2,1]>
<gurobi.Var demand[c2,2]>
<gurobi.Var demand[c2,3]>
<gurobi.Var demand[c3,1]>
<gurobi.Var demand[c3,2]>
<gurobi.Var demand[c3,3]>
我希望我能得到列表值的值。我应该如何将需求添加为变量?
您正在将需求添加为 objective 系数。您需要调用 model.getObjective()
来查看这些值(请参阅变量的 getObjective documentation). Alternative, you could query the obj attribute:
for x in demand.values():
print(x.Obj)