Ortools - 将 MPVariable 设置为 MPCons应变绑定

Ortools - Set MPVariable as MPConstrains bound

本文关键字:MPCons 绑定 设置 MPVariable Ortools      更新时间:2023-10-16

我想做这样的事情:

namespace opr = operations_research;
double min = 10;
opr::MPVariable* const y1 = solver.MakeBoolVar("y1");
opr::MPConstraint *const c1 = solver.MakeRowConstraint(-min * y1, 10);

但是编译器抱怨因为数据类型不兼容,取消引用指针也不起作用,编译它的唯一方法是使用y1->solution_value()但我认为这不是正确的解决方案,因为它复制了当前的 y1 值,我希望绑定随 y1 一起变化。 对于上下文,y1是一个标志变量,用于满足其他变量的特定条件。 我尝试编写的约束类型如下所示:

-M * y_1 <= x <= 10

正如@LaurentPerron评论中提到的,解决方案只是重写约束,使边界是数字而不是 MPVariable。 对于我发布的示例,如下所示:

-M * y_1 <= x <= 10

写成:

x <= 10
x + M * y1 >= 0