0

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

Marking Optimization Mathematical Equations

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

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.

Result from Pycharm

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

  • are you sure you have indexed constraint 18 correctly? From the formulation snippet that you posted, it is hard to tell. Not sure what the set "S" is, for example – ekrall Aug 07 '22 at 02:47
  • Yes, it is. 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])))) – Nicholas Nicholas Aug 07 '22 at 03:00

2 Answers2

0

You stated "The correct answer should be y = 0.014285" but you have declared the y variable to be an integer using:

y = mdl.integer_var(lb = 0, ub=inf, name='y')

Try fixing that first. If you still have issues with your model and the results, use some 'standard' model debugging approaches:

  • Try outputing the generated model from CPLEX into an LP file. The format is quite readable and you can check whether the model that you have actually created agrees with what you intended to create.
  • Add some constraints to fix the values of some or all of your decision variables to known values and run the model again to see whether those values are feasible and what objective value they give you.
TimChippingtonDerrick
  • 2,042
  • 1
  • 12
  • 13