Purpose of the preprocessing scripts

Explains how to initialize an array, set a CPLEX parameter, set an OPL option, and set the display of variables.

In this model, preprocessing scripts are used for:

Initializing an array

The recommended syntax to initialize arrays is via generic indexed arrays, as shown in the following code extract, which sets up a cost array for the routes.

Generic indexed arrays, an example (transp4.mod)


float Cost[Routes] = [ <t.p,<t.o,t.d>>:t.cost | t in TableRoutes ];

This cost array is used in the objective, which aims to minimize the sum of transportation costs along all routes. See also As generic indexed arrays in the Language Reference Manual.

However, you can also use a preprocessing execute block as shown in the following code extract, which contains a script named INITIALIZE.

Preprocessing script: initializing an array (transp4.mod)

float Cost[Routes];
execute INITIALIZE {
  for( var t in TableRoutes ) {
      Cost[Routes.get(t.p,Connections.get(t.o,t.d))] = t.cost;
   }
}

Setting a CPLEX parameter

The script named PARAMS sets a CPLEX parameter for the algorithm.

Preprocessing script: setting a CPLEX parameter  (transp4.mod)

execute PARAMS {
  cplex.tilim = 100;
}

This parameter sets a time limit on each call to the optimizer. See Changing CPLEX parameters in the Language User’s Manual.

Setting an OPL option

The script statement named SETTINGS sets the display of the component name on 40 characters.

Preprocessing script: setting an OPL option  (transp4.mod)

execute SETTINGS {
  settings.displayComponentName = true;
  settings.displayWidth = 40;
  writeln("Routes: ",Routes);
}

Setting the display of variables

The script statement named DISPLAY displays the routes in the Routes tuple set in the format : product: origin --> destination.

The DISPLAY script uses a function to do this. You can see this display of the routes in the Scripting Log tab.

Preprocessing script: displaying variables (transp4.mod)

execute DISPLAY {
  function printRoute(r) {
    write("  ",r.p,":");
    writeln(r.e.o,"->",r.e.d);
  }

  writeln("Routes:");
  for (var r in Routes) {
    printRoute(r);
  }
}