Decision Optimization

 View Only
  • 1.  Piecewise function for solving nonlinear objective function

    Posted Mon June 22, 2020 10:07 AM
    Hi,
    Can anybody help me how to solve nonlinear objective function using piecewise linear function? In my objective function, the setup cost is nonlinear function where there are division of decision variable; sum(i in depot, j in customer)(D[j]-R[j])*y[i][j]/Q[i]. How to use pwl for the variable Q[i]?. Thank you.


    {string} depot=...;
    {string} customer=...;
    {string} vehicle=...;
    {string} node=...;
    int N=card(customer);
    int pr=...;
    float dist[node][node]=...;
    float fcost[depot]=...;
    float D[customer]=...;
    float R[customer]=...;
    float VC[vehicle]=...;
    float VD[depot]=...;
    dvar boolean x[node][node][vehicle];
    dvar boolean z[depot];
    dvar boolean y[depot][customer];
    dvar float+ u[customer][vehicle];
    dvar float+ Q[depot];

    dexpr float facility=sum(i in node, j in node:j!=i, k in vehicle)x[i][j][k]*dist[i][j];
    dexpr float travel=sum(i in depot)fcost[i]*z[i];
    dexpr float setup=17*sum(i in depot, j in customer)(D[j]-R[j])*y[i][j]/Q[i];
    dexpr float holdinginventory=(1/pr)*sum(i in depot)(Q[i]*pr - Q[i]*sum(j in customer)(D[j]+R[j])*y[i][j]);
    dexpr float cost=travel + facility + setup + holdinginventory;


    minimize cost;
    subject to {
    forall(j in customer) sum(i in node, k in vehicle)x[i][j][k]==1;
    forall(k in vehicle) sum(i in depot, j in customer)x[i][j][k]<=1;
    forall(k in vehicle) sum(i in node, j in customer:j!=i)D[j]*x[i][j][k]<=VC[k];
    forall(i in node, k in vehicle) (sum(j in node)x[i][j][k])- (sum(j in node)x[j][i][k])==0;
    forall(i in depot) sum(j in customer)D[j]*y[i][j]<=VD[i]*z[i];
    forall(j in customer) sum(i in depot)y[i][j]==1;
    forall(l in customer, j in customer, k in vehicle) u[l][k]-u[j][k]+N*x[l][j][k]<= N-1;
    forall(i in depot, j in customer, k in vehicle) (sum(h in node)(x[i][h][k]+x[h][j][k]))-y[i][j]<=1;
    }

    ------------------------------
    Farahanim Misni
    ------------------------------

    #DecisionOptimization


  • 2.  RE: Piecewise function for solving nonlinear objective function

    Posted Mon June 22, 2020 11:12 AM
    Hi,

    you could use invQ the inverse of Q
    And to compute invQ through a piecewise linear with breeakpoints see

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

    in

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

    regards

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



  • 3.  RE: Piecewise function for solving nonlinear objective function

    Posted Tue June 23, 2020 10:25 AM
    Hi Alex,
    I just saw the link that you gave to me. May I know for that example, how can we set up the values of first and last slope and how to set the value of piecewise <0,0>, <1,1>, <2,4> in that example. For example one of my objective function is sum(i in depot, j in customer)(D[j]-R[j])*y[i][j]/Q[i]. You said used invQ instead of Q right? Can you give me some example which is quite similar with my objective function? Thank you in advanced.

    float firstSlope=0.5;
    float lastSlope=2;

    tuple breakpoint
    {
    key float n;
    float p;
    }
    sorted{breakpoint}breakpoints={<0,0>,<1,1>,<2,4>};
    float SlopesBeforeBreakpoint[b in breakpoints]=(b.n==first(breakpoints).n)?firstSlope:(b.p-prev(breakpoints,b).p)/(b.n-prev(breakpoints,b).n);
    pwlFunction f=piecewise (b in breakpoints){SlopesBeforeBreakpoint[b]->b.n;lastSlope}(first(breakpoints).n, first(breakpoints).p);
    assert forall(b in breakpoints)f(b.n)==b.p;

    ------------------------------
    Farahanim Misni
    ------------------------------



  • 4.  RE: Piecewise function for solving nonlinear objective function

    Posted Tue June 23, 2020 12:39 PM
    Hi,
    let me adapt my interpolation code from https://community.ibm.com/community/user/datascience/communities/community-home/digestviewer/viewthread?GroupId=5557&MID=93099&CommunityKey=ab7de0fd-6f43-47a9-8261-33578a231bb7&tab=digestviewer for 1/x:

    int sampleSize=10000;
    float s=1;
    float e=10;
    
    float x[i in 0..sampleSize]=s+(e-s)*i/sampleSize;
    
    int nbSegments=100;
    
    float x2[i in 0..nbSegments]=(s)+(e-s)*i/nbSegments;
    float y2[i in 0..nbSegments]=1/x2[i];  // y=f(x)
    
    float firstSlope=0;
     float lastSlope=0;
     
     tuple breakpoint // y=f(x)
     {
      key float x;
      float y;
     }
     
     sorted { breakpoint } breakpoints={<x2[i],y2[i]> | i in 0..nbSegments};
     
     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;
     
     float maxError=max (i in 0..sampleSize) abs(1/x[i]-f(x[i]));
     float averageError=1/(sampleSize+1)*sum (i in 0..sampleSize) abs(1/x[i]-f(x[i]));
     
     execute
    {
    writeln("maxError = ",maxError);
    writeln("averageError = ",averageError);
    } 
    
    execute
    {
      writeln("inverse of 2 : ",f(2));
    }​


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