Summation in Cplex constraints

2.6k views Asked by At

In CPLEX OPL, how do I write summation of i (from 1 to i-1) in constraints?

another question: how do I input 3 dimensional matrix?

Many thanks in advance,

2

There are 2 answers

0
jjlema On

You can create a dvar with 3 dimensions using something like:

dvar int+ x[1..2][1..7][1..3];

and, in order to sum using a range you can do:

dexpr int obj = sum(i in 1..2, j in 1..7, z in 1..3) x[i][j][z];
0
cemal On

To get 3 dimensional input, you can flatten your 3-d input array to a 1-d array, then assign values to your 3-d array using a simple OPL script code like:

int array[0..I-1][0..J-1][0..K-1];//3-d array 
int input[I*J*K]=...;//1-d array to be read as input

execute assignArray{
var index=0;
for(var i=0;i<I;i++){
  for(var j=0;j<J;j++){
     for(var k=0;k<K;k++){
      array[i][j][k]=input[index];
      index++;
    }
  }    
}
}