Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  How to solve the same model with different data ? Change an array and generate again

    Posted 12/13/16 05:21 AM

    Hi

    sub.mod

    int y[1..2][1..2]=...;

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

    dvar float x;

    maximize x;
    subject to {
      x<=sum(i in 1..2, j  in 1..2) y[i][j];
    }

    then in a an other .mod file

    int a[1..2][1..2];

    main {
      var source = new IloOplModelSource("sub.mod");
      var cplex = new IloCplex();
      var def = new IloOplModelDefinition(source);
     
     
     
      for(var k=11;k<=15;k++)
      {
      var opl = new IloOplModel(def,cplex);
     
     
        
      var data2= new IloOplDataElements();
     
      data2.y=thisOplModel.a;
      data2.y[1][1]=k;
      opl.addDataSource(data2);
     
      opl.generate();

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

    gives

    y= [[11 0]
             [0 0]]
    OBJ = 11
    y= [[12 0]
             [0 0]]
    OBJ = 12
    y= [[13 0]
             [0 0]]
    OBJ = 13
    y= [[14 0]
             [0 0]]
    OBJ = 14
    y= [[15 0]
             [0 0]]
    OBJ = 15

    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
    #OPLusingCPLEXOptimizer


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

    Posted 02/24/17 01:48 PM

    Originally posted by: ShafiAhad


    Hello Alex,

     

    Thanks for your reply. 

    But when I am trying to run this I am getting following error "Scripting runtime error: Element "a" does not exist in OPL model.."

    And this is understandable because, int a[1..2][1..2]=...; is outside of main{} and it won't recognize it. can you please suggest any solution??

    I am attaching the .mod an d.dat file.

    Thanks in advance..!!


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


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

    Posted 02/24/17 03:33 PM

    Hi

    oplrun main.mod works

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


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

    Posted 02/28/17 09:30 PM

    Originally posted by: ShafiAhad


    Hello Mr. Alex,

     

    This really helps. Thanks a lot. 

    And just a note that I am trying to learn and use OPL to solve a big complex problem. I might contact you in future as well. Hope I'll get help from you. I really appreciate your help.

     

    Thanks again.

     

    Shafi


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


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

    Posted 03/08/17 08:06 PM

    Originally posted by: ShafiAhad


    Hello Mr. Alex,

     

    I am trying to solve a model in iteration using flow control (main). I can solve the problem without issue but the problem I am facing is displaying the values of decision variable.

    the command "writeln("OBJ = ",cplex.getObjValue());" displays the value of objective function. 

    but when I try to print value of decision variable it is showing error. for example if I have a decision variable 'x' and i write" writeln("X= ",x);" it is showing "Scripting runtime error: unknown variable 'x'."

    can you please help me with the right function to write the value of decision variables? 

    Thanks in advance.


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


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

    Posted 03/08/17 09:25 PM

    Originally posted by: ShafiAhad


    Hello Mr. Alex,

     

    I am trying to solve a model in iteration using flow control (main). I can solve the problem without issue but the problem I am facing is displaying the values of decision variable.

    the command "writeln("OBJ = ",cplex.getObjValue());" displays the value of objective function. 

    but when I try to print value of decision variable it is showing error. for example if I have a decision variable 'x' and i write" writeln("X= ",x);" it is showing "Scripting runtime error: unknown variable 'x'."

    can you please help me with the right function to write the value of decision variables? 

    Thanks in advance.


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


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

    Posted 03/09/17 02:14 AM

    Hi,

    ig you go back to the example I wrote,

    writeln("x in the main = ",opl.x);

    would do the job.

    If I call

     

        int a[1..2][1..2];

        main {
          var source = new IloOplModelSource("sub.mod");
          var cplex = new IloCplex();
          var def = new IloOplModelDefinition(source);
         
         
         
          for(var k=11;k<=15;k++)
          {
          var opl = new IloOplModel(def,cplex);
         
         
            
          var data2= new IloOplDataElements();
         
          data2.y=thisOplModel.a;
          data2.y[1][1]=k;
          opl.addDataSource(data2);
         
          opl.generate();

          if (cplex.solve()) {
             writeln("OBJ = " + cplex.getObjValue());
             writeln("x in the main = ",opl.x);
          } else {
             writeln("No solution");
          }
          data2.end();
         opl.end();
         
         
        }  
         
        }

    then I get

    y= [[11 0]
             [0 0]]
    OBJ = 11
    x in the main = 11
    y= [[12 0]
             [0 0]]
    OBJ = 12
    x in the main = 12
    y= [[13 0]
             [0 0]]
    OBJ = 13
    x in the main = 13
    y= [[14 0]
             [0 0]]
    OBJ = 14
    x in the main = 14
    y= [[15 0]
             [0 0]]
    OBJ = 15
    x in the main = 15

    regards

     

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


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


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

    Posted 03/09/17 12:54 PM

    Originally posted by: ShafiAhad


    Thank you very much. Really appreciate it.


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


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

    Posted 03/31/17 02:25 AM

    Originally posted by: ShafiAhad


    Hello Mr. Alex,

     

    Hope you are doing good. 

    to solve an OPL model I need to read data from a txt file within a main block and use that data to calculate some other parameter. I want to read the data in a loop (the data file are named in an orderly manner like data1.txt; data2.txt; data3.txt etc). I was trying to use below code but it's not working.

     

    execute READ_FROM_FILE {
        var f = new IloOplInputFile();
        f.open("data.txt");
        read(f, "data", data);
        f.close();
        writeln("DONE");
        writeln(data);
        
    }

     

    I would really appreciate your help. Thanks in advance


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


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