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