gpt4 book ai didi

java - 使用 ILOG Cplex Java API 生成 LP 约束

转载 作者:行者123 更新时间:2023-11-30 06:13:37 26 4
gpt4 key购买 nike

我正在学习 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

最佳答案

        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());

最后一行打印:

IloModel  {
IloRange : 1.0 <= (1.0*x_11 + 1.0*x_12 + 1.0*x_13 + 1.0*x_14 + 1.0*x_15) <= 1.0

}

关于java - 使用 ILOG Cplex Java API 生成 LP 约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49722656/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com