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

Viewed 17

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 example:

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

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

Here are the mathematical models.

Mathematical Models

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]]) #number of nonadjacent process steps
range_A = range(len(A))
p = np.array([40,100,60]) #processing time for PM1 (second)
c = np.array([20,100,50])
v = 3 #robot moving time (second)
w = 5 #loading or unloading time (second)

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


#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
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? Thank you so much.

Best regards, Nicholas

0 Answers
Related