使用ILOG Cplex Java API生成LP约束

时间:2018-04-08 20:48:54

标签: java cplex

我正在学习Cplex Java API来解决TSP问题。我能够生成obj(具有随机权重的城市之间的路径),如下所示:

randValue x12 + randValue x13 + randValue x14 + ...

使用此代码:

      for(int i = 0; i < n; i++) {
            x[i] = cplex.numVarArray(n, 0.0, Double.MAX_VALUE);
            for(int j = 0; j < n; j++) {
                if(i != j) {
                    if (i + 1 > 10 && j + 1 < 10) {
                        x[i][j].setName("x" + (i + 1) + "0" + (j + 1));
                    } else {
                        x[i][j].setName("x" + (i + 1) + (j + 1));
                    }                   
                    expr.addTerm(50 + r.nextInt(951), x[i][j]);
                } 
            }
        }

然后,我想为5个城市添加一个约束,如下所示:

x12 + x13 + x14 + x15 = 1

使用此代码:

       for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                if(i != j) {
                    if(j >= n - 1) {
                        cplex.addEq(cplex.sum(x[i][j],0), 1);
                    } else {
                        cplex.addEq(cplex.sum(x[i][j],x[i][j + 1]), 1);
                    }
                    j++;
                }
            }
        }

但是当我编写System.out.println(cplex.toString())时;它提供如下输出:

IloModel  {
IloMinimize  : (389.0*x12 + 292.0*x13 + 298.0*x14 + 506.0*x15 + 777.0*x21 + 
333.0*x23 + 985.0*x24 + 501.0*x25 + 899.0*x31 + 349.0*x32 + 237.0*x34 + 
611.0*x35 + 219.0*x41 + 310.0*x42 + 347.0*x43 + 170.0*x45 + 773.0*x51 + 
362.0*x52 + 118.0*x53 + 629.0*x54)
IloRange  : 1.0 <= (1.0*x12 + 1.0*x13) <= 1.0
IloRange  : 1.0 <= (1.0*x14 + 1.0*x15) <= 1.0
IloRange  : 1.0 <= (1.0*x21 + 1.0*[0.0..infinity]) <= 1.0
IloRange  : 1.0 <= (1.0*x23 + 1.0*x24) <= 1.0
IloRange  : 1.0 <= (1.0*x25) <= 1.0
IloRange  : 1.0 <= (1.0*x31 + 1.0*x32) <= 1.0
IloRange  : 1.0 <= (1.0*x34 + 1.0*x35) <= 1.0
IloRange  : 1.0 <= (1.0*x41 + 1.0*x42) <= 1.0
IloRange  : 1.0 <= (1.0*x43 + 1.0*[0.0..infinity]) <= 1.0
IloRange  : 1.0 <= (1.0*x45) <= 1.0
IloRange  : 1.0 <= (1.0*x51 + 1.0*x52) <= 1.0
IloRange  : 1.0 <= (1.0*x53 + 1.0*x54) <= 1.0
IloRange  : 1.0 <= (1.0*x21 + 1.0*x31) <= 1.0
IloRange  : 1.0 <= (1.0*x41 + 1.0*x51) <= 1.0
IloRange  : 1.0 <= (1.0*x12 + 1.0*[0.0..infinity]) <= 1.0
IloRange  : 1.0 <= (1.0*x32 + 1.0*x42) <= 1.0
IloRange  : 1.0 <= (1.0*x52) <= 1.0
IloRange  : 1.0 <= (1.0*x13 + 1.0*x23) <= 1.0
IloRange  : 1.0 <= (1.0*x43 + 1.0*x53) <= 1.0
IloRange  : 1.0 <= (1.0*x14 + 1.0*x24) <= 1.0
IloRange  : 1.0 <= (1.0*x34 + 1.0*[0.0..infinity]) <= 1.0
IloRange  : 1.0 <= (1.0*x54) <= 1.0
IloRange  : 1.0 <= (1.0*x15 + 1.0*x25) <= 1.0
IloRange  : 1.0 <= (1.0*x35 + 1.0*x45) <= 1.0
IloRange  : 0.0 <= (1.0*x12 - 1.0*x21) <= 0.0
IloRange  : 0.0 <= (1.0*x13 - 1.0*x31) <= 0.0
IloRange  : 0.0 <= (1.0*x14 - 1.0*x41) <= 0.0
IloRange  : 0.0 <= (1.0*x15 - 1.0*x51) <= 0.0
IloRange  : 0.0 <= (-1.0*x12 + 1.0*x21) <= 0.0
IloRange  : 0.0 <= (1.0*x23 - 1.0*x32) <= 0.0
IloRange  : 0.0 <= (1.0*x24 - 1.0*x42) <= 0.0
IloRange  : 0.0 <= (1.0*x25 - 1.0*x52) <= 0.0
IloRange  : 0.0 <= (-1.0*x13 + 1.0*x31) <= 0.0
IloRange  : 0.0 <= (-1.0*x23 + 1.0*x32) <= 0.0
IloRange  : 0.0 <= (1.0*x34 - 1.0*x43) <= 0.0
IloRange  : 0.0 <= (1.0*x35 - 1.0*x53) <= 0.0
IloRange  : 0.0 <= (-1.0*x14 + 1.0*x41) <= 0.0
IloRange  : 0.0 <= (-1.0*x24 + 1.0*x42) <= 0.0
IloRange  : 0.0 <= (-1.0*x34 + 1.0*x43) <= 0.0
IloRange  : 0.0 <= (1.0*x45 - 1.0*x54) <= 0.0
IloRange  : 0.0 <= (-1.0*x15 + 1.0*x51) <= 0.0
IloRange  : 0.0 <= (-1.0*x25 + 1.0*x52) <= 0.0
IloRange  : 0.0 <= (-1.0*x35 + 1.0*x53) <= 0.0
IloRange  : 0.0 <= (-1.0*x45 + 1.0*x54) <= 0.0

}

如何生成约束 x12 + x13 + x14 + x15 = 1

1 个答案:

答案 0 :(得分:1)

             <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.4.0</version>
            </dependency>
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>2.4.0</version>
            </dependency>

最后一行打印:

        IloCplex cplex = new IloCplex();
        int n = 5;
        IloNumVar[][] x = new IloNumVar[n][];
        for (int i = 0; i < n; i++) {
            x[i] = cplex.boolVarArray(n,
                    new String[]{
                            "x_" + (i + 1) + "1",
                            "x_" + (i + 1) + "2",
                            "x_" + (i + 1) + "3",
                            "x_" + (i + 1) + "4",
                            "x_" + (i + 1) + "5",
                    });
        }
        IloLinearNumExpr expr = cplex.linearNumExpr();
        for (int j = 0; j < n; j++) {
            expr.addTerm(1.0, x[0][j]);
        }
        cplex.addEq(expr,1.0);

        System.out.println(cplex.toString());