【问题标题】:Solving Minimization problem with Ortools in python在 python 中使用 Ortools 解决最小化问题
【发布时间】:2020-12-11 09:52:36
【问题描述】:

我正在尝试解决最小化问题。

但结果总是给我零。

是我设置的约束有误还是代码有误?

目标函数:

M = 80x+60y+55z

约束:

1. 4x+2z

2. 4y+z

3. x+y+z

4. 0.215x+0.04y+0.367z=0.3(x+y+z)

我对使用 Ortools 的了解不多,但我写了这些:

使用 GLOP 后端创建线性求解器。

求解器 = pywraplp.Solver('main',pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)

创建变量 xyz。

x = solver.IntVar(0.0, solver.infinity(), 'x')

y = solver.IntVar(0.0, solver.infinity(), 'y')

z = solver.IntVar(0.0, solver.infinity(), 'z')

设置约束:(仅显示其余类似)

案例:4x+2z

constraint0 = solver.Constraint(-solver.infinity(), 20)

constraint0.SetCoefficient(x, 4)

constraint0.SetCoefficient(z, 2)

设置约束:(设置最终约束)

案例:0.215x+0.04y+0.367z=0.3(x+y+z)

constraint3 = solver.Constraint(0,-solver.infinity())

constraint3.SetCoefficient(x,-0.0850)

constraint3.SetCoefficient(y,-0.26)

constraint3.SetCoefficient(z,0.067)

创建目标函数

M = 80x+60y+55z

objective = solver.Objective()

objective.SetCoefficient(x, 80)

objective.SetCoefficient(y, 60)

objective.SetCoefficient(z, 55)

objective.SetMinimization()

最后求解方程并输出结果

solver.Solve()

print('Objective value =', objective.Value())

print('x =', x.solution_value())

print('y =', y.solution_value())

print('z =', z.solution_value())

【问题讨论】:

    标签: python minimize or-tools


    【解决方案1】:

    3 分:

    • 您犯了一个常见错误,您混淆了 MIP 求解器和 LP 求解器。请使用 CBC、SCIP 或 CP_SAT 作为后端。
    • 0 似乎是所有变量的最佳值。
    • 您可以使用 natural API:model.Add(4* x + 2* z <= 20)

    【讨论】:

    • -感谢您的评论-我更改为“pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING”来解决问题-似乎我为问题设置的约束是错误的,因此输出零
    猜你喜欢
    • 2020-09-07
    • 2019-06-29
    • 2020-07-16
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 2014-10-05
    • 2020-07-08
    相关资源
    最近更新 更多