Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  SheetWrite command in Flow Control

    Posted 07/04/17 05:12 PM

    Originally posted by: MJCplex


    Hi everyone,

     

    I have been using the forum for quite a while and found many great solutions to minor problems I have had - thanks for that already!

    But right now I got stuck on a problem that I can't find a solution to - so maybe you can help me with my first post:

     

    I have created a program with flow control using a main.mod and an additional loop.mod.

    Each iteration the loop.mod is solved and the found solution is added to a set of penalized solutions for the next iteration (so that the solution is more unlikely to be found again in the next iteration).

    Once a solution has been found a second time the program stops.

     

    Everything is running as expected as long as I only try to read from the excel-file for the initial parameters.

    Any attempts to write the found solutions to excel failed so far - the corresponding line in the .dat file is completly skipped.

    I also added .postProcess() after each .generate() in the loop, but the SheetWrite-command is still ignored.

    It does not even cause an error when I have the excel-file open while executing the program (which usually leads to the 'file is read-only' error).

     

    As the model itself got quite big by now I try to bring it down to the essential part :

    main.mod:

    //---------- Solving the base model without penalties ----------------------

           //Creating the model

           var defBase = thisOplModel.modelDefinition;

           var cplexBase = cplex;

           var oplBase = new IloOplModel(defBase,cplexBase);

          

           //Adding the data

           var dataBase = thisOplModel.dataElements;

           oplBase.addDataSource(dataBase);

          

           //Generating the model

           oplBase.generate();

           if (cplexBase.solve())

           {

                 oplBase.postProcess();

           }

           else

           {

           writeln("No Solution");

           }

    ....

    ....

    ....

    ....

    while(loopCondition == 0)

    {

          

           ObjValueStorage[loop] = cplexLoop.getObjValue();

           simulationObjective[loop] = ObjValueStorage[loop]*(0.4*(Opl.rand(101)/100));

          

           //add penalty from last iteration to new candidate solution

           dataLoop.P.add(loop, simulationObjective[loop]);

                       

           //add new candidate solution from last iteration

           for(var sNode = 1; sNode <= oplBase.maxNodes; sNode++)

           {

                        for(var tNode = 1; tNode <= oplBase.maxNodes; tNode++)

                        {

                               dataLoop.H.add(loop, sNode, tNode, oplLoop.x[sNode][tNode].solutionValue);                          

                        }

                       

           }

           dataLoop.r = loop;

           oplLoop = new IloOplModel(defLoop,cplexLoop);

           oplLoop.addDataSource(dataLoop);

           oplLoop.generate();

           if (cplexLoop.solve())

           {

                 oplLoop.postProcess();

           }

           else

           {

           writeln("No Solution");

           }

          

           for(var iteration = 1; iteration <= loop; iteration++)

           {

                 loopCondition = loopCondition + oplLoop.U[iteration].solutionValue;

           }     

    }

     

    .data:

    SheetConnection Import("Import.xlsx");

    SheetConnection Export("Export.xlsx");

    //Sets / ranges

     

    maxNodes from SheetRead(Import,"number_of_nodes");

     

    //Parameters

     

    w from SheetRead(Import,"Scaled_Flow"); //Flow originated at node i in N that is destined to node j in N.

     

    f from SheetRead(Import,"Fixed_Costs"); //Fixed setup cost for location a hub at node k in N.

     

    g from SheetRead(Import,"Link_Costs"); //Fixed cost for operating a hub link between hubs k in N and l in N.

            

    c from SheetRead(Import,"Costs");

     

    alpha from SheetRead(Import,"alpha"); //Economies of Scale discount factor for the flow transferred between hubs.

     

    O from SheetRead(Import,"Origin");

    D from SheetRead(Import,"Destination");

     

     

    //---------------------------  exporting data

    x to SheetWrite(Export,"SolutionValue");

     

    Any ideas?

    Thanks!

     

    Best greetings,

     

    MJ

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: SheetWrite command in Flow Control

    Posted 07/05/17 01:48 AM

    Hi,

    can you try to replace

    main
    {
    var defBase = thisOplModel.modelDefinition;

           var cplexBase = cplex;

           var oplBase = new IloOplModel(defBase,cplexBase);

          

           //Adding the data

           var dataBase = thisOplModel.dataElements;

           oplBase.addDataSource(dataBase);

          

           //Generating the model

           oplBase.generate();

           if (cplexBase.solve())

           {

                 oplBase.postProcess();

           }

           else

           {

           writeln("No Solution");

           }
    }

    by

    main
    {
    thisOplModel.generate();
    cplex.solve();
    thisOplModel.postProcess();
    }

    ?

    regards

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: SheetWrite command in Flow Control

    Posted 07/05/17 12:50 PM

    Or what you could also do is use an additional main.mod such as

    main
    {
    var source = new IloOplModelSource("model.mod");
    ;
      var defBase = new IloOplModelDefinition(source);


           var cplexBase = cplex;

           var oplBase = new IloOplModel(defBase,cplexBase);

          

           //Adding the data

           var dataBase = new IloOplDataSource("data.dat");

           oplBase.addDataSource(dataBase);

          

           //Generating the model

           oplBase.generate();

           if (cplexBase.solve())

           {

                 oplBase.postProcess();

           }

           else

           {

           writeln("No Solution");

           }
    }

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: SheetWrite command in Flow Control

    Posted 07/05/17 01:47 PM

    Originally posted by: MJCplex


    Hey Alex,

     

    thanks for your answer!

    Apparently replacing my rather complex initialization with your shorter one triggers the export to excel.

    Because I actually want to understand what's going on in my model before adapting all the remaining lines of code to the new construction: Do you have any idea why my initial formulation skips the export?

     

    Regarding your 2nd answer:

    To my understanding cplex only allows 1 main.mod per run configuration?

    Right now I use my main.mod for flow control and for the initial solution and then use another model.mod for the loop iterations.

     

    Maybe to rephrase it:

    Using a main.mod, a model.mod and one data.dat file, how can I create a loop where data is written to excel after each iteration, recalculated there and imported again for the next iteration (so that some values actually change in the progress)?

    I am right now working on a short and simplified version to demonstrate what I am thinking about.

     

    Thanks!

     

    Best regards,

     

    MJ


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 5.  Re: SheetWrite command in Flow Control

    Posted 07/05/17 03:43 PM

    Hi,

    "Do you have any idea why my initial formulation skips the export?"  ==> No, this looks strange.

    To my understanding cplex only allows 1 main.mod per run configuration?  ==> From another .mod you may call the main of another .mod , see the method main() in IloOplModel

    About getting a solution and using that as input you may have a look at

    https://www.ibm.com/developerworks/community/forums/html/topic?id=8d5bdc42-8b25-4e6c-928c-d0fba4862eee&ps=25

    and

    https://www.ibm.com/developerworks/community/forums/html/topic?id=092c58bb-7bc8-4121-b3d9-91c07424c586

    Plus some useful links at

    https://www.ibm.com/developerworks/community/forums/html/topic?id=0d0b2396-3b48-4638-b032-3b9ea74f1a11&ps=25

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer