Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.

Β View Only
Expand all | Collapse all

Help with error codes (CPLEX/OPL cannot extract expression, index out of bound for array)

ALEX FLEISCHER

ALEX FLEISCHERFri December 15, 2023 04:13 AM

  • 1.  Help with error codes (CPLEX/OPL cannot extract expression, index out of bound for array)

    Posted Thu December 14, 2023 11:22 AM

    Hello! I started with CPLEX 2 days ago, to develop a university project, without having any prior knowledge. Needless to say, it's been hard to learn everything in this little timeframe. The objective is to program the following model, and run it with certain given data.

    This is my .mod:

    int n=...; //set of terminals
    range N=1..n;
    
    int t=...; //set of time periods
    range T=1..t;
    
    int v=...; //set of vehicles
    range V=1..v;
    
    range Np=1..n; range Vp=1..v;
       float p[Vp][Np][Np] =...;
       
    range Nc=1..n; range Vc=1..v;
       float c[Vc][Nc][Nc] =...;
       
    range Nd=1..n; range Td=1..t;
       float d[Td][Nd][Nd] =...;
    
    range Nm=1..n; range Tm=1..t; range Vm=1..v;
       int m[Vm][Nm][Tm] =...;
       
    range NA=1..n; range VA=1..v;
       int A[VA][NA][NA] =...;
       
    range Ntau=1..n;;
       int tau[Ntau][Ntau] =...;
       
    dvar boolean x[N][N][T][V]; //π‘₯𝑖𝑗𝑑𝑣 ∢ takes the value of 1 if vehicle 𝑣 ∈ 𝑉 start moving a load from terminal 𝑖 to terminal 𝑗 beginning at time 𝑑 to satisfy the demand 𝑑𝑖𝑗𝑑, βˆ€π‘– ∈ 𝑁, 𝑗 ∈ 𝑁 and 𝑑 ∈ 𝑇 , and 0 otherwise;
    dvar boolean y[N][N][T][V]; //𝑦𝑖𝑗𝑑𝑣 ∢ takes the value of 1 if vehicle 𝑣 ∈ 𝑉 start moving empty from terminal 𝑖 to terminal 𝑗 beginning at time 𝑑, βˆ€π‘– ∈ 𝑁, 𝑗 ∈ 𝑁 and 𝑑 ∈ 𝑇 , and 0 otherwise.
    
    dexpr float f=sum(i in N,j in N:i!=j,t in T, v in V)(p[v][i][j]*x[i][j][t][v] - c[v][i][j]*y[i][j][t][v]);
       
    maximize f;
    subject to {
      forall(i in N, t in T, v in V)sum(j in N)(x[i][j][t][v] + y[i][j][t][v])- sum(k in N: k != i, t_ in T: t_ > tau[k][i])(x[k][i][t_-tau[k][i]][v] + y[k][i][t_-tau[k][i]][v])- y[i][i][t-1][v] == m[v][i][t];
    
      forall(i in N, j in N, t in T)
        sum(v in V) x[i][j][t][v] <= d[t][i][j];
    
      forall(i in N, j in N, t in T, v in V)
        A[v][i][j] == 0 => x[i][j][t][v] == 0 && y[i][j][t][v] == 0;
    
      forall(i in N, j in N, t in T, v in V)
        x[i][j][t][v] >= 0 && x[i][j][t][v] <= 1 && y[i][j][t][v] >= 0 && y[i][j][t][v] <= 1;
    }
     
       

    and this is my .dat:

    n = 6;
    t = 6;
    v = 2;
    
    c = [[  [0 1 2 2 2 2]
     [1 0 2 2 2 2]
     [2 2 0 2 1 1]
     [2 2 2 0 1 1]
     [2 2 1 1 0 1]
     [2 2 1 1 1 0]]
     
     [  [0 3 3 2 2 2]
     [3 0 3 3 2 2]
      [3 3 0 1 2 2]
      [2 3 1 0 3 3]
      [2 2 2 3 0 3]
      [2 2 2 3 3 0]]
      ]; 
      
    p = [[  [0 1.8 3.6 3.6 3.6 3.6]
     [1.8 0 3.6 3.6 3.6 3.6]
     [3.6 3.6 0 3.6 1.8 1.8]
     [3.6 3.6 3.6 0 3.6 3.6]
     [3.6 3.6 1.8 3.6 0 1.8]
     [3.6 3.6 1.8 3.6 1.8 0]]
     
     [  [0 4.2 4.2 3.6 3.6 3.6]
     [4.2 0 4.2 4.2 3.6 3.6]
      [4.2 4.2 0 4.5 3.6 3.6]
      [3.6 4.2 4.5 0 4.2 4.2]
      [3.6 3.6 3.6 4.2 0 4.2]
      [3.6 3.6 3.6 4.2 4.2 0]]
      ]; 
      
    //d_ijt
    d = [[[0 0 0 0 0 0] 
     [0 0 0 0 0 0]
     [0 0 0 0 0 0]
     [0 0 0 0 0 0]
     [0 0 0 0 0 0]
     [0 0 0 0 0 0]]
     
      [[0 0 0 0 0 0]
      [0 0 0 0 0 0]
      [0 0 0 0 0 0]
      [0 3 0 0 0 0]
      [0 0 0 0 0 1]
      [0 0 0 0 0 0]]
      
      [[0 0 0 0 0 0]
      [0 0 3 0 0 0]
      [0 0 0 0 0 0]
      [0 0 0 0 0 0]
      [0 0 0 0 0 0]
      [0 0 0 0 0 0]]
      
      [[0 0 0 0 0 0]
      [0 0 0 0 0 0]
      [0 0 0 0 0 0]
      [0 0 0 0 0 0]
      [0 0 0 0 0 0]
      [0 0 0 0 0 0]]
      
      [[0 0 0 0 0 0]
      [0 0 0 0 0 0]
      [0 0 0 0 0 0]
      [0 0 0 0 0 0]
      [2 0 0 0 0 0]
      [0 0 0 0 0 0]]
      
      [[0 0 0 0 0 0]
      [0 0 0 0 0 0]
      [0 0 0 0 0 0]
      [0 0 0 0 0 0]
      [0 0 0 0 0 0]
      [0 0 0 0 0 0]]
      ]; 
      
      
    m = [[  [0 0 0 0 0 0] //m_itv
     [0 0 0 0 0 0]
     [0 0 0 0 0 0]
     [0 0 0 0 0 0]
     [0 0 0 0 0 0]
     [1 0 0 0 0 0]]
     
     [  [0 0 0 0 0 0]
     [0 0 0 0 0 0]
      [0 0 0 0 0 0]
      [0 1 0 0 0 0]
      [0 0 0 0 0 0]
      [0 0 0 0 0 0]]
      ]; 
      
    A = [[  [1 0 1 1 1 1] //A_ijv
     [0 1 1 1 1 1]
     [1 1 1 1 1 1]
     [1 1 1 1 1 1]
     [1 1 1 1 1 1]
     [1 1 1 1 1 1]]
     
     [[1 0 1 1 1 1]
     [0 1 1 1 1 1]
      [1 1 1 1 1 1]
      [1 1 1 1 1 1]
      [1 1 1 1 1 1]
      [1 1 1 1 1 1]]
      ]; 
      
      
    tau = [[1 2 1 1 1 2] //tau_ij
     [2 1 2 1 2 3]
     [1 2 1 1 3 2]
     [1 1 1 1 1 3]
     [1 2 3 1 1 1]
     [2 3 2 3 1 1]];

    I'm sure there are easier and more efficient ways to do what I'm trying to do, but I would like to know what's wrong with my code? 

    All of these errors are happening on constraint (2), so I'm not sure if there's anything else wrong. 

    Thanks!



    ------------------------------
    TomΓ‘s Bandeira
    ------------------------------


  • 2.  RE: Help with error codes (CPLEX/OPL cannot extract expression, index out of bound for array)

    Posted Fri December 15, 2023 04:13 AM

    same question at https://stackoverflow.com/questions/77655403/cplex-cannot-extract-expression/77659049#77659049

    if you change

    forall(i in N, t in T, v in V)
        sum(j in N)(x[i][j][t][v] + y[i][j][t][v])
    

    into

    forall(i in N, t in T, v in V:(t-1) in T)
        sum(j in N)(x[i][j][t][v] + y[i][j][t][v])
    

    your model will work fine. You have an out of bound in the range.



    ------------------------------
    [Alex] [Fleischer]
    [Data and AI Technical Sales]
    [IBM]
    ------------------------------