Gurobi 800,Java,检索变量数returns 零

Gurobi 800, Java, retrieving number of variables returns zero

检索模型变量和约束数量的方法似乎不起作用。

代码如下:

        GRBVar x = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "x");

        GRBLinExpr expr = new GRBLinExpr();
        expr.addTerm(1.0, x);
        GRBConstr c0 = model.addConstr(expr, GRB.LESS_EQUAL, 4.0, "c0");

        System.out.println("Number of variables: " + model.get(IntAttr.NumVars));
        System.out.println("Number of constraints: " + model.get(IntAttr.NumConstrs));
        System.out.println("UB = " + c0.get(DoubleAttr.RHS));

输出:

Number of variables: 0
Number of constraints: 0
Error code: 10006. Error at GRBConstr.get

据我所知,您必须在创建变量之后和添加约束之前调用 model.update()