Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.

 View Only
  • 1.  Including a max constraint in CPLEX MILP - Python

    Posted Mon December 16, 2024 09:34 AM

    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
    ------------------------------


  • 2.  RE: Including a max constraint in CPLEX MILP - Python

    Posted Tue December 17, 2024 07:21 AM

    Yes use max

    from docplex.mp.model import Model
    
    mdl = Model(name='buses')
    
    nbKids=300;
    buses=[30,40,50]
    
    #decision variables
    mdl.nbBus = {b: mdl.integer_var(name="nbBus"+str(b)) for b in buses}
    
    # Constraint
    mdl.add_constraint(sum(mdl.nbBus[b]*b for b in buses) >= nbKids, 'kids')
    
    # Objective
    # logical constraint is the max of all nbBus
    mdl.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]
    ------------------------------



  • 3.  RE: Including a max constraint in CPLEX MILP - Python

    Posted Mon December 30, 2024 09:59 PM

    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
    ------------------------------



  • 4.  RE: Including a max constraint in CPLEX MILP - Python

    Posted Tue December 31, 2024 04:03 AM

    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]
    ------------------------------