【问题标题】:How to set gap tolerance in cplex-python?如何在 cplex-python 中设置间隙容差?
【发布时间】:2019-10-07 10:35:33
【问题描述】:

我想设置一个差距值 (GAP),以便在当前差距低于 GAP 时停止优化过程。我已经阅读了cplex-python 文档,发现:

Model.parameters.mip.tolerances.absmipgap(GAP)

但我收到下一个警告:

Model.parameters.mip.tolerances.mipgap(float(0.1))
TypeError: 'NumParameter' object is not callable

有什么想法吗?请帮我。提前致谢。

【问题讨论】:

  • 澄清一下,您使用的是 docplex 还是 CPLEX Python API?

标签: python optimization mathematical-optimization modeling cplex


【解决方案1】:

让我根据您的问题调整我的巴士示例:

from docplex.mp.model import Model

mdl = Model(name='buses')

# gap tolerance
mdl.parameters.mip.tolerances.mipgap=0.001;


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)

mdl.solve()

for v in mdl.iter_integer_vars():
    print(v," = ",v.solution_value)

print("gap tolerance = ",mdl.parameters.mip.tolerances.mipgap.get())

给出:

nbBus40  =  6.0
nbBus30  =  2.0
gap tolerance =  0.001

【讨论】:

    【解决方案2】:

    你的错误是把参数当作函数来调用。更改参数的正确方法是分配给它:

    Model.parameters.mip.tolerances.absmipgap = GAP
    

    还要确保您不使用 Model 类,而是使用此类的实例:

    mdl = Model()
    mdl.parameters.mip.tolerances.absmipgap = GAP
    

    还要注意有两个间隙参数:绝对和相对。相对间隙是最常用的。您可以找到herehere(相对公差的参数称为mipgap)。

    【讨论】:

      【解决方案3】:

      根据您收到的错误,我认为您可能使用的是 CPLEX Python API 而不是 docplex(如其他答案中所示)。要解决您的问题,请考虑以下示例:

      import cplex                                                                    
      Model = cplex.Cplex()                                                           
      # This will raise a TypeError                                                   
      #Model.parameters.mip.tolerances.mipgap(float(0.1))                             
      # This is the correct way to set the parameter                                  
      Model.parameters.mip.tolerances.mipgap.set(float(0.1))                          
      # Write the parameter file to check that it looks as you expect                 
      Model.parameters.write_file("test.prm")
      

      您需要使用set() 方法。您可以通过write_file方法将参数文件写入磁盘并查看它,以确保参数按预期更改。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-11-20
        • 2011-11-30
        • 2012-01-30
        • 1970-01-01
        • 1970-01-01
        • 2019-10-20
        相关资源
        最近更新 更多