Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
Expand all | Collapse all

On going value in cplex

ALEX FLEISCHER

ALEX FLEISCHER02/07/18 03:46 AM

  • 1.  On going value in cplex

    Posted 02/07/18 03:07 AM

    Originally posted by: Bueffel


    Hi,

    I want to schedule a refuel plan for cars. There is a fuel level for each car. It starts at 100% in the morning and depending on their distances I want the fuel level to go down and if the car ist refilled I want it to go up.

    I want to maximize a function that depends on the fuel level of the cars with subject to some constraints like the fuel level can`t go below zero and so on.

    But I don`t know how to start the fuel level.

    For example if I have two cars i would start the fuel in my .dat like:

    fuel[car1][1] = [1];
    fuel[car2][1] = [1];

    "1" is the number of the time period.

     

    In my .mod I need to calculate the fuel level for both cars for every time period:

    forall(i in car, t in Time)

          if(Pres[i][t]==1){                                                                   //car is present and can be refilled

                 fuel[i][t]= fuel[i][t-1] + x[i][t] * fill rate;                           // new fuel level = old fuel level + decision variable * fill rate

         } else {                                                                                   //car is not present and fuel is used

                  fuel[i][t]= fuel[i][t-1] - dis[i][t] * consumption;           //new fuel level = old fuel level - distance * consumption

        }

     

    x[i][t] is the decision variable. If two cars are present only one should be able to refill, depending on my maximizing function.

     

    But I don`t know where to put this code since in the constraints only relational operations are allowed and before I`m getting mistakes all of the time.

     

    Thanks for your help.

    Best regards

    Thorben

     


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 2.  Re: On going value in cplex

    Posted 02/07/18 03:46 AM

    Hi,

    instead of

    forall(i in car, t in Time)

          if(Pres[i][t]==1){                                                                   //car is present and can be refilled

                 fuel[i][t]= fuel[i][t-1] + x[i][t] * fill rate;

    you could try

    forall(i in car, t in Time)

          (Pres[i][t]==1) =>                                                                  //car is present and can be refilled

                ( fuel[i][t]== fuel[i][t-1] + x[i][t] * fill rate);

    regards


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 3.  Re: On going value in cplex

    Posted 02/07/18 07:17 AM

    Originally posted by: Bueffel


    Thanks for your answer.

     

    Now I´m using "dexpr" before my optimization problem to calculate the fuel level. It looks like:

    dexpr float fuel[m in t, n in c] = sum(m in t, n in c) soc[n][m-1] + x[n][m]*charge - dis[n][m] * con;

    Depending on the previous time period it shall calculate the new level. But I´m getting the error that the element "fuel" is not availabe. Why does this error appear?

    And how and where can I initialize the start fuel level of 100% (=1). In the .dat like

    fuel[1][0]=1;               //car 1 fuel level at time 0 is 1

    fuel[2][0]=1;               //car 2 fuel level at time 0 is 1

    ?

     

    Best regards

     

     


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 4.  Re: On going value in cplex

    Posted 02/07/18 08:46 AM

    Can you show the full model or at least everything before the constraint section? Otherwise it is hard to tell why 'fuel' is not defined.

    For your other question, did you see the section about logical constraints in the manual? These should allow you to implement the conditions you mentioned in your first post (Alex already mentioned the => constraint in his post).

    Finally, you cannot initialize decision variables in a .dat file. Instead you should create a data element that specifies the initial fuel level:

    float initFuel[Cars] = ...;

    initialize that in the .dat file like

    initFuel = [ 1, 1 ] // Initial fuel level for each car

    and then add a constraint in the .mod file

    forall (c in Cars) fuel[c][0] == initFuel[c];

     


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 5.  Re: On going value in cplex

    Posted 02/08/18 02:59 AM

    Originally posted by: Bueffel


    Thank you Daniel, I´ll give it a try.

    Here is the model before the constraints. In every help I´ve seen the variables in dexpr are not defined before :/

     

    int duration = ...;                        //time periods
    range d= 1..Dur;                      
    int vehicle = ...;                          // number of vehicles
    range c= 1..veh;
    float consumption=...;              //consumption per km
    float fillrate=...;                          //refill rate
    float Pres[c][t] = ...;                   //vehicle is present at filling station (1=y, 0=n)

    float distance[c][t]= ...;            //driven distance if car is absent

    float minfuellevel=0.2;       
    float maxfuellevel=1.0;

    dvar boolean x[c][t];                 //decision variable if car is filled

     

    dexpr float soc[m in t, n in c] = sum(m in t, n in c) soc[n][m-1] + x[n][m]*fillrate - distance[n][m] * consumption;
       
    maximize ...

    s.t...


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 6.  Re: On going value in cplex

    Posted 02/08/18 09:58 AM

    I am no expert in this but the following strikes me as odd in the definition of soc:

    1. The indices on the left and the right side of the definition are misaligned. On the left you have "m in t, n in c" which basically is soc[t][c], on the right you have soc[c][t]. Is that intended?
    2. soc[n][m-1] is probably not defined for the smallest value m?

    #DecisionOptimization
    #OPLusingCPOptimizer


  • 7.  Re: On going value in cplex

    Posted 02/09/18 07:46 AM

    Originally posted by: Bueffel


    First point was a mistake. It is "working" now,but i still don`t get used to the fuel level. I defined it like:

    fuel=[[1 0 0 0 0 0 0 0 ]
              [1 0 0 0 0 0 0 0 ]
              ];

    So it is defined as 1 in the first period and the model works with it, but I can`t change it`s value because I don`t know how. I couldn`t use "NULL" to keep it empy.  The fuel level is not a decision variable so i can`t use "dexpr", right? It`s not one of the subject to constraints, so i can`t calculate it there.

    I just need a way to calculate fuel[1][2], fuel[2][2], ... ,fuel[2][8] for the periods after the first one.I know the formula but I don`t know where to do it. Like: Fuel[m][n+1]=fuel[m][n]-consumption[m][n]

    Do you have any further hints for me?

     

    And again thanks a lot for your support!


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 8.  Re: On going value in cplex

    Posted 02/09/18 09:16 AM

    You cannot specify fuel in the .dat file and then overwrite it later. This data is assumed to be constant. So your problem is that fuel is partly constant and partly not, and also that it is defined recursively.

    Here is something that defines fuel as a dexpr which is equal to the initialization in the first period and to some expression in x in subsequent periods. Can you define fuel in that way?

    range Periods = 1..5;
    int firstPeriod = first({p | p in Periods});
    range Cars = 1..2;

    float fuelInit[Cars] = [ 1, 1.5 ];

    dvar float+ x[Periods][Cars];

    dexpr float fuel[c in Cars][p in Periods] =
     (p == firstPeriod)
      ? fuelInit[c] // In the first period the fuel is constant as defined by the data
      : (x[p][c]);    // In subsequent periods the fuel is an expression in x

    Another thing to try that might be a lot simpler is this:

    • define fuel as 'dvar float'
    • State the definition 'fuel[m+1] == fuel[m] - ...' as constraint (here you can use fuel on the left and the right side)
    • Add an explicitl constraint 'fuel[0] = fuelInit' to fix the fuel for the first period

    #DecisionOptimization
    #OPLusingCPOptimizer


  • 9.  Re: On going value in cplex

    Posted 02/22/18 03:24 AM

    Originally posted by: Bueffel


    Fuel as a dvar works fine, also the initialization.

    Now there is another problem with the bounds. In my subject to I defined the bounds like

        forall (i in c, j in d)                                    
           fuel[i][d] >= minFUEL;   

        forall (i in c, j in d)                        
           fuel[i][d] <= maxFUEL;

     

    I would have expected that an error occurs if these equations are not fulfilled, but it`s only a relaxation. Is there a way to define it as a bound?

      "  Bounds                    
        forall (i in c, j in d)                                    
          fuel[i][d] >= minFUEL;    
        end"

    This doen`t work.

    And is it possible to see, for my decision variabel, why the program has chosen it like it is? Sometimes I would expect something different and I don`t know how to understand it`s behaviour.

     

    And once again I`m very thankful for your support!


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 10.  Re: On going value in cplex

    Posted 02/22/18 03:36 AM

    You can specify bounds in the definition of decision variables. Like so:

    float minFUEL = 0.1;
    float maxFUEL = 1.5;
    range float FuelRange = minFUEL .. maxFUEL;
    dvar float fuel[c][d] in FuelRange;

    This should however be equivalent to your first set of constraints. And CPLEX should not return solution that violate these constraints (or the bounds). Do you really see solutions that violate these constraints? If so, are these reported as feasible solutions or as relaxations? Maybe you have to uncheck "Display relaxations" and/or "Display conflicts" in the settings editor to prevent the IDE from computing relaxations and instead get an error if the model is infeasible.

    If you see results that look unexpected and you would have expected different things then one way to try to understand this is to fix the variables to the values you expected and solve again. With this you can often find out if your expectation is wrong, your model is missing some constraints, etc.


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 11.  Re: On going value in cplex

    Posted 02/22/18 04:16 AM

    Originally posted by: Bueffel


    How can I change the values of decision variables?

    Now I´m getting an error, depending on my driven distances:

    Beschreibung    Ressource    Pfad    Position    Typ
    Ausnahme von IBM ILOG CPLEX: CPLEX Error  5002: objective is not convex.->Problem can be solved to global optimality with solution target 3->.    Load        Unbekannt    OPL-Problemmarkierung

     

    If i change some distances the error disappears.


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 12.  Re: On going value in cplex

    Posted 02/22/18 04:32 AM

    To fix decision variables just add a constraint that fixes the variable to a certain value:

    x == 0.5;

    For your issue with the quadratic objective you may want to read this chapter in the user manual.


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 13.  Re: On going value in cplex

    Posted 03/01/18 04:27 AM

    Originally posted by: Bueffel


    I don`t find a solution in the manual. The function i want to maximize is like "(a-0.1)^2".

    Do you have any hints for me?


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 14.  Re: On going value in cplex

    Posted 02/22/18 04:20 AM

    Originally posted by: Bueffel


    The error occurs because of an sqare in my dexpr. I delete it for the moment. Thankt for your help, maybe I´ll come back here soon. Thank you!


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 15.  Re: On going value in cplex

    Posted 02/23/18 03:40 AM

    Originally posted by: Bueffel


    Nice idea! Thanks a lot for your support!


    #DecisionOptimization
    #OPLusingCPOptimizer