简体   繁体   English

使用 Cplex 进行 LP 松弛 (Python)

[英]LP Relaxation with Cplex (Python)

I am developing a lp model with docplex on python and I need to get solution variables of the relaxed one but do not know how to ask python to give me the relaxed solution variables of the model.我正在 python 上开发一个带有 docplex 的 lp 模型,我需要获取宽松的解变量,但不知道如何要求 python 给我模型的宽松解变量。 I can see them as an output but when I print it, it just turns the unrelaxed values of the variables.我可以将它们视为输出,但是当我打印它时,它只会改变变量的非松弛值。 So, the question is how can i addressed,所以,问题是我该如何解决,

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

Best Regards,最好的祝福,

You could rely on LinearRelaxer as shown in https://github.com/AlexFleischerParis/zoodocplex/blob/master/zoorelaxintegrity.py您可以依赖 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) 

which gives这使

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

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

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