Decision Optimization

Decision Optimization

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

 View Only
  • 1.  Python doopl setStartingPoint for CPO interval variable

    Posted 06/11/20 08:51 AM
    Hello,
    I am trying to warmstart a CPOptimizer model with the value of an interval decision variable from python. 
    Is there a way to use setStartingPoint using the doopl interface? I do set_input for Tuples today. 
    I have searched the forum for prior posts on this topic but don't seem to find one. 
    Any help on this one is much appreciated!
    Thanks in advance,
    Bala

    ------------------------------
    Balakrishnan Ananthanarayanan
    ------------------------------

    #DecisionOptimization


  • 2.  RE: Python doopl setStartingPoint for CPO interval variable
    Best Answer

    Posted 06/11/20 01:00 PM
    Edited by System Admin 01/20/23 04:49 PM
    Hi,

    see example in OPL

    https://community.ibm.com/community/user/datascience/communities/community-home/digestviewer/viewthread?MessageKey=2e7e9b61-1183-48c9-be12-2dfd33e2ef09&CommunityKey=ab7de0fd-6f43-47a9-8261-33578a231bb7&tab=digestviewer#bm2e7e9b61-1183-48c9-be12-2dfd33e2ef09

    With doopl you can do the same

    if you write warmstartcposchedulingdoopl
    using CP;
    
    tuple t
    {
      int v;
    }
    
    {t} s=...;
    
    {int} mySet={ i.v | i in s};
    
    range r = 1..10;
    dvar interval x[r] size 1;
    
    dvar interval y[r] size 1;
    // The following array of values (defined as data) will be used as  
    // a starting solution to warm-start the CP Optimizer search.
    
    int values[i in r] = (i in mySet)? 10 : 0;     
    
    minimize sum( i in r ) startOf(x[i]) + sum( j in r ) startOf(y[j]);
    subject to
    {
    ctSum: sum( i in r ) startOf(x[i]) >= 10;
    forall( j in r ) ctEqual: startOf(y[j]) == j;
    }   
    
    execute
    {
    for(i in r) write(Opl.startOf(x[i])," ");
    writeln();
    }
    
    tuple t2
    {
      int a;
      int b;
    }
    
    {t2} result={<i,startOf(x[i])> | i in r};
    
    execute
    {
      result;
    }
    
    main
    {
    thisOplModel.generate();
    var sol=new IloOplCPSolution();
    
    for(var i=1;i<=10;i++) sol.setStart(thisOplModel.x[i],thisOplModel.values[i]);
    cp.solve();
    thisOplModel.postProcess();
    cp.setStartingPoint(sol);
    cp.solve();
    thisOplModel.postProcess();
    } ​

    then you can write the python file

    from doopl.factory import *
    
    # Create an OPL model from a .mod file
    with create_opl_model(model="warmstartcposchedulingdoopl.mod") as opl:
        # tuple can be a list of tuples, a pandas dataframe...
        s=[(5,),]
        opl.set_input("s",s )
    
        # 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)​

    which will give you

    Table names are: ['result']
    Table : result
    Pandas(a=1, b=0)
    Pandas(a=2, b=0)
    Pandas(a=3, b=0)
    Pandas(a=4, b=0)
    Pandas(a=5, b=10)
    Pandas(a=6, b=0)
    Pandas(a=7, b=0)
    Pandas(a=8, b=0)
    Pandas(a=9, b=0)
    Pandas(a=10, b=0)​


    regards



    ------------------------------
    ALEX FLEISCHER
    ------------------------------



  • 3.  RE: Python doopl setStartingPoint for CPO interval variable

    Posted 06/18/20 05:34 AM
    Thanks a lot Alex! This really helped!

    ------------------------------
    Balakrishnan Ananthanarayanan
    ------------------------------