Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.

 View Only
Expand all | Collapse all

non linear constraint

ALEX FLEISCHER

ALEX FLEISCHERFri July 14, 2017 05:26 AM

Archive User

Archive UserMon March 04, 2019 06:13 AM

ALEX FLEISCHER

ALEX FLEISCHERMon March 04, 2019 10:17 AM

ALEX FLEISCHER

ALEX FLEISCHERTue March 05, 2019 07:03 AM

ALEX FLEISCHER

ALEX FLEISCHERTue March 05, 2019 11:04 AM

ALEX FLEISCHER

ALEX FLEISCHERFri March 08, 2019 02:00 AM

ALEX FLEISCHER

ALEX FLEISCHERFri March 08, 2019 02:50 AM

ALEX FLEISCHER

ALEX FLEISCHERTue March 12, 2019 05:00 AM

ALEX FLEISCHER

ALEX FLEISCHERTue March 12, 2019 07:31 AM

  • 1.  non linear constraint

    Posted Thu July 13, 2017 02:43 AM

    Originally posted by: sandeepsinghchauhan


    Is there any example which solves a non linear constraints in opl (except quadratic programming)? I mean How can i convert a non linear constraints into linear constraints (any example) which helps me to solve a non linearproblem.

    Please help


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: non linear constraint

    Posted Thu July 13, 2017 10:58 AM

    Hi,

    let me give you a tiny example.

    Suppose you want to minimize x*x*x*x-log(x) with x between 0 and 1

    With CPO you could write

    using CP;

    int scale=1000000;

    dvar int+ scalex in 1..scale;


    dexpr float x = scalex/scale;


    minimize pow(x,4)-log(x);

    subject to {
       
    }

    with CPLEX you could write

    float firstSlope=0;
     float lastSlope=0;
     
     tuple breakpoint // y=f(x)
     {
      key float x;
      float y;
     }
     
     sorted { breakpoint } breakpoints={<i/100,pow(i/100,4)-log(i/100)> | i in 1..100};
     
     float slopesBeforeBreakpoint[b in breakpoints]=
     (b.x==first(breakpoints).x)
     ?firstSlope
     :(b.y-prev(breakpoints,b).y)/(b.x-prev(breakpoints,b).x);
     
     pwlFunction f=piecewise(b in breakpoints)
     { slopesBeforeBreakpoint[b]->b.x; lastSlope } (first(breakpoints).x, first(breakpoints).y);
     
     
     dvar float x in 0.01..1;
     minimize f(x);
     
     subject to
     {
     
     }

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: non linear constraint

    Posted Thu July 13, 2017 11:16 AM

    Originally posted by: sandeepsinghchauhan


    Thanks a lot Alex. From where I can get more examples to solve nonlinear problem with CPLEX.


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: non linear constraint



  • 5.  Re: non linear constraint

    Posted Fri July 14, 2017 02:49 AM

    Originally posted by: sandeepsinghchauhan


    what is happening over in this equation:

     

    float slopesBeforeBreakpoint[b in breakpoints]= (b.x==first(breakpoints).x) ? firstSlope:(b.y-prev(breakpoints,b).y)/(b.x-prev(breakpoints,b).x);

    meaning of "?".


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 6.  Re: non linear constraint

    Posted Fri July 14, 2017 02:55 AM


  • 7.  Re: non linear constraint

    Posted Fri July 14, 2017 04:52 AM

    Originally posted by: sandeepsinghchauhan


    Alex thanks for showing the formulation of X^4-log(x) on CPLEX. How will I formulate the constraints for the same? Please


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 8.  Re: non linear constraint

    Posted Fri July 14, 2017 05:26 AM

    hi

    once you have f you may use that in a constraint

    float firstSlope=0;
     float lastSlope=0;
     
     tuple breakpoint // y=f(x)
     {
      key float x;
      float y;
     }
     
     sorted { breakpoint } breakpoints={<i/100,pow(i/100,4)-log(i/100)> | i in 1..100};
     
     float slopesBeforeBreakpoint[b in breakpoints]=
     (b.x==first(breakpoints).x)
     ?firstSlope
     :(b.y-prev(breakpoints,b).y)/(b.x-prev(breakpoints,b).x);
     
     pwlFunction f=piecewise(b in breakpoints)
     { slopesBeforeBreakpoint[b]->b.x; lastSlope } (first(breakpoints).x, first(breakpoints).y);
     
     
     dvar float x in 0.01..1;
     maximize (x);
     
     subject to
     {
     f(x)<=0.5;
     }

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 9.  Re: non linear constraint

    Posted Mon March 04, 2019 06:13 AM

    Originally posted by: sandeepsinghchauhan


    Hi,

     

    I have 1 to 20students and My equation is Y = aX^2 +bX+c.

    a,b, and c values changes with students. Like all 20 student have their own different a, b and c values.

    Correspond to X decision variable , I want to evaluate Y. Can you help me to formulate this equations in opl? Please


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 10.  Re: non linear constraint

    Posted Mon March 04, 2019 10:17 AM

    Hi

    using CPo that should be easy 

    y==a*x*x+b*x+c;

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 11.  Re: non linear constraint

    Posted Tue March 05, 2019 01:07 AM

    Originally posted by: sandeepsinghchauhan


    Could you please suggest me how to formulate this without CPO? 


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 12.  Re: non linear constraint



  • 13.  Re: non linear constraint

    Posted Tue March 05, 2019 05:45 AM

    Originally posted by: sandeepsinghchauhan


    pwlFunction f[dv in PU]=piecewise(b in breakpoints){ slopesBeforeBreakpoint[b]->b.x; lastSlope } (first(breakpoints).x, first(breakpoints).y);

     

    Where PU is a tuple.

    X is a continuous variable decision variable.

    While in constraintwhen I am evaluating

    f(X[<<1,2>,1,1>]) <=100

    I am getting an error: Cannot use type pwlFunction[PU] for userFunction.   

    Please help em to resolve this


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 14.  Re: non linear constraint

    Posted Tue March 05, 2019 07:03 AM

    Hi,

    in OPL you cannot yet use an array of pwlFunction

    Do not hesitate to log a wish at https://ibmanalytics.ideas.aha.io/?project=CPLEX

    if you think that feature could help

    regards

    NB:

    as a workaround do not forget you could use dexpr array

    Let me adapt a bit https://www.ibm.com/developerworks/community/forums/html/topic?id=8e27069e-ce8e-4131-a04d-436c120c17e9&ps=25

    int nbKids=300;
        float costBus40=500;
        float costBus30=400;

        // If we take more than 4 buses for a given size, 20% cheaper for additional buses
         
        dvar int+ nbBus40;
        dvar int+ nbBus30;

        pwlFunction pricePerQuantity[i in 1..2]=piecewise{1->4;0.8+i-1};
        dexpr float pricePerQuantityExpr40[i in 1..2]=piecewise{1->4;0.8+1-i}nbBus40;
        dexpr float pricePerQuantityExpr30[i in 1..2]=piecewise{1->4;0.8+1-i}nbBus30;

        
         
        minimize
         costBus40*pricePerQuantityExpr40[1]  +pricePerQuantityExpr30[1]*costBus30;
         
        subject to
        {
         40*nbBus40+nbBus30*30>=nbKids;
        }

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 15.  Re: non linear constraint

    Posted Tue March 05, 2019 10:14 AM

    Originally posted by: sandeepsinghchauhan


    tuple breakpoint // y=f(x)
     {
      key float x;
      float y;
     }
    sorted { breakpoint } breakpoints={<i,.06*pow(i,2)-12.07*pow(i,1)+592.76+20.96> | i in 70..135};

    float slopesBeforeBreakpoint[b in breakpoints]=(b.x==first(breakpoints).x)?firstSlope:(b.y-prev(breakpoints,b).y)/(b.x-prev(breakpoints,b).x);
     
    pwlFunction f1=piecewise(b in breakpoints){ slopesBeforeBreakpoint[b]->b.x; lastSlope } (first(breakpoints).x, first(breakpoints).y);

     

    similar to f1 I had made f2, f3 for different breakpoints.

     

    now

    In constraint I want to use f1 value which indicate student 1 value. I want to evaluate f1 value corresponding to X decision variable first value. how can I formulate this? similarly I also want to evaluate f2, f3 value at X[2] and X[3].

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 16.  Re: non linear constraint

    Posted Tue March 05, 2019 11:04 AM

    Hi

    tuple breakpoint // y=f(x)
     {
      key float x;
      float y;
     }
    sorted { breakpoint } breakpoints={<i,.06*pow(i,2)-12.07*pow(i,1)+592.76+20.96> | i in 70..135};

    float firstSlope=0;

    float lastSlope=0;

    float slopesBeforeBreakpoint[b in breakpoints]=(b.x==first(breakpoints).x)?firstSlope:(b.y-prev(breakpoints,b).y)/(b.x-prev(breakpoints,b).x);
     
    pwlFunction f1=piecewise(b in breakpoints){ slopesBeforeBreakpoint[b]->b.x; lastSlope } (first(breakpoints).x, first(breakpoints).y);

    dvar float x[1..3];
    dvar float y;

    subject to
    {
    y==f1(x[1]);
    }

    works fine

     

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 17.  Re: non linear constraint

    Posted Fri March 08, 2019 01:47 AM

    Originally posted by: sandeepsinghchauhan


    range float fr = 1.2..2.2;
    sorted { breakpoint } breakpoints3={<i,.05*pow(i,2)+606.98> | i in fr};

     

    Error:
    Cannot use the type range float with "in". 
    The function f(dvar float+) does not exist.   

     

    Please help me to resolve this

     

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 18.  Re: non linear constraint

    Posted Fri March 08, 2019 02:00 AM

    Hi

    float fr[1..2] = [1.2,2.2];

    tuple breakpoint
    {
    float x;
    float y;
    }

    sorted { breakpoint } breakpoints3={<fr[i],.05*pow(fr[i],2)+606.98> | i in 1..2};

    execute
    {
    writeln(breakpoints3);
    }

     

    gives

     

    {<1.2 607.05> <2.2 607.22>}

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 19.  Re: non linear constraint

    Posted Fri March 08, 2019 02:02 AM

    Originally posted by: sandeepsinghchauhan


    sorted { breakpoint } breakpoints3={<fr[i],.05*pow(fr[i],2)+606.98> | i in 1..2};

     

    in how many points this will break the curve?


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 20.  Re: non linear constraint



  • 21.  Re: non linear constraint

    Posted Fri March 08, 2019 02:15 AM

    Originally posted by: sandeepsinghchauhan


    But dividing a curve at three breakpoints and calculating its slope is going to make my nonlinear curve into  two piecewise linear functions.

    If I increase my breakpoints then my nonlinear curve would be more accurate with piecewise linear. How can I increase my breakpoints?


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 22.  Re: non linear constraint



  • 23.  Re: non linear constraint

    Posted Fri March 08, 2019 02:23 AM

    Originally posted by: sandeepsinghchauhan


    IfI have a nonlinear curve as shown in attached image with black color.  Then, with break points it will break the curve into two pieces as shown in red curves using three points?


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 24.  Re: non linear constraint

    Posted Fri March 08, 2019 02:50 AM

    Hi,

    with your graph, you can handle with one breakpoint.

    - slope before the breakpoint

    - slope after the breakpoint

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 25.  Re: non linear constraint

    Posted Fri March 08, 2019 04:19 AM

    Originally posted by: sandeepsinghchauhan


    tuple breakpoint // y=f(x)
     {
      key float x;
      float y;
     }
    sorted { breakpoint } breakpoints1={<i,.06*pow(i,2)-12.07*pow(i,1)+592.76> | i in 7..13};
    float slopesBeforeBreakpoint[b in breakpoints1]=(b.x==first(breakpoints1).x)?firstSlope:(b.y-prev(breakpoints1,b).y)/(b.x-prev(breakpoints1,b).x);
    pwlFunction f1=piecewise(b in breakpoints1){ slopesBeforeBreakpoint[b]->b.x; lastSlope } (first(breakpoints1).x, first(breakpoints1).y);

     

    How to find f1 value?

    execute{
    writeln(f1(X[<<1, 0.975> ,1>]), "F11");
    }


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 26.  Re: non linear constraint

    Posted Fri March 08, 2019 04:28 AM

    Hi

    tuple breakpoint // y=f(x)
     {
      key float x;
      float y;
     }
     
     float firstSlope=0;
     float lastSlope=0;
    sorted { breakpoint } breakpoints1={<i,.06*pow(i,2)-12.07*pow(i,1)+592.76> | i in 7..13};
    float slopesBeforeBreakpoint[b in breakpoints1]=(b.x==first(breakpoints1).x)?firstSlope:(b.y-prev(breakpoints1,b).y)/(b.x-prev(breakpoints1,b).x);
    pwlFunction f1=piecewise(b in breakpoints1){ slopesBeforeBreakpoint[b]->b.x; lastSlope } (first(breakpoints1).x, first(breakpoints1).y);

     

    //How to find f1 value?

    execute{
    writeln(f1(0.975), " F11");
    }

    works fine

     

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 27.  Re: non linear constraint

    Posted Fri March 08, 2019 04:37 AM

    Originally posted by: sandeepsinghchauhan


    My decision variable is in tuple format

    dvar float+ X[PU];

     

    execute{
    writeln(f1(X[<<1, 0.975> ,1>]),"f1");
     }

    Error:
    Impossible to load model.   
    Scripting parser error: missing expression.   

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 28.  Re: non linear constraint

    Posted Fri March 08, 2019 04:44 AM

    Originally posted by: sandeepsinghchauhan


    My breakpoint1 range is from 7 to 13. then how i am getting value at f(0.975)? Please help me to understand this

     

    tuple breakpoint // y=f(x)
     {
      key float x;
      float y;
     }
     
     float firstSlope=0;
     float lastSlope=0;
    sorted { breakpoint } breakpoints1={<i,.06*pow(i,2)-12.07*pow(i,1)+592.76> | i in 7..13};
    float slopesBeforeBreakpoint[b in breakpoints1]=(b.x==first(breakpoints1).x)?firstSlope:(b.y-prev(breakpoints1,b).y)/(b.x-prev(breakpoints1,b).x);
    pwlFunction f1=piecewise(b in breakpoints1){ slopesBeforeBreakpoint[b]->b.x; lastSlope } (first(breakpoints1).x, first(breakpoints1).y);

     

    //How to find f1 value?

    execute{
    writeln(f1(0.975), " F11");
    }

     

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 29.  Re: non linear constraint

    Posted Fri March 08, 2019 04:44 AM

    Hi,

    you mix OPL and OPL scripting

     tuple breakpoint // y=f(x)
     {
      key float x;
      float y;
     }
     
     float firstSlope=0;
     float lastSlope=0;
    sorted { breakpoint } breakpoints1={<i,.06*pow(i,2)-12.07*pow(i,1)+592.76> | i in 7..13};
    float slopesBeforeBreakpoint[b in breakpoints1]=(b.x==first(breakpoints1).x)?firstSlope:(b.y-prev(breakpoints1,b).y)/(b.x-prev(breakpoints1,b).x);
    pwlFunction f1=piecewise(b in breakpoints1){ slopesBeforeBreakpoint[b]->b.x; lastSlope } (first(breakpoints1).x, first(breakpoints1).y);

     
    tuple t
    {
    float x;
    float y;
    }

    {t} PU={<1, 0.975>};


    dvar float+ X[PU];

    subject to
    {

    }

     

    execute{
     writeln(f1(X[PU.find(1, 0.975)]),"f1");
      }

    works fine

     

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 30.  Re: non linear constraint

    Posted Fri March 08, 2019 04:47 AM

    Originally posted by: sandeepsinghchauhan


    Thank you so much alex.

    My breakpoint1 range is from 7 to 13. then how i am getting value of f(0.975)? Please help me to understand this


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 31.  Re: non linear constraint

    Posted Fri March 08, 2019 05:22 AM

    Originally posted by: sandeepsinghchauhan


    when X[PU.find(1, 0.975)]  = 0,

    then f( X[PU.find(1, 0.975)] ) value should also be zero. but it is giving me first breakpoint value.


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 32.  Re: non linear constraint

    Posted Fri March 08, 2019 05:32 AM

    yes that s fine.

     tuple breakpoint // y=f(x)
     {
      key float x;
      float y;
     }
     
     float firstSlope=0;
     float lastSlope=0;
    sorted { breakpoint } breakpoints1={<i,.06*pow(i,2)-12.07*pow(i,1)+592.76> | i in 7..13};
    float slopesBeforeBreakpoint[b in breakpoints1]=(b.x==first(breakpoints1).x)?firstSlope:(b.y-prev(breakpoints1,b).y)/(b.x-prev(breakpoints1,b).x);
    pwlFunction f1=piecewise(b in breakpoints1){ slopesBeforeBreakpoint[b]->b.x; lastSlope } (first(breakpoints1).x, first(breakpoints1).y);

     
    tuple t
    {
    float x;
    float y;
    }

    {t} PU={<1, 0.975>};


    dvar float+ X[PU];

    subject to
    {

    }

     

    execute{
    writeln("x=",X[PU.find(1, 0.975)]);
    writeln("f1(0)=",f1(0));
    writeln("f1(3)=",f1(3));
    writeln("f1(5)=",f1(5));
     writeln(f1(X[PU.find(1, 0.975)]),"f1");
      }

     

    gives

    x=0
    f1(0)=511.21
    f1(3)=511.21
    f1(5)=511.21
    511.21f1

    which is normal since the first slope is 0 so all the values before the first breakpoint are 511.21

     

    if you change the slope to 1

    float firstSlope=1;

    then you ll get

     

    x=0
    f1(0)=504.21
    f1(3)=507.21
    f1(5)=509.21
    504.21f1

     

    regards

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 33.  Re: non linear constraint

    Posted Fri March 08, 2019 05:41 AM

    Originally posted by: sandeepsinghchauhan


    If X value will not in range of breakpoints then how will I set f(X) value to zero.

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 34.  Re: non linear constraint



  • 35.  Re: non linear constraint

    Posted Fri March 08, 2019 06:00 AM

    Originally posted by: sandeepsinghchauhan


    What will be my equation for discontinuous piecewise linear function?

    tuple breakpoint // y=f(x)
     {
      key float x;
      float y;
     }
     
     float firstSlope=0;
     float lastSlope=0;
    sorted { breakpoint } breakpoints1={<i,.06*pow(i,2)-12.07*pow(i,1)+592.76> | i in 7..13};
    float slopesBeforeBreakpoint[b in breakpoints1]=(b.x==first(breakpoints1).x)?firstSlope:(b.y-prev(breakpoints1,b).y)/(b.x-prev(breakpoints1,b).x);
    pwlFunction f1=piecewise(b in breakpoints1){ slopesBeforeBreakpoint[b]->b.x; lastSlope } (first(breakpoints1).x, first(breakpoints1).y);


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 36.  Re: non linear constraint

    Posted Fri March 08, 2019 11:16 AM

    Hi,

    let me show you how you could get 0 at the beginning

    tuple breakpoint // y=f(x)
     {
      float x;
      float y;
     }
     
     float firstSlope=0;
     float lastSlope=0;
    { breakpoint } breakpoints1=
    {<7,0>} union
    {<7,.06*pow(7,2)-12.07*pow(7,1)+592.76>} union
    {<i,.06*pow(i,2)-12.07*pow(i,1)+592.76> | i in 8..13}

    ;
    float slopesBeforeBreakpoint[b in breakpoints1]=(b.x==
    first(breakpoints1).x)?firstSlope:(b.y-prev(breakpoints1,b).y)/(b.x-prev(breakpoints1,b).x);

    pwlFunction f1=piecewise(b in breakpoints1)
    { slopesBeforeBreakpoint[b]->b.x; lastSlope }


    (first(breakpoints1).x, first(breakpoints1).y);

    range r=0..15;
    execute
    {
    for(var i in r)writeln(i," ==> ",f1(i));
    }

    gives

     

    0 ==> 0
    1 ==> 0
    2 ==> 0
    3 ==> 0
    4 ==> 0
    5 ==> 0
    6 ==> 0
    7 ==> 0
    8 ==> -11.17
    9 ==> -22.22
    10 ==> -33.15
    11 ==> -43.96
    12 ==> -54.65
    13 ==> -65.22
    14 ==> -65.22
    15 ==> -65.22

     

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 37.  Re: non linear constraint

    Posted Tue March 12, 2019 04:52 AM

    Originally posted by: sandeepsinghchauhan


    tuple breakpoint // y=f(x)
     {
      float x;
      float y;
     }
      sorted { breakpoint } breakpoints={<i,.exp(2*sin(i))> | i in 70..135};

     

    I am getting error. Please help me to resolve this issue.


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 38.  Re: non linear constraint

    Posted Tue March 12, 2019 05:00 AM

    Hi,

    sin was removed from the OPL language some years ago.

    You could compute the values you need in scripting

    regards

    PS: Do not hesitate to log a wish in Aha https://ibmanalytics.ideas.aha.io/?project=CPLEX


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 39.  Re: non linear constraint

    Posted Tue March 12, 2019 05:03 AM

    Originally posted by: sandeepsinghchauhan


    How will I calculate this in scripting?

    exp(2*sin(i))> | i in 70..135;


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 40.  Re: non linear constraint

    Posted Tue March 12, 2019 07:31 AM

    Hi,

    you could write

    //exp(2*sin(i))> | i in 70..135;

    range r=70..135;
    float sin[r];

    execute
    {
    for(i in r) sin[i]=Math.sin(i*Math.PI/180);
    }

    float res[i in r]=exp(2*sin[i]);

    execute
    {
    res;
    }

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer