If else on the date in CPLEX OPL

107 Views Asked by At

I want to make, if total production task "i" * duration at day 15 <= not equal to our target then choose alternative 2,else choose alternative 1. I made the data on tuple. but I still confuse to determine for at day x the calculation must be more or less then the result. THis is my model.

Tuple Precedence {
  key int id;
  {int}   succs;
  int RelDate;
  int VolCost;
}
{Precedence} Precedences = ...;

tuple Mode {
  key int taskId;
  key int id;
  int pt;
  int costprod;
  int dmdIntRes [IntRes];
  int dmdExtRes [ExtRes];
}
{Mode} Modes = ...;

dvar interval Tasks [p in Precedences]in p.RelDate..EndMax   ; //in p.RelDate..EndMax in 0..EndMax
dvar interval mode[m in Modes] optional  size m.pt;
//dvar boolean m1 [m in Modes];

cumulFunction IntResUsage[r in IntRes] = 
  sum (m in Modes: m.dmdIntRes[r]>0) pulse(mode[m], m.dmdIntRes[r]);
  
cumulFunction ExtResUsage[r in ExtRes] = 
  sum (m in Modes: m.dmdExtRes[r]>0) pulse(mode[m], m.dmdExtRes[r]);
  
execute {
        cp.param.FailLimit = 10000;
}
 
minimize max(p in Precedences) endOf(Tasks[p]);
subject to {
  forall (p in Precedences, m in Modes) {
 
    alternative(Tasks[p], all(m in Modes: m.taskId==p.id) mode[m])

This is an example my tuple data for modes

Modes = {
  < 0, 1, 3,  700000,[ 2 ], [ 0 ]  >,
  < 0, 2, 2, 1060000,[ 2 ], [ 1 ] >,
  < 1, 1,24,   70313,[ 2 ], [ 0 ]  >,
  < 1, 2,20,   80357,[ 2 ], [ 1 ]  >,
  < 2, 1,24,  143750,[ 5 ], [ 0 ]  >,
  < 2, 2,20,  164286,[ 5 ], [ 1 ]  >,
  < 3, 1, 5, 1500000,[ 6 ], [ 0 ]  >,
2

There are 2 best solutions below

0
Alex Fleischer On

as said in How to make a if and else in alternative function in OPL?

you could rely on presenceOf

With your .dat

Modes = {
  < 0, 1, 3,  700000,[ 2 ], [ 0 ]  >,
  < 0, 2, 2, 1060000,[ 2 ], [ 1 ] >,
  < 1, 1,24,   70313,[ 2 ], [ 0 ]  >,
  < 1, 2,20,   80357,[ 2 ], [ 1 ]  >,
  < 2, 1,24,  143750,[ 5 ], [ 0 ]  >,
  < 2, 2,20,  164286,[ 5 ], [ 1 ]  >,
  < 3, 1, 5, 1500000,[ 6 ], [ 0 ]  >,
  };

.mod

using CP;

int EndMax=100;
range IntRes=1..1;
range ExtRes=2..2;

tuple Precedence {
  key int id;
  
}
{Precedence} Precedences = {<1>};

tuple Mode {
  key int taskId;
  key int id;
  int pt;
  int costprod;
  int dmdIntRes [IntRes];
  int dmdExtRes [ExtRes];
}
{Mode} Modes = ...;

dvar interval Tasks [p in Precedences]in 1..1000; 
dvar interval mode[m in Modes] optional size m.pt;



 
minimize max(p in Precedences) endOf(Tasks[p]);
subject to {
  forall (p in Precedences, m in Modes) {
 
    alternative(Tasks[p], all(m in Modes: m.taskId==p.id) mode[m]);   
}   

// if then with presenceOf
  forall (p in Precedences)
      (lengthOf(Tasks[p])>=15) == presenceOf(mode[first(Modes)]);

} 

works and can help you with the syntax

0
Beta On

but i think it little bit understanding, the day 15 is like the calendar. for an example if at day 15 = cost production 500/day * duration <= 1000(planning value) then choose second modes