简体   繁体   English

如何使用 docplex 从第一次计算中获取解决方案值并将其用于 python 中的另一次计算

[英]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.

相关问题 如何将变量结果用于另一个计算python - How to use the variable result for another calculation python 如何从 .txt 文件中提取浮点值以用于 python 中的计算? - How do I extract floating point values from .txt file to use for calculation in python? python - 当我们提供输入时,如何从用户那里获取输入并并行运行另一个连续计算 - how to take input from a user and run another continuous calculation in parallel while we feed the input, in python 如何从Python 3中的计算中修复溢出错误? - How Can I Fix An Overflow Error From the Calculation In Python 3? 如何在 python 中保留零进行计算? - How can I retain zero in python for calculation? 无法使创建的 python function 返回一个值,然后我可以在另一个计算中使用该值 - Can't make created python function return a value which I can then use in another calculation 使用数据帧中的值进行计算 - Using values from dataframe for calculation 您可以使用另一个列表/集合中的值在数据框中为计算指定列吗? - Can you specify columns for a calculation in a dataframe using values from another list/set? 如何根据列表中字典中的值进行的新计算创建统计计算,python 3 - How to create statistical calculation from a new calculation made from values in dictionaries within a list, python 3 如何从 class 进行计算并将其写入文件 - How to take a calculation from a class and write that into file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM