c++ - 如何将数字乘数分配给 or-tools (C++) 中的变量

标签 c++ or-tools

std::vector<const MPVariable *> P(mP);
for (int a = 1; a <= mP; a++)
    P[a - 1] = solver->MakeBoolVar("P"+std::to_string(a));

for (int a = 1; a <= mP; a++)
{
    LinearExpr eq;
    for (int b = 1; b <= T; b++)
    {
      ; //eq += ... ;
    }
    eq -=  T * P[a - 1]; // error; however, eq -=  P[a - 1] is okay.
    solver->MakeRowConstraint(eq <= 0.0);
}

eq -= T * P[a - 1] 给出错误。然而,eq -= P[a - 1] 是可以的。 T 是一个 int

错误消息:

 invalid operands of types ‘int’ and ‘__gnu_cxx::__alloc_traits<std::allocator<const operations_research::MPVariable*>, const operations_research::MPVariable*>::value_type’ {aka ‘const operations_research::MPVariable*’} to binary ‘operator*’

如何将数字乘数 (T) 分配给变量 (P[a - 1])?

另外,如何打印生成的LP模型,以便我可以调试它?

最佳答案

编辑:我最初错误地显示了 CP-SAT 求解器的代码,而不是 MIP 求解器,已在此处更正。

https://github.com/google/or-tools/blob/stable/ortools/linear_solver/linear_expr.h#L174 的文档中我可以看到 LinearExpr 的简单算术运算符的重载如下:

LinearExpr operator+(LinearExpr lhs, const LinearExpr& rhs);
LinearExpr operator-(LinearExpr lhs, const LinearExpr& rhs);
LinearExpr operator*(LinearExpr lhs, double rhs);
LinearExpr operator/(LinearExpr lhs, double rhs);
LinearExpr operator*(double lhs, LinearExpr rhs);

int 相乘没有重载,您需要使用 double 作为因子,而不是像这样的 int:

double doubleT = double(T);
eq -= doubleT * P[a - 1];

关于c++ - 如何将数字乘数分配给 or-tools (C++) 中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70845828/

相关文章:

python - ORtools 安排学生上课时间冲突

constraints - 如何使用 CP-SAT 求解器或其他工具来查找具有行、列和管(代表学校类(class)、学期和天数)约束的 3D 数组?

c++ - 由于未调用 _ReadBarrier() 而导致错误的示例程序是什么?

c++ - 单击按钮后,Wt C++ 使窗口不可单击

c++ - 如何在 CMake 中明确指定树外源?

python - 使用 Google OR-tools 设置二元约束

java - 在 Windows 10 (Intellij IDEA) 上使用 gradle 项目安装 Google or-tools

c++ - std::atomic 变量的比较操作线程安全吗?

c++ - 使用 SIMD 查找表

c++ - 如何将 OR-Tools 链接到我的 CMake 项目?