cplexpiecewise

In CPLEX, piecewise-based stepwise example result


I'm a student studying optimization using CPLEX. I created a simple stepwise example, but I have some doubts about the results.

dvar float x;
dvar float y;

dexpr float z= piecewise {0->0; 10->0; 0->10; 5->10; 0->20; 5->20; 0} (1,10) x;
dexpr float goal = y+z;

minimize goal;

subject to {

9<=x;
x<=18;
4<=y;
y<=10;
}

In the above formula, I expected x and y to be 9 and 4 respectively, and the solution to yield the expected result of 14. (when x is 9, z becomes 10, so y+z=4+10=14.) However, why does the decision variable show x as 10 and the goal as 19 instead?

I would appreciate your response. Thank you.


Solution

  • Your piecewise is discontinuous, which means for 10 the result is either 10 or 15

    dvar float x;
    dvar float y;
    
    dvar float z;
    dvar float goal;
    
    pwlFunction f=piecewise {0->0; 10->0; 0->10; 5->10; 0->20; 5->20; 0} (1,10);
    
    minimize goal;
    
    subject to {
      
       z== f(x);
     goal== y+z;
    
    9<=x;
    x<=18;
    4<=y;
    y<=10;
    
    }
    
    execute
    {
      writeln(z);
      writeln(f(x));
    }
    

    what you could do in order to remove this choice is write

    (x==10) => (z==15);
    

    and then

    dvar float x;
    dvar float y;
    
    dvar float z;
    dvar float goal;
    
    pwlFunction f=piecewise {0->0; 10->0; 0->10; 5->10; 0->20; 5->20; 0} (1,10);
    
    minimize goal;
    
    subject to {
      
       z== f(x);
     goal== y+z;
    
    9<=x;
    x<=18;
    4<=y;
    y<=10;
    
    (x==10) => (z==15);
    
    }
    
    execute
    {
      writeln(z);
      writeln(f(x));
    }
    

    gives

    10
    10
    

    and you get x=9