Search code examples
cplexopl

Is it possible to modify a range variable?


I need to solve a model in which I have an array with a size of 1..118 but I only want to solve it for some values (e.g. 1..9,11..60,62..115 and 117..118). The numbers that I excluded are the ones that I don't want to solve the problem for, because they will return a solution with no value and therefore are not of interest to me.

It's not possible for me to change the original array since I'll be using it to solve the relaxed version of the problem with the positions not taken before.

Is there any possible way of doing this?


Solution

  • let me give you an example on how to set a range through flow control in OPL:

    You have a first model sub.mod

    int minOfx = ...;
    int maxOfx = ...;
    range r=minOfx..maxOfx;
    dvar float x1 in r;
    dvar float x2 in r;
    
    maximize x2-x1;
    subject to {
    
    }
    
    execute
    {
    writeln("x2-x1= ",x2-x1);
    }
    

    and then you have your main model

    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.minOfx=(k-1)*(k-1);
      data2.maxOfx=k*k;
      opl.addDataSource(data2);
      opl.generate();
    
      if (cplex.solve()) {  
         opl.postProcess();
    
      } else {
         writeln("No solution");
      }
     opl.end();
    }  
    
    }
    

    When you run your main model you'll get

    x2-x1= 1
    x2-x1= 3
    x2-x1= 5
    x2-x1= 7
    x2-x1= 9
    x2-x1= 11
    x2-x1= 13
    x2-x1= 15
    x2-x1= 17
    x2-x1= 19
    

    Using flow control you can change some data in a model and that can change a range.