简体   繁体   English

仅在约束Gurobi Java中使用决策变量

[英]Use decision variable only in constraint Gurobi Java

Usually in Gurobi's Java interface I create the model by adding variables to the model with this for example: 通常,在Gurobi的Java界面中,我通过向模型添加变量来创建模型,例如:

for (int i = 0; i < n; i++){
    for (int j = 0; j < n; j++) {
        XobjectiveCoef = distance(i, j);
        X[i][j] = model.addVar(0.0, 1.0, XobjectiveCoef, GRB.BINARY, "x" + String.valueOf(i) + "_" + String.valueOf(j));

    }
}

When I am done I have my model which is the sum of all Xs over i and j, and I can use X[i][j] in any constraint I want with something like this: 完成后,我得到了模型,该模型是i和j上所有X的总和,并且我可以在需要的任何约束中使用X[i][j] ,如下所示:

for (int j = 0; j < n; j++) {
    GRBLinExpr expr = new GRBLinExpr();
    for (int i = 0; i < n; i++){
        expr.addTerm(1.0, X[i][j]);
    }
    model.addConstr(expr, GRB.EQUAL, 1.0, "Name_of_constraint");
}

Now what I am looking for, is a way to use decision variable only in a constraint and not in the model definition. 现在,我正在寻找一种仅在约束中而不在模型定义中使用决策变量的方法。 I checked if addVar is available for GRBLinExpr but it is not. 我检查了addVar是否可用于GRBLinExpr但不是。

Thanks 谢谢

You can create a GRBLinExpr and reuse it. 您可以创建GRBLinExpr并重用它。 For example: 例如:

model.addConstr(expr, GRB.EQUAL, 1.0, "ct1");
expr.addTerm(1.0, Y);
expr.addTerm(1.0, Z).
model.addConstr(expr, GRB.GREATER_EQUAL, 2.0, "ct2");

This may or may not be more efficient than creating a new GRBVar object. 这可能比创建新的GRBVar对象更有效,也可能没有效率。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM