Decision Optimization

Decision Optimization

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

 View Only
  • 1.  Extract the solution value for expression in CP

    Posted Fri July 07, 2023 11:23 AM

    Given a CP optimization like this which can be solved:

    mdl = CpoModel()
    x = mdl.integer_var( min = 0, max = 2, name = 'x')
    y= mdl.integer_var( min = 0, max = 3, name = 'y')

    obj =mdl.maximize(x+y)
    mdl.add(obj)
    msol = mdl.solve()
    msol.print_solution()
    Variables:
    x = 2
     y = 3

    and when we define a KPI in the form of an expression (type docplex.cp.expression.CpoFunctionCall)

    kpi = x**2+y**2
    How can we extract the solution value for this expression? For example in this case it should be 2^2+3^2=13


    ------------------------------
    Mohammad Gorji-Sefidmazgi
    ------------------------------


  • 2.  RE: Extract the solution value for expression in CP

    Posted Mon July 10, 2023 02:07 AM

    Hi,

    you can use the msol object.

    small example at https://github.com/AlexFleischerParis/zoodocplex/blob/master/zookpicpo.py

    from docplex.cp.model import CpoModel
    
    mdl = CpoModel(name='buses')
    nbbus40 = mdl.integer_var(0,1000,name='nbBus40')
    nbbus30 = mdl.integer_var(0,1000,name='nbBus30')
    
    nbbus=nbbus40+nbbus30
    mdl.add_kpi(nbbus,"nbbus")
    
    mdl.add(nbbus40*40 + nbbus30*30 >= 300)
    mdl.minimize(nbbus40*500 + nbbus30*400)
    
    msol=mdl.solve()
    
    print(msol[nbbus40]," buses 40 seats")
    print(msol[nbbus30]," buses 30 seats")
    
    print("nbbus = ",msol["nbbus"])
    
    """
    
    6  buses 40 seats
    2  buses 30 seats
    nbbus =  8
    
    """


    ------------------------------
    [Alex] [Fleischer]
    [Data and AI Technical Sales]
    [IBM]
    ------------------------------



  • 3.  RE: Extract the solution value for expression in CP

    Posted Mon July 10, 2023 11:56 AM
    Edited by Mohammad Gorji-Sefidmazgi Mon July 10, 2023 11:57 AM

    @alex Thank you for the response. So you are suggesting that in CP, for calculating an expression, we must define it as a KPI "before" solving the model. 

    However in MP, we can find the expression value after the solution.

     For example in MP, we can can calculate the expression  nbbus40-nbbus30 after solving the model, while it was "Not" defined as a KPI.

    from docplex.mp.model import Model

    mdl = Model(name='buses')
    nbbus40 = mdl.integer_var(0,1000,name='nbBus40')
    nbbus30 = mdl.integer_var(0,1000,name='nbBus30')

    nbbus=nbbus40+nbbus30
    mdl.add_kpi(nbbus,"nbbus")

    mdl.add(nbbus40*40 + nbbus30*30 >= 300)
    mdl.minimize(nbbus40*500 + nbbus30*400)

    msol=mdl.solve()

    print(msol[nbbus40]," buses 40 seats")
    print(msol[nbbus30]," buses 30 seats")
    print(msol[nbbus]," nbbus")
    print((nbbus40-nbbus30).solution_value," nbbus subtraction")

    6.0 buses 40 seats
    2.0 buses 30 seats
    8.0 nbbus
    4.0 nbbus subtraction
    Is it possible to do something similar in CP? Calculating an expression without defining it as KPI?

     



    ------------------------------
    Mohammad Gorji-Sefidmazgi
    ------------------------------



  • 4.  RE: Extract the solution value for expression in CP

    Posted Tue July 11, 2023 02:21 AM

    I have same issue 



    ------------------------------
    Gourav Rao
    ------------------------------



  • 5.  RE: Extract the solution value for expression in CP

    Posted Tue July 11, 2023 04:22 AM

    Let me share 2 options:

    from docplex.cp.model import CpoModel
    
    mdl = CpoModel(name='buses')
    nbbus40 = mdl.integer_var(0,1000,name='nbBus40')
    nbbus30 = mdl.integer_var(0,1000,name='nbBus30')
    nbbus2 = mdl.integer_var(0,1000,name='nbBus32')
    
    nbbus=nbbus40+nbbus30
    
    
    mdl.add(nbbus40*40 + nbbus30*30 >= 300)
    mdl.add(nbbus2==nbbus40+nbbus30)
    mdl.minimize(nbbus40*500 + nbbus30*400)
    
    msol=mdl.solve()
    
    print(msol[nbbus40]," buses 40 seats")
    print(msol[nbbus30]," buses 30 seats")
    
    print("nbbus = ",msol[nbbus40]+msol[nbbus30])
    print("nbbus = ",msol[nbbus2])
    


    ------------------------------
    [Alex] [Fleischer]
    [Data and AI Technical Sales]
    [IBM]
    ------------------------------



  • 6.  RE: Extract the solution value for expression in CP

    Posted Tue July 11, 2023 04:22 AM

    Let me share 2 options:

    from docplex.cp.model import CpoModel
    
    mdl = CpoModel(name='buses')
    nbbus40 = mdl.integer_var(0,1000,name='nbBus40')
    nbbus30 = mdl.integer_var(0,1000,name='nbBus30')
    nbbus2 = mdl.integer_var(0,1000,name='nbBus32')
    
    nbbus=nbbus40+nbbus30
    
    
    mdl.add(nbbus40*40 + nbbus30*30 >= 300)
    mdl.add(nbbus2==nbbus40+nbbus30)
    mdl.minimize(nbbus40*500 + nbbus30*400)
    
    msol=mdl.solve()
    
    print(msol[nbbus40]," buses 40 seats")
    print(msol[nbbus30]," buses 30 seats")
    
    print("nbbus = ",msol[nbbus40]+msol[nbbus30])
    print("nbbus = ",msol[nbbus2])
    


    ------------------------------
    [Alex] [Fleischer]
    [Data and AI Technical Sales]
    [IBM]
    ------------------------------



  • 7.  RE: Extract the solution value for expression in CP

    Posted Tue July 11, 2023 08:44 AM

    Thanks @ALEX FLEISCHER  You mentioned you wanna "share 2 options". but the posts number 5 and 6 have the same code. Is this correct?



    ------------------------------
    Mohammad Gorji-Sefidmazgi
    ------------------------------



  • 8.  RE: Extract the solution value for expression in CP

    Posted Wed July 12, 2023 02:55 AM

    The 2 options:

    1) Recompute the expression

    2) Use a decision variable

    print("nbbus = ",msol[nbbus40]+msol[nbbus30])
    print("nbbus = ",msol[nbbus2])


    ------------------------------
    [Alex] [Fleischer]
    [Data and AI Technical Sales]
    [IBM]
    ------------------------------



  • 9.  RE: Extract the solution value for expression in CP

    Posted Wed July 12, 2023 02:28 PM

    Hi Gorji, 

    I am putting the solution we discussed here. 

    get_kpi_value(name)[source]
    Get the value of a KPI
        Parameters:name – Name of the KPI  Returns:Value of the KPI  Raises:KeyError if KPI is not in the solution.
    get_kpis()[source]
    Get the solution KPIs.
        Returns:Ordered dictionary containing value of the KPIs that have been defined in the model. Key is KPI publish name, value is expression value. Keys are sorted in the order the KPIs have been defined.

    http://ibmdecisionoptimization.github.io/docplex-doc/cp/docplex.cp.solution.py.html?highlight=get%20kpi%20value#docplex.cp.solut[…]on.get_kpi_value



    ------------------------------
    Fred Garrett
    ------------------------------