Decision Optimization

 View Only
Expand all | Collapse all

Transforming model's result into a pandas dataframe

  • 1.  Transforming model's result into a pandas dataframe

    Posted Tue April 07, 2020 07:32 AM

    Originally posted by: KeerthivasanC


    I have the following output from a model I ran in docplex.cp:

    -------------------------------------------------------------------------------
    Model constraints: 9, variables: integer: 20, interval: 0, sequence: 0
    Solve status: Feasible, Fail status: SearchHasNotFailed
    Search status: SearchCompleted, stop cause: SearchHasNotBeenStopped
    Solve time: 0.02 sec
    -------------------------------------------------------------------------------
    
    TP1_0: 0
    TP1_1: 2
    TP1_2: 0
    TP1_3: 0
    TP1_4: 0
    TP2_0: 0
    TP2_1: 2
    TP2_2: 0
    TP2_3: 2
    TP2_4: 0
    TP3_0: 3
    TP3_1: 0
    TP3_2: 2
    TP3_3: 0
    TP3_4: 1
    TP4_0: 3
    TP4_1: 0
    TP4_2: 3
    TP4_3: 0
    TP4_4: 0
    

     

    I want to transform this output into a dataframe like this:

    TP1 TP2 TP3 TP4
    [TP1_0,TP1_1,TP1_2,TP1_3,TP1_4] [TP2_0,TP2_1,TP2_2,TP2_3,TP2_4] [TP3_0,TP3_1,TP3_2,TP3_3,TP3_4] [TP4_0,TP4_1,TP4_2,TP4_3,TP4_4]

    Each row will be the list of values corresponding to TPi. A code snippet which does something like this would be great because I am unable to find any resources to manipulate the result of the CP solver in python to create dataframes.

     

    Thanks.


    #CPOptimizer
    #DecisionOptimization


  • 2.  Re: Transforming model's result into a pandas dataframe

    Posted Tue April 07, 2020 10:22 AM

    Hi,

    let me use the zoo example as a starting point

     

    https://www.linkedin.com/pulse/making-optimization-simple-python-alex-fleischer/

    from docplex.cp.model import CpoModel

    mdl = CpoModel(name='buses')

    nbbus40 = mdl.integer_var(0,6,name='nbBus40')
    nbbus30 = mdl.integer_var(0,6,name='nbBus30')
    cost= mdl.integer_var(0,1000000,name='cost')

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

    siter = mdl.start_search(SearchType='DepthFirst', Workers=1, TimeLimit=100)
    # Parameters needed to avoid duplicate solutions

    from pandas import *
    dfsol=pandas.DataFrame()

    for msol in siter:
        print(msol[nbbus40]," buses 40 seats")
        print(msol[nbbus30]," buses 30 seats")
        print("cost = ",msol[cost])
        dfsol=concat([dfsol,DataFrame([msol[nbbus40],msol[nbbus30],msol[cost]])],axis=1)
        print("\n")
    dfsol.columns=["nbBus40","nbBus30","cost"]

    print(dfsol)   

     

    gives

       nbBus40  nbBus30  cost
    0        3        4     6
    1        6        5     2
    2     3900     4000  3800

     


    #CPOptimizer
    #DecisionOptimization


  • 3.  Re: Transforming model's result into a pandas dataframe

    Posted Tue April 07, 2020 11:08 AM

    Originally posted by: KeerthivasanC


    Thanks a lot for your help!


    #CPOptimizer
    #DecisionOptimization


  • 4.  Re: Transforming model's result into a pandas dataframe

    Posted Tue April 07, 2020 12:11 PM

    Originally posted by: ChrisBr


    Hello,

    In addition to Alex's answer, I would suggest you to have a look at DOcplex.CP: Constraint Programming Modeling for Python documentation, and especially the part about how to retrieve results.

    Regards,

    Chris.


    #CPOptimizer
    #DecisionOptimization