Modifying data from “main” scripting

Data elements can be modified and then used as a data source for another model.

You can access and modify data in the data elements obtained from the current OPL model, as follows:


data.capacity["flour"] = capFlour;

Then, the value of the variable capacity["flour"] is modified in the structure of the data elements.

Note, however, the following restrictions:

  1. Scalar data, whether in the .mod file or the .dat file, cannot be modified via scripting.

  2. You can use data elements to add new elements, but only for scalar types.

  3. Only external data can be modified by script. If, in the .mod file, you have, for example:

    int arr[1..3] = [1,2,3];

    you cannot modify the array arr. You need to declare it as:

    int arr[1..3] = ...;

    and initialize it externally.

    So you would need to create a .dat file that contains the data to update, except for scalar data. The scalars that need to be updated would not be initialized in the .mod, or in a .dat, but in a new instance of OplDataElements that you can then add as a data source:

    float maxOfx = ...;
    .
    .
    main { 
    .
    .
      var data = new IloOplDataSource("basicmodel.dat");
      var opl = new IloOplModel(def,cplex);
      var data2 = new IloOplDataElements();
      data2.maxOfx=11;
      opl.addDataSource(data);
      opl.addDataSource(data2);
      opl.generate();
    .
    .
    }