【问题标题】:Warm Start in CPLEX PythonCPLEX Python 中的热启动
【发布时间】:2021-03-29 19:10:26
【问题描述】:

我一直在尝试使用其 Python API 在 CPLEX 求解器中实现热启动,以求解线性规划模型。在IBM官网上找到了函数的定义和sn-p的代码。

函数定义如下:

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

这是来自网站的代码sn-p:

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)

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

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

如果能提供任何帮助,我将不胜感激。提前谢谢你。

【问题讨论】:

    标签: python linear-programming cplex


    【解决方案1】:

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

    Easy optimization with python 中的 warm start through API 示例

    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.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-08
      • 2021-06-13
      • 1970-01-01
      • 2021-12-26
      • 1970-01-01
      • 2013-06-05
      • 2017-09-18
      • 2016-07-07
      相关资源
      最近更新 更多