Decision Optimization

 View Only
  • 1.  The CPLEX does not provide the expected result

    Posted Sat August 06, 2022 11:07 PM
    Dear All,

    I am developing the python code for Marking Optimization Problem by using python CPLEX library. Here are the mathematical equations.

    image.png

    Additional explanation for constraint no. 18:

    A ∈ S is a set of nonadjacent process steps , which in this specific case A ∈ S = ({1},{2}). 

    If I breakdown the constraint 18, they would be: 

    for i in range(len(A)): 

    mdl.add_constraint((1 + z[0] >= y*((n+1 -2*(len(A)))*(2*v + 2*w) + (2*w+v+c[0])))) 

    mdl.add_constraint((1 + z[1] >= y*((n+1 -2*(len(A)))*(2*v + 2*w) + (2*w+v+c[1])))) 

    Here is the python code.
    (I give comments by # for each of the codes with the respective mathematical equations).

    ####    

    import cplex

        from docplex.mp.model import Model

        import numpy as np
       
        mdl = Model(name='Marking Optimization')
        inf = cplex.infinity
       
       
        n = 2
        A = np.array([1,2])
       
        p = np.array([40,100])
        c = np.array([20,100])
        v = 3
        w = 5
        m = np.array([1,3])
       
       
        y = mdl.integer_var(lb = 0, ub=inf, name='y')
       
        z = np.empty((n,), dtype= object)
        for i in range(n):
            z[i] = mdl.integer_var(lb = 0, ub=inf, name='z' + str(i + 1))
       
        #constraint 15
        mdl.add_constraint(1 >= y*(n+1)*(2*v + 2*w))
       
        #constraint 16
        for i in range(n):
            mdl.add_constraint(1 >= y*1/m[i]*(p[i] + c[i] + 2*w))
       
        #constraint 17
        for i in range(n):
            mdl.add_constraint(m[i] - z[i] >= y*(p[i] + 3*v + 4*w))
       
        #constraint 18

        for i in range(len(A)):
            mdl.add_constraint((1 + z[i] >= y*((n+1 -2*(len(A)))*(2*v + 2*w) + (2*w+v+c[i]))))
           
       
        #constraint 19A
        for i in range(n):
            mdl.add_constraint(0 <= z[i])
       
        #constraint 19B
        for i in range(n):
            mdl.add_constraint(z[i] <= m[i] -1)
       
        #equation 14
        mdl.maximize(y)
        mdl.print_information()
        solver = mdl.solve() #(log_output=True)
        if solver is not None:
            mdl.print_solution()
        else:
            print("Solver is error")

    ####

    When I run the program, the result is not as expected. The correct answer should be y = 0.014285, z1 = 0 and z2 = 1. However the result from the code is 0 as below screenshot.

    image.png
    Could anyone tell me, what is wrong here and what should I do, please? Thank you in advance!

    #DecisionOptimization


  • 2.  RE: The CPLEX does not provide the expected result

    Posted Mon August 08, 2022 06:18 AM
    Dear Nicholas,

    Please review your breakdown of eq. 18 for which I see two problems:
    First, such breakdown introduces  extra constraints on the individual 'z' values that do not exist on the initial formulation.
    It is because although 
    (a > b)  & (c > d) => (a + c) > (c + d)​
    is true, the reverse implication:
     (a + c) > (c + d)​ => (a > b)  & (c > d)​

    is not. 


    Second, the breakdown looks also wrong considering:
    1 + z[0] >= y*((n+1 -2*(len(A)))*(2*v + 2*w) + (2*w+v+c[0])
    1 + z[1] >= y*((n+1 -2*(len(A)))*(2*v + 2*w) + (2*w+v+c[1])

    when summed produce:

    2 + sum(i in A) z[i] >= 2*y(n+1-2*len(AA))*(2*v + 2*w) + sum(i in A) (2*w+v+c[i])​
    

    which doesn't correspond to the original formula.

    I hope this helps




    ------------------------------
    Renaud Dumeur
    ------------------------------



  • 3.  RE: The CPLEX does not provide the expected result
    Best Answer

    Posted Mon August 08, 2022 06:29 AM
    Dear Nicholas,

    This model works as expected. It defines 'y' as a continuous variable and doesn't break down the constraint #18.

    import cplex
    
    from docplex.mp.model import Model
    
    import numpy as np
    
    mdl = Model(name='Marking Optimization')
    inf = cplex.infinity
    
    
    n = 2
    A = np.array([1,2])
    range_A = range(len(A))
    
    p = np.array([40,100])
    c = np.array([20,100])
    v = 3
    w = 5
    m = np.array([1,3])
    
    
    y = mdl.continuous_var(lb = 0, ub=inf, name='y')
    
    z = np.empty((n,), dtype= object)
    for i in range(n):
        z[i] = mdl.integer_var(lb = 0, ub=inf, name='z' + str(i + 1))
    
    #constraint 15
    mdl.add_constraint(1 >= y*(n+1)*(2*v + 2*w))
    
    #constraint 16
    for i in range(n):
        mdl.add_constraint(1 >= y*1/m[i]*(p[i] + c[i] + 2*w))
    
    #constraint 17
    for i in range(n):
        mdl.add_constraint(m[i] - z[i] >= y*(p[i] + 3*v + 4*w))
    
    #constraint 18
    mdl.add_constraint((1 + sum(z[i] for i in range_A)) >= y*((n+1 -2*(len(A)))*(2*v + 2*w) + sum((2*w+v+c[i]) for i in range_A)))
    
    #constraint 19A
    for i in range(n):
        mdl.add_constraint(0 <= z[i])
    
    #constraint 19B
    for i in range(n):
        mdl.add_constraint(z[i] <= m[i] -1)
    
    #equation 14
    mdl.maximize(y)
    mdl.print_information()
    solver = mdl.solve() #(log_output=True)
    if solver is not None:
        mdl.print_solution()
    else:
        print("Solver is error")
    ​

    ​​

    ------------------------------
    Renaud Dumeur
    ------------------------------



  • 4.  RE: The CPLEX does not provide the expected result

    Posted Tue August 09, 2022 10:07 AM

    Thank you so much @Renaud Dumeur!

    By the way, after getting the result for y, I would like to put a new equation : obj_lambda = 1/y .
    When I wrote the code after the cplex as follows, there is error as in the next screenshot.
    In this case, is there any way for me to add the new equation? Thank you.

    import cplex
    from docplex.mp.model import Model
    import numpy as np

    mdl = Model(name='Marking Optimization')
    inf = cplex.infinity


    n = 2 # number of steps
    A = np.array([1,2]) #number of nonadjacent process steps
    range_A = range(len(A))
    p = np.array([40,100]) #processing time for PM1 (second)
    c = np.array([20,100])
    v = 3 #robot moving time (second)
    w = 5 #loading or unloading time (second)

    m = np.array([1,3])


    #print('m1',m[0]) # y = 1/obj_lambda
    y = mdl.continuous_var(lb = 0, ub=inf, name='y') #could not be continuous

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

    # #constraint 15
    mdl.add_constraint(1 >= y*(n+1)*(2*v + 2*w))

    # #constraint 16
    for i in range(n):
    mdl.add_constraint(1 >= y*1/m[i]*(p[i] + c[i] + 2*w))

    # #constraint 17
    for i in range(n):
    mdl.add_constraint(m[i] - z[i] >= y*(p[i] + 3*v + 4*w))
    #
    # #constraint 18

    mdl.add_constraint((1 + sum(z[i] for i in range_A)) >= y * ((n + 1 - 2 * (len(A))) * (2 * v + 2 * w) + sum((2 * w + v + c[i]) for i in range_A)))
    # mdl.add_constraint((1 + z[0] >= y*((n+1 -2*(len(A)))*(2*v + 2*w) + (2*w+v+c[0]))))
    # mdl.add_constraint((1 + z[1] >= y*((n+1 -2*(len(A)))*(2*v + 2*w) + (2*w+v+c[1]))))
    #constraint 19A
    for i in range(n):
    mdl.add_constraint(0 <= z[i])

    #constraint 19B
    for i in range(n):
    mdl.add_constraint(z[i] <= m[i] -1)
    #

    mdl.maximize(y)
    mdl.print_information()
    solver = mdl.solve() #(log_output=True)
    if solver is not None:
    mdl.print_solution()
    else:
    print("Solver is error")

    obj_lambda = 1/y
    Error message


    ​Thank you.

    Best regards,
    Nicholas



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



  • 5.  RE: The CPLEX does not provide the expected result

    Posted Tue August 09, 2022 10:15 AM
    y is always a variable. To get its value after solve, you have to use y.solution_value
    So you should do the following?
    obj_lambda = 1/y.solution_value


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



  • 6.  RE: The CPLEX does not provide the expected result

    Posted Mon August 08, 2022 08:25 AM
    I' puzzled.
    y is declared in the python code as an integer_var, how could it ever get an optimal value y=0.014285 which is not an integer number ?


    ------------------------------
    Stefano Gliozzi
    ------------------------------