OPL model files
Model (.mod) files contain all your OPL statements. The data and the objective function are not mandatory and there may be more optional components, such as scripting statements, in a model file.
Model (.mod) files contain all your OPL statements. The data and the objective function are not mandatory and there may be more optional components, such as scripting statements. Note that you can also generate a model file in a compiled form (.opl) from the IDE for execution through the OPL interface libraries (see Generating a compiled model). The components of a model file are covered in the following sections.
Declarations of data
Data declarations allow you to name your data items so that you can reference them easily in your model. For example, if your data in a table defines the cost of shipping one unit of material from location i to location j, you might want to call your item of data costij where i=1, . . . , n and j=1, . . . , n and n is the number of locations in your model. You tell OPL that your model uses this data by declaring:
int n = . . . ;
float cost[1..n][1..n] = . . . ;
The . . . (ellipsis) means that the values for your table are located in a data file, which must be listed in the current project.
You could also list the data explicitly in the model file. However, it is recommended that you construct model files without specifying values for data so that you can later easily solve many instances of the same model by simply changing the data file. See also Run configurations.
Note that the int type declared
means that the numbers in the data file must be integers. If the numbers
in the data file are floating-point numbers, use the float type instead.
Declarations of decision variables
In an OPL context, as opposed to IBM ILOG Script and to the general programming context, variables are decision variables. Declarations of decision variables name, and give the type of, each variable in the model. For example, if you want to create a variable that equals the amount of material shipped from location i to location j, you can create a variable named shipij.
dvar float+ ship[1..n][1..n];
That statement declares an array of non-negative floating-point
variables. (That is what float+ means).
The dvar keyword indicates that you are
declaring a decision variable.
An objective function
The objective function is a function that you want to
optimize. This function must consist of variables and data that you
have declared earlier in the model file. The objective function is
introduced by either the minimize or the maximize keyword. For example,
minimize sum(i,j in 1..n) cost[i][j]*ship[i][j];
That statement indicates that you want to minimize the sum of the shipping costs for each origin-destination pair.
Constraints
Constraints indicate the conditions necessary for a feasible
solution to your model. You declare constraints within a subject to block. For example,
subject to {
forall(j in 1..n) sum(i in 1..n) ship[i][j] == demand[j];
}
That statement declares one set of constraints. There
is a constraint for each destination. (That is what the forall keyword indicates.) The constraint for each
destination states that the sum of material shipped to that destination
must equal the demand at that destination. The symbol == indicates equals within a constraint block. The
symbol <= indicates less than or equal
to. The symbol >= indicates greater than
or equal to.