gpt4 book ai didi

linear-programming - 无法使用 CPLEX 从 LP 中导出对偶值

转载 作者:行者123 更新时间:2023-12-04 07:58:02 24 4
gpt4 key购买 nike

我正在尝试使用 CPLEX 编写 Bender 分解算法的代码。为了确保我的编码正确,我遵循了 A.J. 的“数学规划中的分解技术”中的数值示例。 Conejo 等人,第 247 页。

但是,我的问题可以在没有访问提到的 Material 或上下文知识的情况下陈述。我需要解决以下 LP 并导出“fixing_x”约束的对偶值。

import cplex
x_master_value = 100.

c_toy_slave = cplex.Cplex()
types = c_toy_slave.variables.type

y = c_toy_slave.variables.add(names=["y"+str(i) for i in range(3)], lb=[0]*3, types=[types.continuous]*3)
x = c_toy_slave.variables.add(names=["x"], lb=[0], types=[types.continuous])
w = c_toy_slave.variables.add(names=["w"], lb=[0], types=[types.continuous])

cst1 = c_toy_slave.linear_constraints.add([[["y0", "y1", "x", "w"], [-1, -3, 2, -1]]],
names=["cst1"], rhs=[2], senses=['L'])

cst2 = c_toy_slave.linear_constraints.add([[["y0", "y1", "x", "w"], [1, 3, -1, -1]]],
names=["cst2"], rhs=[3], senses=['L'])

cst3 = c_toy_slave.linear_constraints.add([[["y2", "x"], [1, -3]]],
names=["cst3"], rhs=[7/2], senses=['L'])

cst4 = c_toy_slave.linear_constraints.add([[["x"], [1]]],
names=["fixing_x"], rhs=[x_master_value], senses=['E'])

c_toy_slave.objective.set_linear([("y0", -1.5), ("y1", -2), ("y2", -2), ("w", 40)])
c_toy_slave.objective.set_sense(c_toy_slave.objective.sense.minimize)

c_toy_slave.solve()

print("lambda = ", c_toy_slave.solution.get_dual_values("fixing_x"))

但 CPLEX 表示它不能使用 get_dual_values 方法并提示此消息:CPLEX 错误 1017:不可用于混合整数问题。我不知道如何解决这个问题,因为我提供的输入不是 MIP,而是真正的 LP。

最佳答案

事实证明,删除 types=[type.continuous] 可选参数解决了我的问题(即,优化问题随后被正确识别为 LP)。如果我们查看 Cplex.variables.add 的文档,它说:

If types is specified, the problem type will be a MIP, even if all variables are specified to be continuous.

所以,这是预期的行为。

关于linear-programming - 无法使用 CPLEX 从 LP 中导出对偶值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56650285/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com