Decision Optimization

Decision Optimization

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

 View Only
  • 1.  Very simple OPL : call OPL from Python

    Posted Fri September 28, 2018 05:07 AM

    Hi,

    doopl helps call OPL CPLEX from python https://www.linkedin.com/feed/update/urn:li:activity:6450620726520139776

    Let me give you a tiny example out of https://www.linkedin.com/pulse/what-optimization-how-can-help-you-do-more-less-zoo-buses-fleischer/

     

    You may write zootupleset.mod

    int nbKids=300;

    // a tuple is like a struct in C, a class in C++ or a record in Pascal
    tuple bus
    {
    key int nbSeats;
    float cost;
    }

    // This is a tuple set
    {bus} buses=...;

    // asserts help make sure data is fine
    assert forall(b in buses) b.nbSeats>0;
    assert forall(b in buses) b.cost>0;

    // decision variable array
    dvar int+ nbBus[buses];

    // objective
    minimize
     sum(b in buses) b.cost*nbBus[b];
     
    // constraints
    subject to
    {
     sum(b in buses) b.nbSeats*nbBus[b]>=nbKids;
    }

    tuple solution
    {
      int nbBus;
      int sizeBus;
    }

    {solution} solutions={<nbBus[b],b.nbSeats> | b in buses};

     

    and then the following python code

    from doopl.factory import *

    # Data

    Buses=[
        (40,500),
        (30,400)
        ]

    # Create an OPL model from a .mod file
    with create_opl_model(model="zootupleset.mod") as opl:
        # tuple can be a list of tuples, a pandas dataframe...
        opl.set_input("buses", Buses)

        # Generate the problem and solve it.
        opl.run()

        # Get the names of post processing tables
        print("Table names are: "+ str(opl.output_table_names))

        # Get all the post processing tables as dataframes.
        for name, table in iteritems(opl.report):
            print("Table : " + name)
            for t in table.itertuples(index=False):
                print(t)

            # nicer display
            for t in table.itertuples(index=False):
                print(t[0]," buses ",t[1], "seats")

     

    gives

     

    Table names are: ['solutions']
    Table : solutions
    Pandas(nbBus=6, sizeBus=40)
    Pandas(nbBus=2, sizeBus=30)
    6  buses  40 seats
    2  buses  30 seats

    regards

     

    NB:

    You may download doopl at https://pypi.org/project/doopl/

    You may also translate OPL to Python https://www.ibm.com/developerworks/community/forums/html/topic?id=a8580237-639a-411b-b1df-f390a24da06c&ps=25

     

     

     

     

     

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: Very simple OPL : call OPL from Python

    Posted Fri November 15, 2019 03:16 AM

    Originally posted by: qtbgo


    Can we setObjCoef in doopl?


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: Very simple OPL : call OPL from Python

    Posted Fri November 15, 2019 08:57 AM

    Hi,

    you cannot yet call setObjCoef in doopl and do not hesitate to log a wish in Aha if you need that

    https://ibm-data-and-ai.ideas.aha.io/?project=CPLEX

    But what you can do is use a main in OPL and call that from doopl.

    Let me show you.

    zootupleset.mod

    int nbKids=300;

        // a tuple is like a struct in C, a class in C++ or a record in Pascal
        tuple bus
        {
        key int nbSeats;
        float cost;
        }

        // This is a tuple set
        {bus} buses=...; //{<40,500>,<30,400>};

        // asserts help make sure data is fine
        assert forall(b in buses) b.nbSeats>0;
        assert forall(b in buses) b.cost>0;

        // decision variable array
        dvar int+ nbBus[buses];

        // objective
        minimize
         sum(b in buses) b.cost*nbBus[b];
         
        // constraints
        subject to
        {
         sum(b in buses) b.nbSeats*nbBus[b]>=nbKids;
        }

        tuple solution
        {
          int nbBus;
          int sizeBus;
        }

        {solution} solutions={<nbBus[b],b.nbSeats> | b in buses};
        
        main
        {
        thisOplModel.generate();
        cplex.solve();
        var b1=Opl.first(thisOplModel.buses);
        cplex.setObjCoef(thisOplModel.nbBus[b1], 200);
        cplex.solve();
        thisOplModel.postProcess();    
        }

     

    and then the following python code

    from doopl.factory import *

    # Data

    Buses=[
        (40,500),
        (30,400)
        ]

    # Create an OPL model from a .mod file
    with create_opl_model(model="zootupleset.mod") as opl:
        # tuple can be a list of tuples, a pandas dataframe...
        opl.set_input("buses", Buses)

        # Generate the problem and solve it.
        opl.run()

        # Get the names of post processing tables
        print("Table names are: "+ str(opl.output_table_names))

        # Get all the post processing tables as dataframes.
        for name, table in iteritems(opl.report):
            print("Table : " + name)
            for t in table.itertuples(index=False):
                print(t)

            # nicer display
            for t in table.itertuples(index=False):
                print(t[0]," buses ",t[1], "seats")

    will give

     

    8  buses  40 seats
    0  buses  30 seats

    regards

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer