0

I've been working on addressing a specific constraint in OPL and attempted to write code to solve it. However, it's not functioning as expected. Could anyone provide guidance on the correct approach to do that.

Here is the constraint:

constraint image

The full code of the model in OPL : [Edited], the constraint is the second one :

// Example data
range J = 1..5;
range R = 1..3;
range T = 1..10;
int P[J] = [0,1,2,3,4]; 
int d[J] = [1, 1, 3, 5, 2];
int EF[J] = [1, 2, 1, 1, 1];
int LF[J] = [2, 3, 4, 6, 2];
int Tbar = sum(j in J) d[j];

// Resource usage matrix
int u[J][R] = [[1, 0, 2],
               [1, 1, 1], 
               [1, 1, 0], 
               [2, 0, 3], 
               [1, 1, 2]];

// Resource availability
int a[R]= [1,2,3];

// Decision Variables
dvar boolean x[J][T];

dexpr int CT = sum(j in J, t in EF[j]..LF[j])t*x[j][t];
minimize CT;

subject to {
  forall(j in J)
     sum (t in EF[j]..LF[j]) x[j][t] == 1;
  forall(j in J, i in {P[j]}) //no job must be started before all its predecessors have been completed.
     sum (t in EF[j]..LF[j]:t in T) ((t - d[j]) * x[j][t]) - 
     sum (t in EF[i]..LF[i]:t in T) t * x[i][t] >= 0;
  forall(r in R, t in 1..Tbar) {
     sum(j in J) u[j][r] * sum(q in maxl(t, EF[j])..minl(t + d[j] - 1, LF[j])) x[j][q] <= a[r];
  }
}

I've attempted to apply my knowledge in OPL to address this issue, but I'm encountering difficulties. I would appreciate any advice on how to resolve this.

1
  • Welcome to Stack Overflow. It would be helpful to expand your questions to include a complete minimal reproducible example, i.e. to include both inputs, expected outputs and actual outputs. Simply stating that your code doesn't work always runs the risk of your question being closed as it "Needs Debugging Details". Commented Mar 30, 2024 at 8:28

1 Answer 1

1

You get many out of bounds

The following model works better

range J = 0..5;
range R = 1..3;
range T = 1..10;
int P[J] = [0,1,2,3,4]; 
int d[J] = [1, 1, 3, 5, 2];
int EF[J] = [1, 2, 1, 1, 1];
int LF[J] = [2, 3, 4, 6, 2];



dvar int x[J][T];
subject to {
  forall(j in J, i in {P[j]})
    sum (t in EF[j]..LF[j]:t in T) ((t - d[j]) * x[j][t]) - 
    sum (t in EF[i]..LF[i]:t in T) t * x[i][t] >= 0;    
}

and later on with your second model, the following works fine

// Example data
range J = 0..5;
range R = 1..3;
range T = 1..10;
int P[J] = [0,1,2,3,4]; 
int d[J] = [1, 1, 3, 5, 2];
int EF[J] = [1, 2, 1, 1, 1];
int LF[J] = [2, 3, 4, 6, 2];
int Tbar = sum(j in J) d[j];

// Resource usage matrix
int u[J][R] = [[1, 0, 2],
               [1, 1, 1], 
               [1, 1, 0], 
               [2, 0, 3], 
               [1, 1, 2]];

// Resource availability
int a[R]= [1,2,3];

// Decision Variables
dvar boolean x[J][T];

dexpr int CT = sum(j in J, t in EF[j]..LF[j]:t in T)t*x[j][t];
minimize CT;

subject to {
  forall(j in J)
     sum (t in EF[j]..LF[j]:t in T) x[j][t] <= 1;
  forall(j in J, i in {P[j]}) //no job must be started before all its predecessors have been completed.
     sum (t in EF[j]..LF[j]:t in T) ((t - d[j]) * x[j][t]) - 
     sum (t in EF[i]..LF[i]:t in T) t * x[i][t] >= 0;
  forall(r in R, t in 1..Tbar) {
     sum(j in J) u[j][r] * sum(q in maxl(t, EF[j])..minl(t + d[j] - 1, LF[j])) x[j][q] <= a[r];
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi @AlexFleischer, I've encountered an issue with integrating the provided solution into the model; unfortunately, it isn't functioning as expected. I've made adjustments to my original post and included the complete code for the model, which is intended for the Resource-Constrained Project Scheduling Problem (RCPSP).
For RCPSP you should have a look at CPOptimizer within CPLEX : examples/opl/sched_rcpsp

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.