Hi,
within docplex you can use logical constraints
from docplex.mp.model import Model
mdl = Model(name='buses')
nbbus40 = mdl.integer_var(name='nbBus40')
nbbus30 = mdl.integer_var(name='nbBus30')
mdl.add_constraint(nbbus40*40 + nbbus30*30 >= 300, 'kids')
mdl.minimize(nbbus40*500 + nbbus30*400)
mdl.solve()
for v in mdl.iter_integer_vars():
print(v," = ",v.solution_value)
print()
print("with if nb buses 40 more than 3 then nbBuses30 more than 7")
#logical constraint
mdl.add((nbbus40>=3)<=(nbbus30>=7))
mdl.minimize(nbbus40*500 + nbbus30*400)
mdl.solve()
for v in mdl.iter_integer_vars():
print(v," = ",v.solution_value)
------------------------------
[Alex] [Fleischer]
[Data and AI Technical Sales]
[IBM]
------------------------------
Original Message:
Sent: Mon December 30, 2024 09:59 PM
From: Mehrdad Poursadegh
Subject: Including a max constraint in CPLEX MILP - Python
Thank you Alex.
I found using mdl.max computationally very expensive, therefore, since my objective function is of minimization type, I did this:
```
monthly_max_demand = {month: mdl.continuous_var(name=f"max_demand_{month}") for month in range(1, 13)}mdl.add_constraint(monthly_max_demand[month] >=demands[i])
Where "demand[i]" is being calculated for each hour (in a loop). This works fine for now, however, the electricity tariff structure I am trying to implement bill the customer with different rate based on whether the peak demand occurs within peak hours or in off-peak hours. Therefore, I need to know when this peak demand is happening as well as its value. Also, I need to know that within the optimization model so that the algorithm can minimize the costs by shifting the demand based on energy storage options available to it.
I will appreciate any suggestion of yours on how I can do this.
------------------------------
Mehrdad Poursadegh
Original Message:
Sent: Tue December 17, 2024 07:21 AM
From: ALEX FLEISCHER
Subject: Including a max constraint in CPLEX MILP - Python
Yes use max
from docplex.mp.model import Modelmdl = Model(name='buses')nbKids=300;buses=[30,40,50]#decision variablesmdl.nbBus = {b: mdl.integer_var(name="nbBus"+str(b)) for b in buses}# Constraintmdl.add_constraint(sum(mdl.nbBus[b]*b for b in buses) >= nbKids, 'kids')# Objective# logical constraint is the max of all nbBusmdl.minimize(mdl.max(mdl.nbBus[b] for b in buses)) mdl.solve(log_output=True,)mdl.export("c:\\temp\\buses.lp")for v in mdl.iter_integer_vars(): print(v," = ",v.solution_value)
------------------------------
[Alex] [Fleischer]
[Data and AI Technical Sales]
[IBM]
Original Message:
Sent: Fri December 13, 2024 12:22 PM
From: Mehrdad Poursadegh
Subject: Including a max constraint in CPLEX MILP - Python
Hello,
I am trying to optimize the dispatching schedule of a battery system. In doing so, I need to find the maximum electrical demand for each month to be used for electricity bills calculation. Electrical demand is one of my continuous decision variables.
Currently, I have included a mdl.max but I am not sure if that is the best approach.
I appreciate any suggestions on this regard.
------------------------------
Mehrdad Poursadegh
------------------------------