Originally posted by: beratpostalci
Hi all,
I'm learning Cplex Java API to solve TSP. I am able to generate obj (paths between cities with random weights) like below:
randValue x12 + randValue x13 + randValue x14 + ...
with this code:
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]);
}
}
}
Then, I want to add a constraint for 5 cities like below:
x12+x13+x14+x15 = 1
with this code:
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++;
}
}
}
However when I write System.out.println( cplex.toString() ); It gives output as below:
IloModel {
IloMinimize : (738.0*x12 + 968.0*x13 + 268.0*x14 + 732.0*x15 + 431.0*x21 + 173.0*x23 + 675.0*x24 + 174.0*x25 + 371.0*x31 + 831.0*x32 + 236.0*x34 + 279.0*x35 + 293.0*x41 + 119.0*x42 + 673.0*x43 + 465.0*x45 + 125.0*x51 + 433.0*x52 + 875.0*x53 + 561.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
}
How can I generate constraint x12 + x13 + x14 + x15 = 1 ?
#CPLEXOptimizers#DecisionOptimization