Decision Optimization

Decision Optimization

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

 View Only
Expand all | Collapse all

How to define a set of decision variables in ascending order?

  • 1.  How to define a set of decision variables in ascending order?

    Posted Mon July 18, 2022 10:57 AM
    Edited by System Admin Fri January 20, 2023 04:43 PM
    Dear All,
    Good day to you.
    I would like to define two decision variables for docplex.mp in cplex as follows.
    X = a set of decision variables, i.e.[ [X1] ,
                                                             [X2] ,
                                                             [X3], 
                                                             [X4],
                                                             [X5], 
                                                             [X6] ]
    S = a set of sorted decision variables of X in ascending order (smallest value of X to the largest value of X), i.e. [ [S1] ,
                                                                                                                                                                                       [S2] ,
                                                                                                                                                                                       [S3], 
                                                                                                                                                                                       [S4],
                                                                                                                                                                                       [S5], 
                                                                                                                                                                                       [S6] ]
    I need the S decision variables for the next MILP constraints.
    Total_T = 6
    x = np.empty((Total_T,1), dtype= object)
    for i in range(Total_T):
    x[i] = mdl.continuous_var(lb= 0, ub= inf, name='x' + str(i+1))
    S = x.sort()
    When I tried to use the above code, it seems incorrect. Could anyone let me know how could I define the decision variable for S, please? cc: @ALEX FLEISCHER , @Vincent Beraudier @Hugues Juille
    Thank you in advance.

    Yours sincerely,
    Nicholas
    
    

    #DecisionOptimization


  • 2.  RE: How to define a set of decision variables in ascending order?

    Posted Mon July 18, 2022 11:18 AM
    You will have to add a bunch of binary variables and constraints to enforce this. Unfortunately, they require a finite upper bound on each X variable, whereas you have declared the upper bounds to be infinity. Can you come up with a priori upper bounds for every X?

    ------------------------------
    Paul Rubin
    Professor Emeritus
    Michigan State University
    ------------------------------



  • 3.  RE: How to define a set of decision variables in ascending order?

    Posted Mon July 18, 2022 11:41 AM
    Dear Paul,

    Unfortunately there is no apriori upper bound for each X. So, the CPLEX will get the value for each X with the objective function to minimize the cycle time (lambda).

    Will there be any possibility of other ways to define the decision variable S?

    Thank you so much.

    Yours sincerely,
    Nicholas





  • 4.  RE: How to define a set of decision variables in ascending order?

    Posted Mon July 18, 2022 12:07 PM
    You might be able to make it work with implication (if-then) constraints, but if X is truly unbounded I'm not sure how well CPLEX would handle those constraints. What do the X variables represent?

    ------------------------------
    Paul Rubin
    Professor Emeritus
    Michigan State University
    ------------------------------



  • 5.  RE: How to define a set of decision variables in ascending order?

    Posted Mon July 18, 2022 12:23 PM
    You will certainly have to sort it yourself. 
    Indeed, I think a problem is that numpy sort will always fail: when sorting the variables, it will certainly call the <, <= operators which will try to create a constraint as it will see a variable (< will fail with an exception, <= will create a constraint).

    Why do you use numpy for the CPLEX part of your script?
    In general, using Python standard structures to handle the variables and constraint containers lead to a much readable model code imho.
    Did you try to use pandas,numpys for your data, write the model with standard python, and then use pandas/numpy for the result publishing?

    ------------------------------
    Vincent Beraudier
    ------------------------------



  • 6.  RE: How to define a set of decision variables in ascending order?

    Posted Mon July 18, 2022 12:35 PM
    Edited by System Admin Fri January 20, 2023 04:11 PM
    Dear Vincent,
    1. In case that I need to sort the X manually, hence I am thinking of defining the decision variables S and its corresponding constraints after getting the solution (value of each x in X) from cplex (solver). What I meant are as follows.
          1. Put all the related decision variables, constraints, and objective function.
          2. Run the CPLEX to get the result (value) for each x in X.
          3. Create s in S as S = X.sort() and put the S into the respective constraints with S.
                 If the X provided from the solver meet the criteria, then X is optimal solution.
                 If not then maybe solver needs to run the program again? However, I am not sure if CPLEX could provide different answer    (solution) for X.
              What do you think?



    2. The reason why I use numpy for the CPLEX is because it seems easier for me to define the variables as well as mathematical operations for the CPLEX library. Noted, I will try to explore using pandas/ numpys for your data, writing the model with standard python, and then using pandas/numpy for the result publishing.
    Thank you so much.
    Yours sincerely,
    Nicholas





  • 7.  RE: How to define a set of decision variables in ascending order?

    Posted Mon July 18, 2022 12:28 PM
    Dear Paul,

    To give you a little bit of background, the objective is to minimize the cycle time (lambda) for a semiconductor cluster tools scheduling case.
    From the Timed Petri Net concept, we will have a set of transitions (T) and Places (P). Each t in T will be corresponded to each p in P.

    x is the firing epoch of transition ti ∈ T in a cycle. So, if there are 6 transitions, then there will be 6 x's.

    Let me know if you have any further questions, thank you.

    Yours sincerely,
    Nicholas






  • 8.  RE: How to define a set of decision variables in ascending order?

    Posted Mon July 18, 2022 01:35 PM
    If each X is an epoch, then they should all be bounded above by the maximum possible epoch. So you would just need an a priori limit on cycle time.
    In any case, the following is how I would incorporate S directly in the model. In addition to X and S, you would need binary decision variables Y[i][j], where Y[i][j] = 1 implies that S[i] = X[j] (and so X[j] is the i-th smallest of the X values). With 6 transitions, this would be 36 Y variables.

    To make sure that the Y variables are well defined, you need constraints that say the sum over j of Y[i][j] = 1 for all i and the sum over i of Y[i][j] = 1 for all j.

    To get the sorted epoch times in S, you add constraints S[1] <= S[2] <= ... <= S[6].

    Now you just need enforce the definition of Y[i][j]. One possibility is to use implication (if-then) constraints in the model: Y[i][j] == 1 => S[i] = X[j] for all i and j. The other possibility, which depends on knowing an upper bound M for very X variable, is to use two inequalities for each combination of i and j: S[i] <= [j] + M(1-Y[i][j]) and S[i] >= X[j] - M(1 - Y[i][j]). With a reasonably tight (but valid) bound M, I suspect the second approach will solve faster. If you don't have a value for M (other than setting M to some extremely big number), the first approach is at least worth a try.

    ------------------------------
    Paul Rubin
    Professor Emeritus
    Michigan State University
    ------------------------------



  • 9.  RE: How to define a set of decision variables in ascending order?

    Posted Mon July 18, 2022 10:47 PM
    Edited by System Admin Fri January 20, 2023 04:22 PM
    Dear Paul,

    Thank you so much for your recommendation. I already have the Y variables, which in my case it is called Z[k][i].
    However, there is an error when I defined the constraint for the sorted S. Do you have any idea on what I might define it incorrectly? Thank you.

    This is my code:

    Total_T = 6

    obj_lambda = mdl.continuous_var(lb = 0, ub=inf, name='obj_lambda')

    x = np.empty((Total_T,1), dtype= object)
    for i in range(Total_T):
    x[i] = mdl.continuous_var(lb= 0, ub= inf, name='x' + str(i+1))

    Z = np.empty((Total_T, Total_T), dtype = object)
    for k in range(Total_T):
    for i in range(Total_T):
    Z[k][i] = mdl.binary_var(name='Z' + str(k+1) + str(',') + str(i+1))
    S = np.empty((Total_T,1), dtype= object)
    for k in range(Total_T):
    S[k] = mdl.continuous_var(lb= 0, ub= inf, name='S' + str(k+1))
    for k in range(Total_T):
    for i in range(Total_T):
    if Z[k][i] is 1:
    S[k] = x[i]
    for k in range(Total_T):
    if S is not None:
    mdl.add_constraint(S[k]<= S[k + 1] <= S[k + 2] <= S[k + 3] <= S[k + 4] <= S[Total_T - 1] <= S[k] + obj_lambda)
    Traceback (most recent call last):
    File "C:/Users/pknu/Documents/Research/project/MILP Correct 19 July 2022.py", line 152, in <module>
    mdl.add_constraint(S[k]<= S[k + 1] <= S[k + 2] <= S[k + 3] <= S[k + 4] <= S[Total_T - 1] <= S[k] + obj_lambda)
    File "C:\Users\pknu\anaconda3\envs\env-cplex\lib\site-packages\docplex\mp\constr.py", line 101, in __bool__
    self._no_linear_ct_in_logical_test_error() # pragma: no cover
    File "C:\Users\pknu\anaconda3\envs\env-cplex\lib\site-packages\docplex\mp\constr.py", line 549, in _no_linear_ct_in_logical_test_error
    raise TypeError(msg)

    TypeError: Cannot convert a linear constraint to boolean: S1 <= S2

    In addition, the next two constraints which I am going to write are:
    bigM = 1000000

    for k in range(Total_T):
    for i in range(Total_T):
    if S.any() is not None and x.any() is not None:
    mdl.add_constraint(S[k] - xi[i] <= (1 - Z[k][i])*bigM)
    mdl.add_constraint(S[k] - xi[i] >= (Z[k][i] - 1)*bigM)

    ------------------------------
    Nicholas Nicholas
    ------------------------------



  • 10.  RE: How to define a set of decision variables in ascending order?

    Posted Mon July 18, 2022 11:09 PM
    I'm not a python user, but I'm pretty sure you should be adding a single constraint S[k] <= S[k+1] at a time.

    ------------------------------
    Paul Rubin
    Professor Emeritus
    Michigan State University
    ------------------------------