简体   繁体   English

CPLEX Python 中的热启动

[英]Warm Start in CPLEX Python

I have been trying to implement warm start in CPLEX solver using its Python API to solve a linear programming model.我一直在尝试使用其 Python API 在 CPLEX 求解器中实现热启动来求解线性规划 model。 From IBM's official website, I have found the definition of the function and a code snippet.从IBM官网,我找到了function的定义和一段代码。

The function definition is given as this: function 定义如下:

set_start(self, col_status, row_status, col_primal, row_primal, col_dual, row_dual) set_start(自我,col_status,row_status,col_primal,row_primal,col_dual,row_dual)

Here is the code snippet from the website:以下是网站上的代码片段:

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)

However, I couldn't understand the inputs of the function set_start and I couldn't find any examples either.但是,我无法理解 function set_start 的输入,也找不到任何示例。 Are there any example codes that can be helpful for me to implement it?是否有任何示例代码可以帮助我实现它?

Here is another thing I wonder about warm start.这是我想知道的关于热启动的另一件事。 I know that we provide variables and their values from previous solutions, but do constraints remain the same or do they have to be recreated when re-solving the problem?我知道我们提供了以前解决方案中的变量及其值,但是约束是否保持不变,或者在重新解决问题时是否必须重新创建它们?

I'd appreciate any kind of help.我会很感激任何帮助。 Thank you in advance.先感谢您。

you should try to use docplex instead of the matrix api:您应该尝试使用 docplex 而不是矩阵 api:

Example warm start through API in Easy optimization with python 通过 API 进行热启动的示例 使用 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)

which gives这使

nbBus40  =  6.0
nbBus30  =  2.0

and in the log we see在日志中我们看到

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM