Gurobi - 构建约束时出现问题 (Java)

Gurobi - problem when building constraint (Java)

我目前在实施约束时遇到问题。 我有两组(s1 和 s2)特定的 GRBVars 并尝试实现以下约束:

总和(s1) - 总和(s2) + constantValue <= someValue

我的实现(为便于阅读而修改):

GRBVar[][] vars;
....

GRBLinExpr expr1 = new GRBLinExpr();
GRBLinExpr expr2 = new GRBLinExpr();

for (Edge edge : s1) {
expr1.addTerm(1.0, vars[edge.getVertex1][edge.getVertex2]);
}

for (Edge edge : s2) {
expr2.addTerm(-1.0, vars[edge.getVertex1][edge.getVertex1]);
}

expr1 += expr2 + 50;

最后一行 expr1 += expr2 + 50 被标记为错误,没有任何有用的信息。根据文档 (https://www.gurobi.com/documentation/9.0/refman/cs_lex.html) 应该是可以的。我错过了什么? 有没有更好的方法来实现这个约束?

此致

expr1 += expr2 + 50这样的表达式只有编程语言支持,它支持运算符重载,如the documentation:

In .NET languages that support operator overloading, you generally build linear expressions using overloaded operators. [...]

link https://www.gurobi.com/documentation/9.0/refman/cs_lex.html is referencing the .NET API Details for the .NET languages. You want to look at https://www.gurobi.com/documentation/9.0/refman/java_lex.html which is for the Java API Details。那里有可以使用 add*() 方法添加两个 expressions/terms.

的文档