hello,

I am trying to solve the following model in CPLEX, however, I am having in trouble in the part where I want to find min or max of a set. So I have a set of completion times for order i as C[i][j] = {2,3,5} and I want to get max (C_late) which is 5 and min (C_early) which is 2. And then I wand to minimize the difference between them. I think I need to use big M method however I am not sure why its not working.

```
int I=...;
int J=...;
int K=...;
int T=...;
range Order = 1..I;
range Job = 1..J;
range Machine=1..K;
range Position = 1..T;
int p[Order][Job]=...;
int a[Order][Job][Machine]=...;
dvar boolean x[Order][Job][Machine][Position];
dvar boolean z[Order][Job];
dvar int+ C_late[Order];
dvar int+ C_early[Order];
dvar int FTW[Order];
dvar int CD;
dvar int+ y [Machine][Position];
dvar int+ C[Order][Job];
dvar int Cmax;
minimize
sum(i in Order) (C_late[i] - C_early[i]);
subject to{
// Ensure that a job is scheduled on one position only
forall(i in Order, j in Job: p[i][j]>0) sum(t in Position, m in Machine)
x[i][j][m][t] == 1;
forall(m in Machine, t in Position) sum(i in Order, j in Job: p[i][j]>0)
x[i][j][m][t] <= 1;
forall(i in Order, j in Job: p[i][j]>0 , m in Machine, t in Position)
x[i][j][m][t] - a[i][j][m] <= 0;
forall (m in Machine)
y[m][1] >= sum(i in Order, j in Job) p[i][j]*x[i][j][m][1];
forall(m in Machine, t in Position: t>=2)
y[m][t] >= y[m][t-1] + sum(i in Order, j in Job) p[i][j]*x[i][j][m][t];
forall(i in Order, j in Job: p[i][j]>0, m in Machine, t in Position)
C[i][j] >= y[m][t] - 100000*(1 - x[i][j][m][t]);
forall(m in Machine)
sum(i in Order, j in Job, t in Position) p[i][j]*x[i][j][m][t] - Cmax <= 0;
**forall(i in Order, j in Job: p[i][j]>0)
C_early[i] <= C[i][j];
forall(i in Order, j in Job: p[i][j]>0)
C_late[i] >= C[i][j];**
}
```

and here is sample data set:

```
J=3;
K=3;
T=10;
I=10;
p= [
[15,0,0],
[14,0,0],
[16,0,0],
[15,0,0],
[14,0,0],
[16,0,0],
[16,0,0],
[14,0,0],
[15,0,0],
[17,16,14]
];
a = [
[[1,1,1], [0,0,0], [0,0,0]],
[[0,1,1], [0,0,0], [0,0,0]],
[[1,1,1], [0,0,0], [0,0,0]],
[[1,0,1], [0,0,0], [0,0,0]],
[[0,1,1], [0,0,0], [0,0,0]],
[[1,1,1], [0,0,0], [0,0,0]],
[[0,1,1], [0,0,0], [0,0,0]],
[[0,1,1], [0,0,0], [0,0,0]],
[[1,1,1], [0,0,0], [0,0,0]],
[[1,1,1], [1,1,1], [1,0,1]],
];
```

So is it possible to minimize function like this?

Your approach to C_early and C_late is correct. What do you mean by "not working"?

When I run the model, it will change the variable C[i][j] to wrong values just to minimize objective function to 0