[英]How can I take the solution values from first calculation and use it in another calculation in python using docplex
I calculated a machine scheduling problem using docplex in python. I obtained one of the decision variable as:我在 python 中使用 docplex 计算了一个机器调度问题。我获得了一个决策变量:
yib yib | solution解决方案 |
---|---|
y_0_1 y_0_1 | 1 1个 |
y_1_2 y_1_2 | 1 1个 |
y_2_1 y_2_1 | 1 1个 |
y_3_1 y_3_1 | 1 1个 |
y_4_1 y_4_1 | 1 1个 |
Since I wanted to use this values in another calculation I used this code:因为我想在另一个计算中使用这个值,所以我使用了这个代码:
ySol = [y[i,b].solution_value for i in range(0,J) for b in range(1,B)] ySol = [y[i,b].solution_value for i in range(0,J) for b in range(1,B)]
Then, I tried to use ySol in my constraints.然后,我尝试在我的约束中使用 ySol。
***my first question is, this code is true code to take the decision variable? ***我的第一个问题是,这段代码是采用决策变量的真实代码吗?
after I added ySol in the second calculation I took this error:在第二次计算中添加 ySol 后,我出现了这个错误:
"TypeError: list indices must be integers or slices, not tuple" “TypeError:列表索引必须是整数或切片,而不是元组”
I tried some alternative ways but I've not solved the tuple problem yet.我尝试了一些替代方法,但我还没有解决元组问题。
***my second question is, how can I solve this error? ***我的第二个问题是,我该如何解决这个错误?
thank you in advance!先感谢您!
you can either use warmstart你可以使用warmstart
from docplex.mp.model import Model
mdl = Model(name='buses')
nbbus40 = mdl.integer_var(name='nbBus40')
nbbus30 = mdl.integer_var(name='nbBus30')
mdl.add_constraint(nbbus40*40 + nbbus30*30 >= 300, 'kids')
mdl.minimize(nbbus40*500 + nbbus30*400)
warmstart=mdl.new_solution()
warmstart.add_var_value(nbbus40,8)
warmstart.add_var_value(nbbus30,0)
mdl.add_mip_start(warmstart)
sol=mdl.solve(log_output=True)
for v in mdl.iter_integer_vars():
print(v," = ",v.solution_value)
or fixed start或固定开始
from docplex.mp.model import Model
mdl = Model(name='buses')
nbbus40 = mdl.integer_var(name='nbBus40')
nbbus30 = mdl.integer_var(name='nbBus30')
mdl.add_constraint(nbbus40*40 + nbbus30*30 >= 300, 'kids')
mdl.minimize(nbbus40*500 + nbbus30*400)
#Fixed start nbBus40 should be 5
nbbus40.lb=5
nbbus40.ub=5
mdl.solve()
for v in mdl.iter_integer_vars():
print(v," = ",v.solution_value)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.