【问题标题】:LP Relaxation with Cplex (Python)使用 Cplex 进行 LP 松弛 (Python)
【发布时间】:2021-07-06 10:48:30
【问题描述】:

我正在用 python 上的 docplex 开发一个 lp 模型,我需要获取宽松的解决方案变量,但不知道如何要求 python 给我模型的宽松解决方案变量。我可以将它们视为输出,但是当我打印它时,它只是转换了变量的非松弛值。所以,问题是我该如何解决,

y[i].solution_value    # how can i addressed y[i] as a relaxed solution value of y[i]

最好的问候,

【问题讨论】:

    标签: python cplex docplex


    【解决方案1】:

    您可以依赖 LinearRelaxer,如 https://github.com/AlexFleischerParis/zoodocplex/blob/master/zoorelaxintegrity.py 所示

    from docplex.mp.model import Model
    from docplex.mp.relax_linear import LinearRelaxer
    
    def make_bus_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)
        return mdl
    
    if __name__ == '__main__':
        bm1 = make_bus_model()
        bm1.print_information()
        s1 = bm1.solve(log_output=True)
        s1.display()
    
        bmr = LinearRelaxer.make_relaxed_model(bm1)
        bmr.print_information()
        rs = bmr.solve(log_output=True)
        rs.display()
    
        duals = bmr.get_constraint_by_name("kids").dual_value
    
        print("dual of the 300 kids constraint = ",duals) 
    

    给了

    solution for: buses
    objective: 3800
    nbBus40 = 6
    nbBus30 = 2
    Model: lp_buses
     - number of variables: 2
       - binary=0, integer=0, continuous=2
     - number of constraints: 1
       - linear=1
     - parameters: defaults
     - objective: minimize
     - problem type is: LP
    Version identifier: 12.10.0.0 | 2019-11-26 | 843d4de2ae
    CPXPARAM_Read_DataCheck                          1
    CPXPARAM_RandomSeed                              201903125
    Tried aggregator 1 time.
    LP Presolve eliminated 1 rows and 2 columns.
    All rows and columns eliminated.
    Presolve time = 0.00 sec. (0.00 ticks)
    solution for: lp_buses
    objective: 3750.000
    nbBus40 = 7.500
    dual of the 300 kids constraint =  12.5
    

    【讨论】:

    • 感谢您的快速回答,实际上我已经查看了这个示例以了解我的代码有什么问题,但它也没有帮助。该模型总是给出不松弛的 lp 模型的变量结果。我需要存储放松的,作为输出实际上没有问题>>> rs.display()这段代码运行良好
    猜你喜欢
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多