I'm new to CPLEX, starter using it, from scratch,2 days ago, for a university project, and I've been struggling quite a bit with it. I'm sure it's not the most efficient way to do this, but this is my .mod file:
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];
dvar boolean y[N][N][T][V];
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 what's on my .dat file:
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 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[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]];
Basically, what I have to do is use the data given on an article, and use that model to prove that it works, and obtain the solution that they want. However, when I run this, I get 4 errors in the first constraint:
-CPLEX cannot extract expression: forall...
-Index out of bound for array "y#0#0":0
-OPL cannot extract expression: forall...
-OPL cannot extract expression: sum...
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.
The full .mod that works fine
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];
dvar boolean y[N][N][T][V];
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:(t-1) in T)
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;
}