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
Thank you.
Best regards,
Nicholas
------------------------------
Nicholas Nicholas
------------------------------
Original Message:
Sent: Mon August 08, 2022 06:29 AM
From: Renaud Dumeur
Subject: The CPLEX does not provide the expected result
Dear Nicholas,
This model works as expected. It defines 'y' as a continuous variable and doesn't break down the constraint #18.
import cplexfrom docplex.mp.model import Modelimport numpy as npmdl = Model(name='Marking Optimization')inf = cplex.infinityn = 2A = np.array([1,2])range_A = range(len(A))p = np.array([40,100])c = np.array([20,100])v = 3w = 5m = 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 15mdl.add_constraint(1 >= y*(n+1)*(2*v + 2*w))#constraint 16for i in range(n): mdl.add_constraint(1 >= y*1/m[i]*(p[i] + c[i] + 2*w))#constraint 17for i in range(n): mdl.add_constraint(m[i] - z[i] >= y*(p[i] + 3*v + 4*w))#constraint 18mdl.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 19Afor i in range(n): mdl.add_constraint(0 <= z[i])#constraint 19Bfor i in range(n): mdl.add_constraint(z[i] <= m[i] -1)#equation 14mdl.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
Original Message:
Sent: Sat August 06, 2022 11:06 PM
From: Nicholas Nicholas
Subject: The CPLEX does not provide the expected result
Dear All,
I am developing the python code for Marking Optimization Problem by using python CPLEX library. Here are the mathematical equations.
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.
Could anyone tell me, what is wrong here and what should I do, please? Thank you in advance!
#DecisionOptimization