Decision Optimization

Expand all | Collapse all

How to solve the same model with different data ? Change a value and generate again

ALEX FLEISCHER

ALEX FLEISCHERTue December 13, 2016 05:10 AM

Archive User

Archive UserWed May 31, 2017 10:13 AM

Archive User

Archive UserThu June 01, 2017 05:11 AM

ALEX FLEISCHER

ALEX FLEISCHERThu June 01, 2017 06:08 AM

ALEX FLEISCHER

ALEX FLEISCHERThu June 01, 2017 02:31 PM

ALEX FLEISCHER

ALEX FLEISCHERMon September 18, 2017 10:35 AM

ALEX FLEISCHER

ALEX FLEISCHERTue September 19, 2017 08:30 AM

ALEX FLEISCHER

ALEX FLEISCHERFri September 22, 2017 06:19 AM

Archive User

Archive UserTue September 26, 2017 11:09 AM

ALEX FLEISCHER

ALEX FLEISCHERTue September 26, 2017 12:17 PM

ALEX FLEISCHER

ALEX FLEISCHERFri September 29, 2017 06:23 AM

Archive User

Archive UserFri September 29, 2017 07:40 AM

ALEX FLEISCHER

ALEX FLEISCHERFri September 29, 2017 08:44 AM

  • 1.  How to solve the same model with different data ? Change a value and generate again

    Posted Tue December 13, 2016 05:10 AM

    sub.mod

    float maxOfx = ...;
    dvar float x;

    maximize x;
    subject to {
      x<=maxOfx;
    }

    execute
    {
    writeln("x= ",x);
    }

    and then

    main {
      var source = new IloOplModelSource("sub.mod");
      var cplex = new IloCplex();
      var def = new IloOplModelDefinition(source);
     
     
     
      for(var k=1;k<=10;k++)
      {
      var opl = new IloOplModel(def,cplex);
        
      var data2= new IloOplDataElements();
      data2.maxOfx=k;
      opl.addDataSource(data2);
      opl.generate();

      if (cplex.solve()) {  
         opl.postProcess();
         writeln("OBJ = " + cplex.getObjValue());
      } else {
         writeln("No solution");
      }
     opl.end();
    }  
     
    }

    gives

    x= 1
    OBJ = 1
    x= 2
    OBJ = 2
    x= 3
    OBJ = 3
    x= 4
    OBJ = 4
    x= 5
    OBJ = 5
    x= 6
    OBJ = 6
    x= 7
    OBJ = 7
    x= 8
    OBJ = 8
    x= 9
    OBJ = 9
    x= 10
    OBJ = 10

    regards



  • 2.  Re: How to solve the same model with different data ? Change a value and generate again

    Posted Wed November 01, 2017 12:32 PM

    Originally posted by: gustavgans123


    Hi Alex,

    thanks for this thread & the example. Unfortunately I am not able to adapt the example to my problem / model.

    I would like to use main {} to run my Model several times, each time with a different inputs for U[i in NW] = [x,x,x] each time, the obj. value and the values for my decision variables should be safed in an excel file.

    I need to change the input values by hand, as it is not something like x+i for each run. I would like to to 12 runs, which means 12 different input arrays of "U" and 12 different results.

    Could someone help to adapt the example to my model?

    All files attached. So far main.mod:

    main{

    var source = new IloOplModelSource("Opt_Final.mod");

    var cplex = new IloCplex();

    var def = new IloOplModelDefinition(source);

    var opl = new IloOplModel(def,cplex);

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

    opl.addDataSource(data);

    opl.generate();

    if (cplex.solve()) {

    curr = cplex.getObjValue();

    writeln();

    writeln("OBJECTIVE: ",curr);

    }

    else {

    writeln("No solution");

    }

    var data2 = new IloOplDataSource("Opt_data.dat");

    var opl2 = new IloOplModel(def,cplex);

    opl2.addDataSource(data2);

    opl2.dataElements.U.add(12,12,12); // new array of U for 2nd run

    opl2.generate();

    if (cplex.solve()) {

    curr = cplex.getObjValue();

    writeln();

    writeln("OBJECTIVE: ",curr);

    }

    else {

    writeln("No solution");

    }

    opl.end();

    opl2.end();

    data.end();

    def.end();

    cplex.end();

    source.end();

    }

    For e.g. 12 runs,  would I need to write the blocks 12 times?:  opl.2generate, opl3.generate,...,opl12.generate

    Or can I pre define the 12 input arrays somewhere and the model always runs with the next array.

     

    In short: run my model e.g. 12 times with different input values for U, each time saving the objective value and the values for the decision variables

    Any help is greatly appreciated!

    BR

    gustav

     

    ps: I checked the other related threads, but did not manage to adapt to my model...:/

    Attachment(s)

    docx
    data.docx   14 KB 1 version
    docx
    main_mod.docx   14 KB 1 version
    docx
    opt_model.docx   26 KB 1 version


  • 3.  Re: How to solve the same model with different data ? Change a value and generate again

    Posted Wed May 31, 2017 10:13 AM

    Originally posted by: mGlass


    Good afternoon,

     

    is the main{} part in sub.mod?

     

    If besides the objective function I want to keep information of my solution arrays... Can I do it?

    I'll explain better: my obj function is total cost in a year, but I also want to keep information regarding transportation costs, production costs, etc per month.

    I already have those arrays and can print them in excel when I only run the model once

     

    Thank you,

     



  • 4.  Re: How to solve the same model with different data ? Change a value and generate again

    Posted Mon May 14, 2018 04:38 AM

    Originally posted by: ShaCplex


    Hi sir,

    i have a model ;

     

    tuple MoneyExchange {
     string moneyExchange;
     float rate;
     float serviceCharge; 
     }
     {MoneyExchange}MoneyEX={<"AExchange",18.05,16>,<"BExchange",18.06,0>,<"CExchange",18.12,19>};

     

     

     dvar int amount ; 

     

    maximize amount;
     subject to {

     

    ct1:
    forall(m1,m2 in MoneyEX: m1.moneyExchange=="AExchange" && m1!=m2)
      amount*(m1.rate-m2.rate) - ((m1.serviceCharge*m1.rate - m2.serviceCharge*m2.rate))>= 0;


    execute{
    writeln("Solution status: "+cplex.getCplexStatus());

    writeln("Objective value: "+cplex.getBestObjValue());
    }

     

    i want to automate the solve for each of the m in MoneyEX. As highlighted above for the first solving, m1.moneyExchange=="AExchange" and then  m1.moneyExchange=="BExchange" and then  m1.moneyExchange=="CExchange" .

    How can I achieve this?

     

     

     

    regards,

    shahul

     



  • 5.  Re: How to solve the same model with different data ? Change a value and generate again

    Posted Fri September 22, 2017 06:19 AM

    Hi,

    1) What you did is "freeze" some decision variables. You could also have a look at warmstart.

    https://www.ibm.com/support/knowledgecenter/SSSA5P_12.7.1/ilog.odms.ide.help/OPL_Studio/opllanguser/topics/opl_languser_script_fc_default_initset.html

    2) It depends. Sometimes generation is tiny and sometimes it takes long and that is why sometimes rather than regenerating you could prefer to do some incremental changes. See https://www.ibm.com/developerworks/community/forums/html/topic?id=0289cdc7-a237-4442-91f3-9c8e2944a8a7&ps=25

    PS:

    Are you a student, a researcher or a business user ?



  • 6.  Re: How to solve the same model with different data ? Change a value and generate again



  • 7.  Re: How to solve the same model with different data ? Change a value and generate again

    Posted Tue September 26, 2017 12:17 PM

    Hi

    I modeled last period as a decision variable in order to avoid generating again but you could have this as data of course.

    And the title of this post was "solve again with regenerate " 

    regards 



  • 8.  Re: How to solve the same model with different data ? Change a value and generate again

    Posted Fri September 22, 2017 06:05 AM

    Originally posted by: felycite28


    Hello Alex ,

    I need your precious advice again . After solving the prroblem until the last period we defined  we get the initial result and I want to save them and use them next iteration . I did it as the following

    main
    {
    thisOplModel.generate();
    thisOplModel.lastPeriod.LB=2;
    thisOplModel.lastPeriod.UB=2;
    cplex.solve();

    //I want to use this initial result as input for the next iteration

    var cplex2=new IloCplex();
           var Mod2 = new IloOplModelSource("felycite.mod");
        var Mod2Def = new IloOplModelDefinition(Mod2);
          var oplmodel2 = new IloOplModel(Mod2Def, cplex2);
          var Mod2data = new IloOplDataSource("28.dat");
          oplmodel2.addDataSource(Mod2data);
          oplmodel2.generate();

    oplmodel2.x.UB = thisOplModel.x.solutionValue;
               oplmodel2.x.LB = thisOplModel.x.solutionValue;

     

    1)It seems it works but I could not be sureEyebrow how can we  save the initial result in a vector and print it to be sure that the model is working well,

    I am  sure you can suggest me a better way to set the initial solution as the input of next iteration .This was the first question

    2) It is general question , when we generate the model for the second or more time in the main block , does it affect the CPU time of the model in a bad way ?

    Thank you so much

     

     



  • 9.  Re: How to solve the same model with different data ? Change a value and generate again

    Posted Mon September 18, 2017 09:59 AM

    Originally posted by: felycite28


    Milliooon thanks Alex , I adapted your great example into my model . I just have two  questions;

    1)forall( p in Products, t in Periods ) (t>=lastPeriod+1) => (Inside[p][t]+Outside[p][t]+Inv[p][t]==0);

    For this line, do all the variables we summed have to be the same type and same indices ?

    2)  You did not apply any fixing procedure  from plan 1 and plan 2 for the bold values

    plan= [[ <10 0 0>  <1 149 50>  <0 0 0>]
             [ <0 20 0>  <0 300 100>  <0 0 0>]
             [ <50 0 0>  <65 135 100>  <0 0 0>]]
    plan= [[ <10 0 0>  <1 99 0>  <1 49 0>]
             [ <0 20 0>  <0 200 0>  <0 100 0>]
             [ <50 0 0>  <65 35 0>  <65 35 0>]]

    did they come out the same by chance ? or did you apply any fixing I did not realised ?

     

    Thank you in advance



  • 10.  Re: How to solve the same model with different data ? Change a value and generate again

    Posted Sun September 17, 2017 04:25 PM

    Originally posted by: felycite28


    Hello Alex ,

    I need your help ; I am trying to get good initial solution for my MIP . To do that , instead of solving the model for all time periods at once I  split the horizon into slices.

    For example ,instead of solving ten period original problem , first I am solving the first two periods and get the result of this small part.  Basically I should change the data of "period".

    I tried something similar as you did above but did not work . if it is wrong approach how can I do that easily? or By adding a constraint? Creating a sub problem(s) ? I would be very happy if you can give me an example? .

    Thank you so much in advance.

    int T=...;
    range t=1..T; //period information.

     main
    {
      var source = new IloOplModelSource("trial.mod");
      var cplex = new IloCplex();
      var def = new IloOplModelDefinition(source);

        for (var a = 1; a<=2; ++a)
        {
        var opl = new IloOplModel(def,cplex);
        var data2= new IloOplDataElements();
         data2.T=2;
          opl.addDataSource(data2);
          opl.generate();
              //thisOplModel.generate();
             //cplex.solve();
        if (cplex.solve())
          {
                     writeln("OBJ = " + cplex.getObjValue());
                        writeln("OBJ = " +thisOplModel.y);
                    
              }
                  else
              {
                     writeln("No solution");
                  }
       
         }



  • 11.  Re: How to solve the same model with different data ? Change a value and generate again



  • 12.  Re: How to solve the same model with different data ? Change a value and generate again

    Posted Fri September 29, 2017 05:55 AM

    Originally posted by: felycite28


    I will check it Alex , thanks .

    I tried to apply FR heuristic by only opl script so far but if we code the math.model and heuristic on Python and called cplex to solve it ,would this method solve the same data within  less CPU time ?

    Regards and Thanks



  • 13.  Re: How to solve the same model with different data ? Change a value and generate again

    Posted Mon May 14, 2018 08:24 AM

    Originally posted by: ShaCplex


    Thank you sir, that helps!!

     

     

     

     

     

    regards,

    shahul



  • 14.  Re: How to solve the same model with different data ? Change a value and generate again

    Posted Mon September 18, 2017 10:35 AM

    Hi

    1) You could write what you need

    forall( p in Products, t in Periods ) (t>=lastPeriod+1) => (Inside[p][t]+Outside[p][t]+Inv[p][t]==0);

    means that for t=3 all 3 decision variables Inside, Outside and Cost will be 0

    2)

    Indeed I have not done any fixing and this happened through warmstart

    But you could fix.

    For example, you could write for the main:

    main
    {
    thisOplModel.generate();
    thisOplModel.lastPeriod.LB=2;
    thisOplModel.lastPeriod.UB=2;
    cplex.solve();
    thisOplModel.postProcess();
    var frozen=thisOplModel.Outside["kluski"][2].solutionValue;
    writeln("freeze ",frozen," for kluski period 2 Outside");
    thisOplModel.lastPeriod.LB=3;
    thisOplModel.lastPeriod.UB=3;
    thisOplModel.Outside["kluski"][2].UB=frozen;
    thisOplModel.Outside["kluski"][2].LB=frozen;
    cplex.solve();
    thisOplModel.postProcess();


    }

    and then you would get

    plan= [[ <10 0 0>  <1 149 50>  <0 0 0>]
             [ <0 20 0>  <0 300 100>  <0 0 0>]
             [ <50 0 0>  <65 135 100>  <0 0 0>]]
    freeze 149 for kluski period 2 Outside
    plan= [[ <10 0 0>  <0 149 49>  <1 0 0>]
             [ <0 20 0>  <0 200 0>  <0 100 0>]
             [ <50 0 0>  <66 34 0>  <65 35 0>]]

    regards

     

     



  • 15.  Re: How to solve the same model with different data ? Change a value and generate again

    Posted Fri September 29, 2017 08:44 AM

    Hi,

    you could use Array from Javascript:

    range r1=1..2;
    range r2=1..3;

    dvar int x[r1][r2];

    subject to
    {
    forall(i in r1,j in r2) x[i][j]==i*j;
    }

    execute
    {
    writeln(x);
    }

    main
    {
    thisOplModel.generate();
    cplex.solve();
    thisOplModel.postProcess();
    var saveX=new Array(2);
    for(var i in thisOplModel.r1) saveX[i]=new Array(3);
    for(var i in thisOplModel.r1)
        for(var j in thisOplModel.r2)
            saveX[i][j]=thisOplModel.x[i][j].solutionValue;
    for(var i in thisOplModel.r1)
        for(var j in thisOplModel.r2)
            writeln(i,"*",j," ==> ",saveX[i][j]);        
            
    }

    gives

     [[1 2 3]
             [2 4 6]]
    1*1 ==> 1
    1*2 ==> 2
    1*3 ==> 3
    2*1 ==> 2
    2*2 ==> 4
    2*3 ==> 6

    regards



  • 16.  Re: How to solve the same model with different data ? Change a value and generate again

    Posted Thu June 01, 2017 05:11 AM

    Originally posted by: mGlass


     

    Good morning,

     

    I'm sorry but I'm still facing some problems. Goofy

     

    1. I have a sub.mod and I get my data from a .dat file. When I run the configuration with main.mod and no .dat file it says: Data element does not exist. When I add the .dat file it says the element is not defined (when it is)

     

    2.     If what I want to kep from my sub.mod is already an array , this should work, right?

    for  (var k=1;k<=10;k++) {
          for  (var t=1;t<=12;k++) {
               cost_transp[k][t]+=opl.cost_transp[k][t]();

            }
     }

     

    3. If I then want to print this in excel, can I use the same .dat file for main.mod as I used for sub.mod?

     

    Thank you SO very much!!

     

    mGlass

     



  • 17.  Re: How to solve the same model with different data ? Change a value and generate again

    Posted Wed May 31, 2017 10:48 AM

    Hi,

    in my example the main was not in the same .mod, I call it main.mod on my machine.

    Let me change a bit the model and main part in order to show you how you could manage:

    sub.mod

     

        float maxOfx = ...;
        dvar float x;

        maximize x;
        subject to {
          x<=maxOfx;
        }

        execute
        {
        writeln("x= ",x);
        }
        
        float  y=2*x;

    and then main.mod

     

        main {
          var source = new IloOplModelSource("sub.mod");
          var cplex = new IloCplex();
          var def = new IloOplModelDefinition(source);
         
         var arrayOfY=new Array(10);
         
          for(var k=1;k<=10;k++)
          {
          var opl = new IloOplModel(def,cplex);
            
          var data2= new IloOplDataElements();
          data2.maxOfx=k;
          opl.addDataSource(data2);
          opl.generate();

          if (cplex.solve()) {  
             opl.postProcess();
             writeln("OBJ = " + cplex.getObjValue());
             writeln("y=",opl.y);
             arrayOfY[k]=opl.y;
          } else {
             writeln("No solution");
          }
         opl.end();
        }  
        write("arrayOfY=");
         for(var k=1;k<=10;k++) write(arrayOfY[k]," ");
         writeln();
         
        }

     

    which gives

    x= 1
    OBJ = 1
    y=2
    x= 2
    OBJ = 2
    y=4
    x= 3
    OBJ = 3
    y=6
    x= 4
    OBJ = 4
    y=8
    x= 5
    OBJ = 5
    y=10
    x= 6
    OBJ = 6
    y=12
    x= 7
    OBJ = 7
    y=14
    x= 8
    OBJ = 8
    y=16
    x= 9
    OBJ = 9
    y=18
    x= 10
    OBJ = 10
    y=20
    arrayOfY=2 4 6 8 10 12 14 16 18 20

    regards



  • 18.  Re: How to solve the same model with different data ? Change a value and generate again

    Posted Tue September 19, 2017 08:17 AM

    Originally posted by: felycite28


    Thank you very much Alex, you taught me many things again .

    I will modify my model according to the latest information .

    Million thanks .
     



  • 19.  Re: How to solve the same model with different data ? Change a value and generate again

    Posted Thu June 01, 2017 11:37 AM

    Originally posted by: mGlass


    Once again with questions 2 and 3:

     

    I've define t_returncosts_pt and the rest as new arrays on top and then it says: 
    Scripting runtime error: cannot add properties to this value, "null".    

     

    And now related to 3: I've tried to use Sheetwrite with the same .dat file and with another but couldn't do it with any of them.

    and lastly a very dumb question: should I be able to see a solution in the solution tab? because it's always blank although I know the model is iterating..

    Thanks once more.

     



  • 20.  Re: How to solve the same model with different data ? Change a value and generate again

    Posted Fri September 29, 2017 06:23 AM

    Hi,

    the solve time would be the same.

    But if you re more fluent with Python then I recommend the DOCPLEX Python API : https://developer.ibm.com/docloud/documentation/optimization-modeling/modeling-for-python/

    Open debate at https://www.linkedin.com/pulse/optimization-aka-prescriptive-analytics-should-we-write-fleischer/

    regards



  • 21.  Re: How to solve the same model with different data ? Change a value and generate again

    Posted Tue September 26, 2017 11:09 AM

    Originally posted by: felycite28


    Thanks Alex ,

    I am a bit stressed out Phd student , I have one last question about this topic .

    You directly introduced "last period" as variable but isn't  it possible to introduce it as data , any appropriate form, which will increment one by one at each iteration ?

    We thought it but could not find a easy way to do it ,if you have any idea , it would contribute to our progress?

    Thanks in advance

     

     

     



  • 22.  Re: How to solve the same model with different data ? Change a value and generate again

    Posted Fri September 29, 2017 07:40 AM

    Originally posted by: felycite28


    Thanks Alex ,

    I checked the link you sent but I decided do deal with the case without regenerating second model ; let me ask my question in a more basic way . I have

    main
    {
    thisOplModel.generate();
    thisOplModel.lastPeriod.LB=2;
    thisOplModel.lastPeriod.UB=2;
    cplex.solve();

    }

    how can I save this solution in an array which is like

    v=[[0 1]

        [1 0]

        [1 1]]

    if I have 3 vehicle , and 2 periods as we set as "last period" .



  • 23.  Re: How to solve the same model with different data ? Change a value and generate again

    Posted Mon September 18, 2017 04:01 AM

    Hi,

    within https://www.linkedin.com/pulse/how-opl-alex-fleischer

    let me show you an example without regeneration:

    I slightly modified the example mulprod  (CPLEX_Studio1271\opl\examples\opl\mulprod)

    I solve for period 1 and 2 and then add period 3

    .mod

    {string} Products = ...;
    {string} Resources = ...;
    int NbPeriods = ...;
    range Periods = 1..NbPeriods;

    float Consumption[Resources][Products] = ...;
    float Capacity[Resources] = ...;
    float Demand[Products][Periods] = ...;
    float InsideCost[Products] = ...;
    float OutsideCost[Products]  = ...;
    float Inventory[Products]  = ...;
    float InvCost[Products]  = ...;
    range Periods0 = 0..NbPeriods;

    dvar int+ Inside[Products][Periods];
    dvar int+ Outside[Products][Periods];
    dvar int+ Inv[Products][Periods0];

    dvar int lastPeriod; // last period to compute

    minimize
      sum( p in Products, t in Periods )
          (InsideCost[p]*Inside[p][t] +
           OutsideCost[p]*Outside[p][t] +
           InvCost[p]*Inv[p][t]);

    subject to {

    forall( p in Products, t in Periods ) (t>=lastPeriod+1) => (Inside[p][t]+Outside[p][t]+Inv[p][t]==0);

      forall( r in Resources, t in Periods )
        ctCapacity:
          sum( p in Products )
            Consumption[r][p] * Inside[p][t] <= Capacity[r];
      forall( p in Products , t in Periods )
        ctDemand:
          Inv[p][t-1] + Inside[p][t] + Outside[p][t] == Demand[p][t] + Inv[p][t];
      forall( p in Products )
        ctInventory:
          Inv[p][0] == Inventory[p];
    };
    tuple plan {
       float inside;
       float outside;
       float inv;
    }
    plan Plan[p in Products][t in Periods] = <Inside[p,t],Outside[p,t],Inv[p,t]>;
    execute DISPLAY {
      writeln("plan=",Plan);
    }


    tuple InsideSolutionT{
        string Products;
        int Periods;
        float value;
    };
    {InsideSolutionT} InsideSolution = {<i0,i1,Inside[i0][i1]> | i0 in Products,i1 in Periods};
    tuple OutsideSolutionT{
        string Products;
        int Periods;
        float value;
    };
    {OutsideSolutionT} OutsideSolution = {<i0,i1,Outside[i0][i1]> | i0 in Products,i1 in Periods};
    tuple InvSolutionT{
        string Products;
        int Periods0;
        float value;
    };
    {InvSolutionT} InvSolution = {<i0,i1,Inv[i0][i1]> | i0 in Products,i1 in Periods0};


    main
    {
    thisOplModel.generate();
    thisOplModel.lastPeriod.LB=2;
    thisOplModel.lastPeriod.UB=2;
    cplex.solve();
    thisOplModel.postProcess();
    thisOplModel.lastPeriod.LB=3;
    thisOplModel.lastPeriod.UB=3;
    cplex.solve();
    thisOplModel.postProcess();


    }

    .dat

    Products =  { kluski capellini fettuccine };
    Resources = { flour eggs };
    NbPeriods = 3;

    Consumption = [
                    [ 0.5, 0.4, 0.3 ],
                    [ 0.2, 0.4, 0.6 ]
                  ];
    Capacity = [ 20, 40 ];
    Demand = [
               [ 10 100 50 ]
               [ 20 200 100]
               [ 50 100 100]
             ];
    Inventory = [ 0 0 0];
    InvCost = [ 0.1 0.2 0.1];  
    InsideCost = [ 0.4, 0.6, 0.1 ];
    OutsideCost  = [ 0.8, 0.9, 0.4 ];

    which gives

    plan= [[ <10 0 0>  <1 149 50>  <0 0 0>]
             [ <0 20 0>  <0 300 100>  <0 0 0>]
             [ <50 0 0>  <65 135 100>  <0 0 0>]]
    plan= [[ <10 0 0>  <1 99 0>  <1 49 0>]
             [ <0 20 0>  <0 200 0>  <0 100 0>]
             [ <50 0 0>  <65 35 0>  <65 35 0>]]

    regards



  • 24.  Re: How to solve the same model with different data ? Change a value and generate again

    Posted Thu June 01, 2017 02:31 PM
      |   view attached

    Hi

    for (var j = 1;j <= data2.nplants;j++){        
            thisOplModel.t_returncosts_pt[j][t]+=opl.returncosts[j][t]
    //        t_returncosts[j][t]+=opl.returncosts[j][t]
    //        t_transfercosts[k][t]+= opl.transfercosts[k][t]
    //        t_suppliercosts[k][t]+= opl.suppliercosts[k][t]
    //        t_suppliercosts[k][t]+= opl.suppliercosts[k][t]
          }

    will work if you add

    float t_returncosts_pt [1..153][1..2];

    Let me attach the main.mod that will make work you example at https://www.ibm.com/developerworks/community/forums/html/topic?id=4e9adeca-5cf3-4ce1-8e17-e8c2e905bfc5&ps=25https://www.ibm.com/developerworks/community/forums/html/topic?id=4e9adeca-5cf3-4ce1-8e17-e8c2e905bfc5&ps=25

     

    Regards

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

    Attachment(s)

    mod
    main.mod   1 KB 1 version


  • 25.  Re: How to solve the same model with different data ? Change a value and generate again

    Posted Wed November 01, 2017 04:27 PM

    Originally posted by: gustavgans123


    thanks a lot alex! i will try to apply that first thing in the morning!! 

    Thank you!



  • 26.  Re: How to solve the same model with different data ? Change a value and generate again

    Posted Mon May 14, 2018 05:00 AM

    Hi,

    again the same idea and let me change a bit the initial example I provided:

    sub.mod

    string param=...;

    execute
    {
    writeln("param=",param);
    }

    int v[i in 1..4]=i;
    string prop[i in 1..4]=(v[i] mod 2==0)?"even":"odd";
    dvar float x;

    maximize x;
    subject to {
          x<=sum(i in 1..4:prop[i]==param) v[i];
    }

        execute
        {
        writeln("x= ",x);
        }
     

    and then

     

        main {
          var source = new IloOplModelSource("sub.mod");
          var cplex = new IloCplex();
          var def = new IloOplModelDefinition(source);
         
         
         
          for(var k=1;k<=2;k++)
          {
          writeln("k=",k);      
          
          var opl = new IloOplModel(def,cplex);
            
          var data2= new IloOplDataElements();
          if (k==1) data2.param="odd";
          else data2.param="even";
          opl.addDataSource(data2);
          opl.generate();

          if (cplex.solve()) {  
             opl.postProcess();
             writeln("OBJ = " + cplex.getObjValue());
          } else {
             writeln("No solution");
          }
          writeln();
         opl.end();
        }  
         
        }

     

    gives

    k=1
    param=odd
    x= 4
    OBJ = 4

    k=2
    param=even
    x= 6
    OBJ = 6

     

    regards



  • 27.  Re: How to solve the same model with different data ? Change a value and generate again

    Posted Thu June 01, 2017 06:08 AM

    Hi,

    do not hesitate to start a new thread in the forum.

    1 - post your model and dat files in that new thread

    2 - yes

    3 - I guess and you should use SheetWrite

    regards