This is the problem I am trying out to resolve using Pulp :
The factory produces 2 objects x and y and sells them resp 10.5 Dollars and 8.5 Dollars; when the production of object x exceeds 20 units, then a maintenance cost of 10 euros is subtracted for our benefit. How to model this simple problem with solvers?
This is a IF conditional basical constraint. It seems to works using CPLEX :
import cplex
import docplex.mp
from docplex.mp.model import Model
maintenance_cost = 10
maintenance_trigger = 20
# Create model
model = Model(name='LP_example', log_output=True)
# Decisions variables
x = model.integer_var(name='x')
y = model.integer_var(name='y')
z = model.binary_var(name='z')
# Objective function
model.maximize(10.5 * x + 8.5 * y - z * maintenance_cost)
# Constraints
model.add_constraint(3 * x + 2 * y <= 420)
# if then constraint
model.add_constraint(model.if_then(x >= maintenance_trigger, z == 1))
model.print_information()
sol_model = model.solve()
model.print_solution()
but, I am bloqued, when using Pulp, how should I set the z variable correctly , how should I set the constraint ? I have no idea :
import pulp as p
Lp_prob = p.LpProblem('Problem', p.LpMaximize)
x = p.LpVariable("x", lowBound = 0, cat='Integer') # Create a variable x >= 0
y = p.LpVariable("y", lowBound = 0, cat='Integer') # Create a variable y >= 0
z = p.LpVariable("z", lowBound=0, cat='Binary')
Lp_prob += 10.5 * x + 8.5 * y - maintenance_cost * z
Lp_prob += 3 * x + 2 * y <= 420
# if then constraint
# The problematic part (Conditional constraint):
Lp_prob += z == 1 # ???
status = Lp_prob.solve()
print(p.value(x)," x produced objects")
print(p.value(y) , "y produced objects" )
print(p.value(z) , "The value of the binary variable, used or not 1 or 0" )
print(p.value(Lp_prob.objective) ,"our Profit" )
EDIT : I have found a solution there : How can I write an IF condition for my decision variable for Mixed Integer Linear Programming (MILP) using PuLP GLPK on Python?
So my pulp code now includes this "BigM" conditional constraint:
M1 = 1e6
Lp_prob += z >= (x - 20)/M1
I do not understand how M1 works, but the result seems to be ok now, any info appreciated.
Edit: I have removed both MIN production volume constraints, because my wish was only to understand if my BigM constraints was well written using Pulp. I don't know how to write "When the production of object x exceeds 20 units, then a maintenance cost of 10 dollars is subtracted for our benefit" and I'd like to add a second big M like this :"When the production of object x exceeds 50 units, then a maintenance cost of 20 dollars is subtracted for our benefit, not 10 dollars".