Decision Optimization

Decision Optimization

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

 View Only
  • 1.  doOPL Flowcontrol

    Posted Sat December 01, 2018 06:46 PM

    Originally posted by: AndyHam


    (I don't think this forum is not for this topic, but I could not find the right forum. Any help will be appreciated. Thanks, Andy)

    Dear IBM,
    I have a flow control model for MIP-CP linking. The flowcontrol model works well within OPL environment. However, when I called the flowcontrol model via doOPL, it does not function. doOPL works well with stand-alone OPL models. The following is what I got when I call "main.py". Basically, it does not do anything. There are codes for main.py and flowcontrol.mod in this post. Would you please take a look at the codes and let me know what I have done wrong?


    C:\Users\Google Drive\Codes\OPL>main.py

    Tried aggregator 1 time.
    No LP presolve or aggregator reductions.
    Presolve time = 0.02 sec. (0.00 ticks)
    oplmodel:
         mod file: C:\Users\AndyHam\Google Drive\Codes\OPL\FlowControl.mod
         0 .dat files
         0 python structures

     

    //main.py

    from doopl.factory import *
    import os
    from os.path import dirname, abspath, join
    DIR_Model = join(dirname(abspath(__file__)), 'code')
    mod = join(DIR_Model,  "FlowControl.mod")
    with create_opl_model(model=mod) as opl:
        opl.run()
        print(opl)
     
     
    //"FlowControl.mod"
    main {
      var cp = new IloCP();
      var cplex = new IloCplex(); 
      var tLimit=5; 
      var model;
      var data1 = new IloOplDataSource("...");
     
      model="Stage1.mod";
      var source = new IloOplModelSource(model);
      var def = new IloOplModelDefinition(source);
      var opl = new IloOplModel(def,cplex); 
      opl.addDataSource(data1);    
      opl.generate();
      cplex.tilim=5;
      cplex.solve(); 
      opl.postProcess();
     
      model="Stage2.mod";
      var source2 = new IloOplModelSource(model);
      var def2 = new IloOplModelDefinition(source2);
      var opl2 = new IloOplModel(def2,cp); 
      opl2.addDataSource(data1);     
      var data0=new IloOplDataElements();
      data0.O2F=thisOplModel.O2F;
      for(var o in opl.OBC) 
        for (var f in opl.FF) 
          if(opl.bjobOnFF[o][f]==1) data0.O2F.add(o.id,f.id);     
      opl2.addDataSource(data0);   
      opl2.generate();
      cp.param.timelimit=tLimit;
      cp.param.NoOverlapInferenceLevel = "Extended";
      cp.param.Workers=3;   
      var f = cp.factory;       
      f.searchPhase(opl2.seqFF);  
      if (cp.solve()) opl2.postProcess();
    }

    #DecisionOptimization
    #OPLusingCPOptimizer


  • 2.  Re: doOPL Flowcontrol

    Posted Wed December 05, 2018 02:29 AM

    Originally posted by: VincentBeraudier


    This is a bug in doopl when using a .mod with a main.

    We will issue a fix very soon.

    I don't see any workaround.


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 3.  Re: doOPL Flowcontrol

    Posted Wed December 05, 2018 01:47 PM

    Originally posted by: AndyHam


    Thanks for checking out this issue. If you can provide me a rough timeline of the fix, it will be very appreciated.


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 4.  Re: doOPL Flowcontrol

    Posted Fri December 28, 2018 04:58 AM

    Hi,

    let me offer you a workaround in the meantime.

    From python you may call a .mod file without any main block that will call a .mod file with a main block through IloOplExec:

    Let me give you some details out of the example from https://www.linkedin.com/pulse/making-decision-optimization-simple-alex-fleischer/

     

    zoo.mod

    tuple bus
        {
        key int nbSeats;
        float cost;
        }

        tuple kid
        {
            string name;
            int nbKids;
        }

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

        // 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];
        dvar int nbKids;

        // 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
        {
            var f=new IloOplOutputFile("solution.txt");
            thisOplModel.generate();
            for(var scenari in thisOplModel.kids)
            {
                thisOplModel.nbKids.LB=scenari.nbKids;
                thisOplModel.nbKids.UB=scenari.nbKids;
                cplex.solve();
                f.writeln("scenarii = ",scenari.name);
                thisOplModel.postProcess();
                f.writeln(thisOplModel.solutions);
                
            }
            f.close();

     


        }    

     

    zoomain.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;
        }

        tuple kid
        {
            string name;
            int nbKids;
        }

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

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


        execute
        {
            var f=new IloOplOutputFile("zoo.dat");
            f.write("buses=");
            f.write(buses);
            f.writeln(";");
            f.write("kids=");
            f.write(kids);
            f.writeln(";");
            f.close();
            
            IloOplExec("C:\\ILOG\\CPLEX_Studio128\\opl\\bin\\x64_win64\\oplrun.exe c:/zoo.mod zoo.dat",true);
        }
        

        

     

    and then callmainbus.py

    from doopl.factory import *

    # Data

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

    Kids=[("scenario1",300),("scenario2",310)]

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

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

    when you run that Python program you get solution.txt

    scenarii = scenario1
     {<6 40> <2 30>}
    scenarii = scenario2
     {<7 40> <1 30>}

    regards

    Alex Fleischer

    PS:

    Many how to with OPL at https://www.linkedin.com/pulse/how-opl-alex-fleischer/

    Many examples from a very good book : https://www.linkedin.com/pulse/model-building-oplcplex-alex-fleischer/

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


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 5.  Re: doOPL Flowcontrol

    Posted Wed January 09, 2019 12:09 PM

    Originally posted by: AndyHam


    Thanks for the feedback.
    However, my client does not like the idea of using "IloOplExec".  They are waiting for doOPL fix. In fact, they plan to submit a formal request.


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 6.  Re: doOPL Flowcontrol

    Posted Wed January 09, 2019 12:12 PM

    Originally posted by: AndyHam


    My client is waiting for the doOPL fix. If you can provide me a rough timeline of the fix, it will be very appreciated. Thanks!


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 7.  Re: doOPL Flowcontrol

    Posted Wed January 09, 2019 12:22 PM

    Originally posted by: VincentBeraudier


    Can you try the new version that was uploaded few days ago on pypi and conda?


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 8.  Re: doOPL Flowcontrol

    Posted Wed January 09, 2019 01:04 PM

    Originally posted by: AndyHam


    I just tried doOPL version 18.1 after upgrading, but it is still not working. When I run FlowControl.mod on OPL environment, it works great. But, Py does not.

    ***********************************************

    /pip-18.1-py2.py3-none-any.whl (1.3MB)
        100% 1.3MB 5.1MB/s
    Installing collected packages: pip
      Found existing installation: pip 18.0
        Uninstalling pip-18.0:
          Successfully uninstalled pip-18.0
    Successfully installed pip-18.1

    C:\Users\Google Drive\Codes\OPL\main.py
    Tried aggregator 1 time.
    No LP presolve or aggregator reductions.
    Presolve time = 0.00 sec. (0.00 ticks)
    oplmodel:
         mod file: C:\Users\Google Drive\Codes\OPL\FlowControl.mod
         0 .dat files
         0 python structure
    ***********************************************


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 9.  Re: doOPL Flowcontrol

    Posted Wed January 16, 2019 05:50 AM

    Originally posted by: Viu-Long Kong


    Hi,

    I think you confused pip and doopl. The log fragment you pasted shows that you upgraded pip (the python package manager), but not doopl.

    Can you please try again after you update doopl with:

     

    pip install --upgrade doopl
    

     

    You can check the installed version with:

     

    pip feeze
    

    (then looking for the line with doopl== )

     

    Thanks


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 10.  Re: doOPL Flowcontrol

    Posted Wed January 16, 2019 08:29 AM

    Originally posted by: AndyHam


    It was my bad.  I did not upgrade doOPL as you found ^^.
    After upgrading doOPL, The flowControl for linking MIP and CP works great!
    Thanks a lot!

     

    Successfully installed doopl-12.8.0.8

    C:\Users\AndyHam\flowcontrol.py
    //MIP
    Total (root+branch&cut) =    0.16 sec. (0.40 ticks)
    maxjobCountOn  =      38
    minjobCountOn  =      12
    travelDistance  =      679
    //CP
     ! Search terminated by limit, 20 solutions found.
     ! Best objective         : 898 (gap is 7.35%)
     ! Total memory usage     : 9.2 MB (9.0 MB CP Optimizer + 0.3 MB Concert)
     ! Time spent in solve    : 5.02s (5.00s engine + 0.02s extraction)
     


    #DecisionOptimization
    #OPLusingCPOptimizer