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/