A few tips on scripting
Comments on various characteristics of IBM® ILOG® Script for OPL of which you should be aware when writing script statements.
The OPL interpreter
The OPL interpreter performs the following tasks:
declarations: types, names for data and decision variables
instantiation
data sources: data given in files or other sources
preprocessing: scripting blocks
modeling: objective and constraints
postprocessing
Script variables
Script
variables declared in one execute block are not visible
in other execute blocks.
Processing order
After the declarations, all the data sources are processed. Preprocessing is done before modeling. Postprocessing is available after a solution is found. However, some postprocessing instructions are not triggered unless the postProcess method is explicitly called on the model object.
When the “Force element usage” option is turned off (the default value), all the declared elements are instantiated “on demand”, that is, when they are first used and the interpreter issues warnings for unused elements. When you turn this option on, all elements are used and no warning message is issued.
Data initialization
If you declare the data of your project internally in the model file (as opposed to externally in a data file), you cannot access it later by means of a script statement such as:
myData.myArray_inMod[1] = 2; Otherwise,
OPL throws an error message because data elements only hold external
data elements declared using the =... (ellipsis) syntax
and read from a .dat file or other data source.
Control on the solve operation
The solve operation is performed
by the flow control in a main block, via the oplrun command,
or in the IDE.
A specific API is provided to enable advanced users to control these tasks. Please refer to the oplrunsample.cpp example. This file is at the following location:
<Install_dir>\opl\examples\opl_interfaces\cpp\src
where <Install_dir> is
your installation directory.
Solutions after the solve operation
NumVar and NumExpr objects
are automatically converted to numbers after a solve, whatever the
context.
Ending objects
In preprocessing, postprocessing, or flow control context, you can end the OPL elements you don’t need to reduce overall memory consumption.
Debugging
To improve the response time of your script blocks, OPL provides the Profiler as a debugging tool. See Profiling the execution of a model for more information on how to identify the blocks that are good candidates for improvement.