cplexpython中的热启动

2024-05-14 08:27:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试在CPLEX solver中使用其Python API实现热启动,以解决线性编程模型。从IBM的官方网站上,我找到了函数的定义和代码片段

函数定义如下所示:

设置开始(自身、列状态、行状态、列原始、行原始、列双重、行双重)

以下是网站上的代码片段:

import cplex
c = cplex.Cplex()
indices = c.variables.add(names = ["v" + str(i) for i in range(5)])
indices = c.linear_constraints.add(names = ["r" + str(i) for i in range(3)])
s = c.start.status
c.start.set_start([s.basic] * 3 + [s.at_lower_bound] * 2, [s.basic] + [s.at_upper_bound] * 2,
                     [0.0] * 5, [1.0] * 3, [2.0] * 5, [3.0] * 3)

但是,我无法理解函数集_start的输入,也找不到任何示例。是否有任何示例代码可以帮助我实现它

这是另一件我想知道的关于热启动的事情。我知道我们提供了以前解决方案中的变量及其值,但约束是否保持不变,或者在重新解决问题时是否必须重新创建约束

我将感谢任何帮助。先谢谢你


Tags: 函数代码inaddfor定义basicnames
1条回答
网友
1楼 · 发布于 2024-05-14 08:27:14

您应该尝试使用docplex而不是matrix api:

示例warm start through APIEasy optimization with python

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)

nbBus40  =  6.0
nbBus30  =  2.0

在日志中我们看到

1 of 1 MIP starts provided solutions.
MIP start 'm1' defined initial solution with objective 4000.0000.

相关问题 更多 >

    热门问题