Decision Optimization

Decision Optimization

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

 View Only
Expand all | Collapse all

Non linear constraints

  • 1.  Non linear constraints

    Posted Tue June 02, 2020 03:53 AM
    Good Morning

    I am tryng to implement the following nonlinear constraint:

       forall(i in N){   forall(i in N){      SetofConst8: Krw[i] == Krwep*pow(Swn[i],Nw);     SetofConst9: Kro[i] == Kroep*pow((1-Swn[i]),No);   }

    the error message is : TypeFunction pow(dvar float+,float) not available in context CPLEX.

    Attached the complete .mod file

    Thank you for your attention and help

    Best regards,

    Larry

    /*********************************************/********************************************* * OPL 12.10.0.0 Model * Author: Lprentt * Creation Date: Jun 1, 2020 at 8:39:14 AM *********************************************/ // data declaration  int n = ...; // number of Layers float re = ...; // Radio del yacimiento o del area de drenaje float rw = ...; // Radio del pozo  range N = 1..n;   // variable declaration dvar float+ Qw[N];  // Tasas de agua por capadvar float+ Qo[N];  // Tasas de petroleo por capa
    dvar float+ Swn[N]; // Saturacion de agua por capa
    dvar float  Krw[N]; // Tasas de petroleo por capadvar float  Kro[N]; // Tasas de petroleo por capa
    float K[N]= ...;    // Permeabilidad absoluta por capafloat h[N]= ...;    // Espesor neto petrolifero por capafloat Uw[N]= ...;   // Viscosidad del agua por capafloat Uo[N]= ...;   // Viscosidad del petroleo por capafloat S[N]= ...;    // Skin o factor de daño por capafloat Pyac[N]= ...; // Presion promedio de yacimiento por capafloat Pwf[N]= ...;  // Presion de fondo fluyendo por capa
    float Qws= ...;  // Tasa Total de agua en superficiefloat Qos= ...;  // Tasa Total de Petroleo en superficie
    float Krwep= ...;  //float Kroep= ...;  //
    float Nw= ...;  //float No= ...;  //
    float Bwi= ...;  // Factor Volumetrico del aguafloat Boi= ...;  // Factor Volumetrico del petroleo
    float Fw;
    execute {    Fw = (Qws/Qos)/((Qws/Qos)+Boi/Bwi);}
     // objective function maximize Fw*sum(i in N)Qo[i] - (1-Fw)*sum(i in N)Qw[i]; // constraints  subject to {    forall(i in N){     SetofConst1:  Qo[i] == 0.00708*K[i]*Kro[i]*h[i]*(Pyac[i]-Pwf[i])/(Uo[i]*(ln(re/rw)-0.75+S[i]));  //   }         forall(i in N){     SetofConst2:  Qw[i] == 0.00708*K[i]*Krw[i]*h[i]*(Pyac[i]-Pwf[i])/(Uw[i]*(ln(re/rw)-0.75+S[i]));  //   }      forall(i in N){     SetofConst3: 0.0 <=  Swn[i] <= 1;     SetofConst4: 0.001 <=  Krw[i] <= 0.1;     SetofConst5: 0.001 <=  Kro[i] <= 0.7;   }
       SetofConst6:  sum(i in N)Qo[i] == Qos*Boi;   SetofConst7:  sum(i in N)Qw[i] == Qws*Bwi;   
       forall(i in N){// here the error      SetofConst8: Krw[i] == Krwep*pow(Swn[i],Nw);     SetofConst9: Kro[i] == Kroep*pow((1-Swn[i]),No);   } }

    ------------------------------
    Larry Prentt
    ------------------------------

    #DecisionOptimization


  • 2.  RE: Non linear constraints

    Posted Tue June 02, 2020 03:56 AM
    With the CPLEX solver engine, the only exponents allowed for decision variables are 1 and 2. If Nw is anything else then this is not supported and you will have to reformulate your objective (for example use a piecewise linear). If Nw happens to be 2 then try to write this explicitly as quadratic term.

    ------------------------------
    Daniel Junglas
    ------------------------------



  • 3.  RE: Non linear constraints

    Posted Tue June 02, 2020 08:50 AM
    Hi,

    what you could try is use CPO and then you would need to move to decomal decision variables.

    See CPO and decimal decision variables in https://www.linkedin.com/pulse/making-decision-optimization-simple-alex-fleischer/


    /*
    
     In Constraint Programming decision variables have to be integer but sometimes we need more than this.
    
    Let's see how we can handle through decision expressions.
    
    Suppose in the bus example we want to book not only whole buses but also percentage of buses.
    
    Then we can write
    
    */
    
        using CP;
    
        int nbKids=310;
        float costBus40=500;
        float costBus30=400;
        int scale=100;
    
         
        dvar int+ scalenbBus40;
        dvar int+ scalenbBus30;
    
        dexpr float nbBus40=scalenbBus40/scale;
        dexpr float nbBus30=scalenbBus30/scale;
         
        minimize
         costBus40*nbBus40  +nbBus30*costBus30;
         
        subject to
        {
         40*nbBus40+nbBus30*30>=nbKids;
        }
    
        execute
        {
          writeln("nbBus40 = ",nbBus40);
          writeln("nbBus30 = ",nbBus30);
        }
    
    /*
    which gives
    
     
    
        nbBus40 = 7.75
        nbBus30 = 0
    
    
    
     */​


    ------------------------------
    ALEX FLEISCHER
    ------------------------------



  • 4.  RE: Non linear constraints

    Posted Tue June 02, 2020 11:45 AM
    Daniel

    Thank you very much.  Nw and No have values between 1 and 5 as float

    I will be implementing the piecewise linear

    Best regards,

    Larry

    Este mensaje y sus archivos adjuntos van dirigidos exclusivamente a su destinatario pudiendo contener información confidencial. No está permitida su reproducción o distribución sin la autorización expresa de la Universidad Sergio Arboleda, sedes o seccionales. Si usted no es el destinatario final por favor elimínelo e informemos por esta vía. El presente correo electrónico solo refleja la opinión de su remitente y no representa necesariamente la opinión oficial de la Universidad Sergio Arboleda, sedes o seccionales o de sus Directivos. Puede usted ejercitar los derechos de consulta, tratamiento, actualización, corrección o supresión sobre sus datos, mediante escrito dirigido a Universidad Sergio Arboleda, a través de correo electrónico en la dirección habeas.data@usa.edu.co, indicando en la línea de asunto el derecho que desea ejercitar, o mediante correo ordinario remitido a la Calle 74 No. 14-14 Bogotá D.C., Dirección Jurídica y de Contratación.






  • 5.  RE: Non linear constraints

    Posted Tue June 02, 2020 11:55 AM
    Hi,

    then if you want to use piecewise in

    https://www.linkedin.com/pulse/how-opl-alex-fleischer/

    you have How to describe a piecewise linear function with breakpoints instead of slopes ?

    regards

    ------------------------------
    ALEX FLEISCHER
    ------------------------------



  • 6.  RE: Non linear constraints

    Posted Tue June 02, 2020 12:27 PM
    Hi Alex,

    Thank you.

    Please send me a link or reference to work with breakpoints,

    Best regards

    Larry

    Este mensaje y sus archivos adjuntos van dirigidos exclusivamente a su destinatario pudiendo contener información confidencial. No está permitida su reproducción o distribución sin la autorización expresa de la Universidad Sergio Arboleda, sedes o seccionales. Si usted no es el destinatario final por favor elimínelo e informemos por esta vía. El presente correo electrónico solo refleja la opinión de su remitente y no representa necesariamente la opinión oficial de la Universidad Sergio Arboleda, sedes o seccionales o de sus Directivos. Puede usted ejercitar los derechos de consulta, tratamiento, actualización, corrección o supresión sobre sus datos, mediante escrito dirigido a Universidad Sergio Arboleda, a través de correo electrónico en la dirección habeas.data@usa.edu.co, indicando en la línea de asunto el derecho que desea ejercitar, o mediante correo ordinario remitido a la Calle 74 No. 14-14 Bogotá D.C., Dirección Jurídica y de Contratación.






  • 7.  RE: Non linear constraints

    Posted Wed June 03, 2020 02:43 AM
    Hi

    example with points:

    /* 
     
     OPL supports piecewise linear functions, which are important in many applications. 
     In most cases, you specify piecewise linear functions by giving a set of slopes, 
     a set of breakpoints at which the slopes change, and the value of the functions at a given point. 
     The parameters are n breakpoints and n+1 slopes, and s -> b means that the slope s goes up to breakpoint b.
     
     But some find this a bit hard and prefer to enter sets of (x,y), 
     so let me help them through an example: 
     
    */
    
        //pwl1: y = x 0.5 (0, 0) (1, 1) (2, 4) 2.0  
    
    
         float firstSlope=0.5;
         float lastSlope=2.0;
         
         tuple breakpoint // y=f(x)
         {
          key float x;
          float y;
         }
         
         sorted { breakpoint } breakpoints={<0,0>,<1,1>,<2,4>};
         
         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);
         
         
        assert forall(b in breakpoints) f(b.x)==b.y;
    ​

    regards

    ------------------------------
    ALEX FLEISCHER
    ------------------------------



  • 8.  RE: Non linear constraints

    Posted Thu June 04, 2020 11:07 PM
    Hi Alex,

    I used the your code example with the function x^4-log(x) when you reply to sandeepsinghchauhan in July-2017 as follows with my function:

    ////////
    float firstSlope=0;
     float lastSlope=0;
     
     tuple breakpoint // y=f(x)
     {
      key float x;
      float y;
     }
     
     sorted { breakpoint } breakpoints={<i/100,Krwep*pow(i/100,Nw)> | 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;
    /////////

    Is it possible to implement x as vector into the piecewise  function? 

    I have four layers and I am calculating production from layers and the idea is to obtain differents values of x per layer.

    Thank you very much.

    Regards,

    Larry

      

    Este mensaje y sus archivos adjuntos van dirigidos exclusivamente a su destinatario pudiendo contener información confidencial. No está permitida su reproducción o distribución sin la autorización expresa de la Universidad Sergio Arboleda, sedes o seccionales. Si usted no es el destinatario final por favor elimínelo e informemos por esta vía. El presente correo electrónico solo refleja la opinión de su remitente y no representa necesariamente la opinión oficial de la Universidad Sergio Arboleda, sedes o seccionales o de sus Directivos. Puede usted ejercitar los derechos de consulta, tratamiento, actualización, corrección o supresión sobre sus datos, mediante escrito dirigido a Universidad Sergio Arboleda, a través de correo electrónico en la dirección habeas.data@usa.edu.co, indicando en la línea de asunto el derecho que desea ejercitar, o mediante correo ordinario remitido a la Calle 74 No. 14-14 Bogotá D.C., Dirección Jurídica y de Contratación.






  • 9.  RE: Non linear constraints

    Posted Fri June 05, 2020 04:04 AM
    Hi,

    you can use arrays of piecewise.

    Let me change a bit the zoo piecewise from https://www.linkedin.com/pulse/making-decision-optimization-simple-alex-fleischer/

        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;
        
        range pricingRule=1..2;
    
        pwlFunction pricePerQuantity[pricingRule]=[piecewise{1->4;0.8},piecewise{1->4;0.9}];
    
        assert forall(r in pricingRule,k in 1..4 ) pricePerQuantity[r](k)==k;
        assert forall(k in 5..10) as:abs(pricePerQuantity[1](k)-4-(k-4)*0.8)<=0.00001;
        assert forall(k in 5..10) as2:abs(pricePerQuantity[2](k)-4-(k-4)*0.9)<=0.00001;
        
        int rule=1;
         
        minimize
         costBus40*pricePerQuantity[rule](nbBus40)  +pricePerQuantity[rule](nbBus30)*costBus30;
         
        subject to
        {
         40*nbBus40+nbBus30*30>=nbKids;
        }​


    ------------------------------
    ALEX FLEISCHER
    ------------------------------



  • 10.  RE: Non linear constraints

    Posted Tue June 09, 2020 04:06 PM
    Hi Alex,

    Thank you very much for this option.

    other question: is it possible to implement arrays of piecewise using the ternary operator ?

    regards,

    Larry

    Este mensaje y sus archivos adjuntos van dirigidos exclusivamente a su destinatario pudiendo contener información confidencial. No está permitida su reproducción o distribución sin la autorización expresa de la Universidad Sergio Arboleda, sedes o seccionales. Si usted no es el destinatario final por favor elimínelo e informemos por esta vía. El presente correo electrónico solo refleja la opinión de su remitente y no representa necesariamente la opinión oficial de la Universidad Sergio Arboleda, sedes o seccionales o de sus Directivos. Puede usted ejercitar los derechos de consulta, tratamiento, actualización, corrección o supresión sobre sus datos, mediante escrito dirigido a Universidad Sergio Arboleda, a través de correo electrónico en la dirección habeas.data@usa.edu.co, indicando en la línea de asunto el derecho que desea ejercitar, o mediante correo ordinario remitido a la Calle 74 No. 14-14 Bogotá D.C., Dirección Jurídica y de Contratación.






  • 11.  RE: Non linear constraints

    Posted Tue June 09, 2020 05:21 PM
    Yes:

    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;
        
        range pricingRule=1..2;
    
        pwlFunction pricePerQuantity[i in pricingRule]=
        piecewise{1->4;(i==1)?0.8:0.9};
        
    
        assert forall(r in pricingRule,k in 1..4 ) pricePerQuantity[r](k)==k;
        assert forall(k in 5..10) as:abs(pricePerQuantity[1](k)-4-(k-4)*0.8)<=0.00001;
        assert forall(k in 5..10) as2:abs(pricePerQuantity[2](k)-4-(k-4)*0.9)<=0.00001;
        
        int rule=1;
         
        minimize
         costBus40*pricePerQuantity[rule](nbBus40)  +pricePerQuantity[rule](nbBus30)*costBus30;
         
        subject to
        {
         40*nbBus40+nbBus30*30>=nbKids;
       }     
    
    ​


    ------------------------------
    ALEX FLEISCHER
    ------------------------------



  • 12.  RE: Non linear constraints

    Posted Sat June 13, 2020 11:20 PM
    Alex thank you.

    I defined this array:

    dvar float+ x[1..n];

    // Kro Piecewise
    pwlFunction F[i in 1..n] = piecewise{ T[i]->BP1[i]; U[i]->BP2[i]; V[i]->BP3[i]; W[i] };

    // Krw Piecewise
    pwlFunction G[i in 1..n] = piecewise{ TW[i]->BP1[i]; UW[i]->BP2[i]; VW[i]->BP3[i]; WW[i] };

    with the following constraint:

    subject to {
    ..........
    ...........
       forall(i in 1..n){
        SetofConst7: Krw[i] <= G(x[i]);
        SetofConst8: Kro[i] <= F(x[i]);
       }  
     }

    I obtained the following error in CPLEX:

    Cannot use type pwlFunction[range] for userFunction.

    How Can I handle this ?

    Regards,

    Larry

    Este mensaje y sus archivos adjuntos van dirigidos exclusivamente a su destinatario pudiendo contener información confidencial. No está permitida su reproducción o distribución sin la autorización expresa de la Universidad Sergio Arboleda, sedes o seccionales. Si usted no es el destinatario final por favor elimínelo e informemos por esta vía. El presente correo electrónico solo refleja la opinión de su remitente y no representa necesariamente la opinión oficial de la Universidad Sergio Arboleda, sedes o seccionales o de sus Directivos. Puede usted ejercitar los derechos de consulta, tratamiento, actualización, corrección o supresión sobre sus datos, mediante escrito dirigido a Universidad Sergio Arboleda, a través de correo electrónico en la dirección habeas.data@usa.edu.co, indicando en la línea de asunto el derecho que desea ejercitar, o mediante correo ordinario remitido a la Calle 74 No. 14-14 Bogotá D.C., Dirección Jurídica y de Contratación.






  • 13.  RE: Non linear constraints

    Posted Sun June 14, 2020 12:37 PM
    Hi,

    you forgot that F and G are not functions but arrays of functions.

    int n=4;
    
    int T[i in 1..n]=i;
    int TW[i in 1..n]=i;
    int BP1[i in 1..n]=i;
    int U[i in 1..n]=i+1;
    int UW[i in 1..n]=i+1;
    int BP2[i in 1..n]=i+2;
    int V[i in 1..n]=i+3;
    int VW[i in 1..n]=i+3;
    int BP3[i in 1..n]=i+4;
    int W[i in 1..n]=i+5;
    int WW[i in 1..n]=i+5;
    
    dvar float+ x[1..n];
    dvar float+ Krw[1..n];
    dvar float+ Kro[1..n];
    
    // Kro Piecewise
    pwlFunction F[i in 1..n] = piecewise{ T[i]->BP1[i]; U[i]->BP2[i]; V[i]->BP3[i]; W[i] };
    
    // Krw Piecewise
    pwlFunction G[i in 1..n] = piecewise{ TW[i]->BP1[i]; UW[i]->BP2[i]; VW[i]->BP3[i]; WW[i] };
    
    //with the following constraint:
    
    subject to {
    
       forall(i in 1..n){
        SetofConst7: Krw[i] <= G[i](x[i]);
        SetofConst8: Kro[i] <= F[i](x[i]);
       }  
     }​

    works fine

    regards

    ------------------------------
    ALEX FLEISCHER
    ------------------------------



  • 14.  RE: Non linear constraints

    Posted Sun June 14, 2020 02:44 PM
    HI Alex,

    Thanks a lot !!!
    it is working very fine.

    Regards,

    Larry

    Este mensaje y sus archivos adjuntos van dirigidos exclusivamente a su destinatario pudiendo contener información confidencial. No está permitida su reproducción o distribución sin la autorización expresa de la Universidad Sergio Arboleda, sedes o seccionales. Si usted no es el destinatario final por favor elimínelo e informemos por esta vía. El presente correo electrónico solo refleja la opinión de su remitente y no representa necesariamente la opinión oficial de la Universidad Sergio Arboleda, sedes o seccionales o de sus Directivos. Puede usted ejercitar los derechos de consulta, tratamiento, actualización, corrección o supresión sobre sus datos, mediante escrito dirigido a Universidad Sergio Arboleda, a través de correo electrónico en la dirección habeas.data@usa.edu.co, indicando en la línea de asunto el derecho que desea ejercitar, o mediante correo ordinario remitido a la Calle 74 No. 14-14 Bogotá D.C., Dirección Jurídica y de Contratación.