Write in and read from the data files

178 Views Asked by At

I have an array as shown below, I want to write it's values in a data file as shown in below, Then read it's values again in another array from the same data file in the same model, So how to read it again from the same data file to same model file with main if possible.

range Number= 0..6;
int Example [i in Number]=i;
  
  execute
 {
  var o=new IloOplOutputFile("Example.dat");
  o.writeln("Example=");
  o.writeln(Example);
  o.writeln(";");
  o.close();
  }

1

There are 1 best solutions below

0
Alex Fleischer On

You could use IloOplInputFile

The example from documentation is

execute {
   var f = new IloOplInputFile("output.txt");
   if (f.exists) {
     writeln("the file output.txt exists");
     var s;
     while (!f.eof) {
      s=f.readline();
      writeln(s);
     }
     f.close();
   } else { 
     writeln("the file output.txt doesn't exist");
   }
}

or since the .dat you have is a .dat well formated file you can use that file directly with

main {
  var source = new IloOplModelSource("Example.mod");
  var cplex = new IloCplex();
  var def = new IloOplModelDefinition(source);
  var opl = new IloOplModel(def,cplex);
  var data = new IloOplDataSource("Example.dat");
  opl.addDataSource(data);
  opl.generate();
  if (cplex.solve()) {
     writeln("OBJ = " + cplex.getObjValue());

  } else {
     writeln("No solution");
  }
  }