Is there a constraint to pieces of the stateFunction only go in ascending or descending order?

49 Views Asked by At

I have 3 intervals:

dvar interval MyInterval[0..2];

And I have state function

stateFunction MyStateFunction;

And I have constraints:

alwysConstant(MyStateFunction, MyInterval[i]); alwyasIn(MyStateFunction, MyInterval[i], value_min[i], value_max[i]);

As a result, I can get a graph like this:

IMG_1 or IMG_2

But can I write a constraint so that the pieces of the function only go in ascending or descending order? (so the situation on IMG_2 is undesirable)

1

There are 1 best solutions below

0
Alex Fleischer On BEST ANSWER

I would use alternative:

using CP;

int value_min[0..2]=[1,1,1];
int value_max[0..2]=[3,3,3];

dvar interval MyInterval[0..2] size 10;
dvar interval MyIntervalOptions[0..2][1..3] optional;

dvar int value[0..2];



stateFunction MyStateFunction;

subject to
{
  endBeforeStart(MyInterval[0],MyInterval[1]);
  endBeforeStart(MyInterval[1],MyInterval[2]);
  
  forall(i in 0..2) alternative(MyInterval[i],all(o in 1..3)MyIntervalOptions[i][o]);
  
forall(i in 0..2)
  {
    alwaysConstant(MyStateFunction, MyInterval[i]); 
    alwaysIn(MyStateFunction, MyInterval[i], value_min[i], value_max[i]);
    forall(o in 1..3) alwaysEqual(MyStateFunction, MyIntervalOptions[i][o],o);
    value[i]==sum(o in 1..3) presenceOf(MyIntervalOptions[i][o])*o;
    }
    
  (value[0]<value[1])   && (value[1]<value[2]) || (value[0]>value[1])   && (value[1]>value[2]);
    
}

which gives

enter image description here