Decision Optimization

 View Only
Expand all | Collapse all

IndexError: index 2 is out of bounds for axis 0 with size 2

  • 1.  IndexError: index 2 is out of bounds for axis 0 with size 2

    Posted Tue August 09, 2022 10:57 AM
    Edited by System Test Fri January 20, 2023 04:43 PM
    Dear All,
    Regarding the CPLEX docplex model for these MILP mathematical models, I would like to generalize the constraint #18 since the A depends on n.
    For examples:
    for n = 3 then A = np.array([1,2,3,[1,3]]) #len(A) = 4
    # the first three elements only have 1 integer but the fourth element has two integers, for example: A[0] = 1 and A[3] = [1,3])

    for n = 4 then A = np.array([1,2,3,4,[1,3],[1,4],[2,5]]) #len(A) = 7

    for n = 5 then A = np.array([1,2,3,4,5,[1,3],[1,4],[1,5],[2,4],[2,5],[3,5],[1,3,5]]) #len(A) = 12
    # the first five elements only have 1 integer, the elements from index 5 to 10 have 2 integers,and the last element (index 11) has 3 integers, for example: A[1] = 2 , A[6] = [1,4], and A[11] = [1,3,5]
    Here are the mathematical models.
    image.png
    Here is the code.
    import cplex
    from docplex.mp.model import Model
    import numpy as np

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


    n = 3 # number of steps
    A = np.array([1,2,3,[1,3]])
    range_A = range(len(A))
    p = np.array([40,100,60])
    c = np.array([20,100,50])
    v = 3
    w = 5
    m = np.array([1,2,1])


    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

    for
    i, idx in enumerate(A):
    # get z and c
    z_temp = z[np.array(idx)-1]
    c_temp = c[np.array(idx)-1]

    mdl.add_constraint((1 + sum(z_temp[i] for i in range_A)) >= y * ((n + 1 - 2 * (len(A))) * (2 * v + 2 * w) + sum((2 * w + v + c_temp[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)

    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.solution_value
    Could anyone let me know how to write the code for constraint #18 correctly so that it will be applied for any n and A scenarios, please? (cc: @Renaud Dumeur, @Vincent Beraudier, @ALEX FLEISCHER)

    Thank you so much.
    Best regards,
    Nicholas
    ​​​​​​​
    #DecisionOptimization


  • 2.  RE: IndexError: index 2 is out of bounds for axis 0 with size 2

    Posted Wed August 17, 2022 04:19 AM

    Hello Nicholas,

    It's not very clear to me here what is going here, but in general you should first find out which line of your code is causing the violation, and then dig down to see exactly what is going on.  Already this code looks strange to me:

    for i, idx in enumerate(A):
        # get z and c
        z_temp = z[np.array(idx)-1]
        c_temp = c[np.array(idx)-1]

    The values of z_temp and c_temp get overwritten by the next loop.  Is this really what you want?

    Regards,

    Paul



    ------------------------------
    Paul Shaw
    ------------------------------