Decision Optimization

Decision Optimization

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

 View Only
Expand all | Collapse all

If function in scipting log

  • 1.  If function in scipting log

    Posted Wed June 17, 2020 04:58 AM
    Hi!

    I would like to create an if function in the scripting log based on the outcomes of my objective function.

    My objective function is: maximize sum (i in S, t in T, v in V, r in R) (w[v][r] <= t + l1) * d[i][t] * x[i][t][v][r]
    + sum (i in S, t in T, v in V, r in R) ((w[v][r] >= t + l1 + 1) && (w[v][r] <= t + l2)) * d[i][t] * x[i][t][v][r] * p1
    + sum (i in S, t in T, v in V, r in R) (w[v][r] >= t + l2 + 1) * d[i][t] * x[i][t][v][r] * p2;

    I would like to calculate:
    d[i][t] * x[i][t][v][r] if w[v][r] <= t + l1,
    d[i][t] * x[i][t][v][r] if w[v][r] >= t + l1 + 1) && (w[v][r] <= t + l2, and
    d[i][t] * x[i][t][v][r] if w[v][r] >= t + l2 + 1.

    I have not used the scripting log often before, so I think I make minor mistakes in the formulation. However, I could not find an answer online that helped me.

    Thank you in advance!

    ------------------------------
    Floor M
    ------------------------------

    #DecisionOptimization


  • 2.  RE: If function in scipting log

    Posted Wed June 17, 2020 05:29 AM
    I am not sure what you are trying to do with the scripting log. This is an output-only widget, so you cannot edit that.


    Would it help to define an auxiliary variable and use logical constraints to set this? With the implication constraint '=>' you can write the conditions you listed:
    (w[v][r] <= t + l1) => (auxvar == d[i][t] * x[i][t][v][r]).
    If you want to do that only for feasible solutions then you could also post process feasible solutions and output whatever you like, see this chapter in the manual.

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



  • 3.  RE: If function in scipting log

    Posted Wed June 17, 2020 05:39 AM
    Hi Daniel,

    I just want to calculate the amount of d[i][t] * c[i][[t][v][r] before, between and after certain time limits
    The constraints are all set, but I want CPLEX to show me those results, because the optimal solution gives a summation of these three calculations and I want to see these results also separated from each other
    I thought the best way to do that is to use execute DISPLAY.

    ------------------------------
    Floor M
    ------------------------------



  • 4.  RE: If function in scipting log

    Posted Wed June 17, 2020 09:13 AM
    You may look at OPL parameter Language / Run / Postprocess feasible solutions.
    It allows post-processing (execute blocks after the constraints) to be called whenever CPLEX encounters a feasible solution.
    You may display a dexpr (KPI) value in scripting, or whatever model element value you want.

    ------------------------------
    Frederic Delhoume
    ------------------------------



  • 5.  RE: If function in scipting log

    Posted Wed June 17, 2020 09:20 AM
    You may also want to create some dexpr (decision expressions) and use them in your objective function (or not).
    You can then display them easily in scripting

    execute {
         writeln(somedexpr);
    }

    The CPO engine uses automatically dexprs as KPI and display them in its log. They are also displayed in the statistics panel.

    ------------------------------
    Frederic Delhoume
    ------------------------------



  • 6.  RE: If function in scipting log

    Posted Wed June 17, 2020 10:39 AM
    That sounds like that could be my solution! I already use post-processing and I see the feasible solutions in my scripting log.
    I tried to create a dexpr, but I keep getting errors when defining them.
    I also tried Daniel's option with auxvar, but as I am new to CPLEX I don't know how to define a auxvar and I cannot find it online.
    How should I define them? Thank you for helping!


    These are my sets, parameters and objective function:
    int ns = ...;
    range S = 0..ns;
    int nv = ...;
    range V = 1..nv;
    int nr = ...;
    range R = 1..nr; 
    int nt = ...;
    range T = 1..nt;
    int c[S][S] = ...;
    int d[i in S][t in T] = ...;
    int l1 = ...;
    int l2 = ...;
    float p1 = ...;
    float p2 = ...;
    int M = ...; 

    dvar boolean x[S][T][V][R];
    dvar boolean y[S][S][V][R];
    dvar boolean z[V][R];
    dvar int+ u[S][V][R];
    dvar int+ w[V][R];

    maximize  sum (i in S, t in T, v in V, r in R) (w[v][r] <= t + l1) * d[i][t] * x[i][t][v][r]
    + sum (i in S, t in T, v in V, r in R) ((w[v][r] >= t + l1 + 1) && (w[v][r] <= t + l2)) * d[i][t] * x[i][t][v][r] * p1
    + sum (i in S, t in T, v in V, r in R) (w[v][r] >= t + l2 + 1) * d[i][t] * x[i][t][v][r] * p2
    - sum (i, j in S, v in V, r in R) c[i][j] * y[i][j][v][r];

    ------------------------------
    Floor M
    ------------------------------



  • 7.  RE: If function in scipting log

    Posted Wed June 17, 2020 10:53 AM
    I recommend against using auxvar when printing things from an execute block. Can you show how you define your dexpr and what errors you get? Note that you cannot use "if" in a dexpr. But you can formulate conditions using the ternary operator ?:

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



  • 8.  RE: If function in scipting log

    Posted Wed June 17, 2020 11:05 AM
    Okay maybe that's where I'm going wrong, I have never used ?: before.
    Right now I tried this
    dexpr int q = sum (i in S, t in T, v in V, r in R) (w[V][R] <= t + l1) => d[S][T] * x[S][T][V][R];
    and the error I get is: Cannot use type range for int.

    ------------------------------
    Floor M
    ------------------------------



  • 9.  RE: If function in scipting log

    Posted Wed June 17, 2020 11:17 AM
    The syntax of ?: is
    condition ? if-true : if-false
    If 'condition' is true then the result of the expression is 'if-true', otherwise it is 'if-false'.
    Your dexpr could be
    dexpr int q[v in V][r in R] = (w[v][r] <= t + l1) ? (sum (i in S, t in T) d[i][t] * x[i][t][v][r]) : (0)
    The '(0)' you have to replace with the expression to be used in case w[v][r] > t + l1 (I am not clear what you want in this case)

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



  • 10.  RE: If function in scipting log

    Posted Wed June 17, 2020 11:28 AM
    Thank you for your response, this makes thing a lot clearer. The error I got before is gone. However, it now says I cannot use my decision variable w[v][r].

    (0) will be sufficient i guess, because I need to calculate if.... then..., otherwise nothing.

    ------------------------------
    Floor M
    ------------------------------



  • 11.  RE: If function in scipting log

    Posted Thu June 18, 2020 05:50 AM
    Cannot you please show the exact error message and also what exactly you wrote and how w is defined in your .mod.
    Even better, could you attach a .mod and .dat that reproduce the problem?

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



  • 12.  RE: If function in scipting log

    Posted Thu June 18, 2020 09:11 AM
    I attached my files, thank you for helping me!

    The error: Decision variable (or expression) "w" not allowed
    w is defined as w[V][R] as can be seen in the .mod file.

    ------------------------------
    Floor M
    ------------------------------



  • 13.  RE: If function in scipting log

    Posted Fri June 19, 2020 01:11 AM
    Indeed, a decision variable is not allowed in this place. Sorry for missing that.
    Given that the value in the "otherwise case" is zero there is an easy workaround, though:
    dexpr int q[v in V][r in R] = (w[v][r] <= t + l1) * (sum (i in S, t in T) d[i][t] * x[i][t][v][r]);
    If w[v][r] <= t + l1 then this expression evaluates to true, which is equivalent to 1 when used in arithmetic expressions. If the condition is false then this is treated as 0 in arithmetic expressions.
    Note that this will work only as long as you use q only for output. If you use q in constraints or in the objective function then the product of variables is likely to cause trouble and you may have to take a detour why an auxiliary variable
    dvar int q[V][R];
    and additional constraints
    forall (v in V, r in R) {
      (w[v][r] <= t + l1) => (q[v][r] == sum(i in S, t in T) d[i][t] * x[i][t][v][r]);
      (w[v][r] > t + l1) => (q[v][r] == 0);
    }

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



  • 14.  RE: If function in scipting log

    Posted Mon June 22, 2020 04:54 AM
    Hi Daniel,

    Thank you so much for helping, it works now!

    ------------------------------
    Floor M
    ------------------------------