Notes On Opl Script
Notes On Opl Script
Warning
No two execute blocks can have the same name within
the same model.
Any execute block placed before the objective or
constraints declaration is part of preprocessing; other
blocks are part of postprocessing.
The scripting context within an execute block
corresponds to the model declarations. You can think of the
statements within an execute block being embedded in
an ILOG Script block named with.
with (thisOPLModel) {
writeln("Hello World.");
}
where thisOPLModel is the instance of IloOplModel
representing the current OPL model.
Language User's Manual > The Application Areas > Linear
and Integer Programming > Linear Programming > A
Production Problem
PREVIOUS NEXT
A Production Problem
PREVIOUS
A Multi-Period Production Planning NEXT
Problem
Large linear-programming problems are often obtained
from simpler ones by generalizing them along one or more
dimensions. A typical extension of production-planning
problems is to consider several production periods and to
include inventories in the model. This section presents a
multiperiod production planning model that generalizes the
model of the previous section A Production Problem.
The main generalization is to consider the demand for the
products over several periods and to allow the company to
produce more than the demand in a given period. Of
course, there is an inventory cost associated with storing
the additional production. Code Sample 5.3 depicts the new
model and Code Sample 5.4 describes the instance data.
{string} Products = ...;
{string} Resources = ...;
int NbPeriods = ...;
range Periods = 1..NbPeriods;
float Consumption[Resources][Products] =
...;
float Capacity[Resources] = ...;
float Demand[Products][Periods] = ...;
float InsideCost[Products] = ...;
float OutsideCost[Products] = ...;
float Inventory[Products] = ...;
float InvCost[Products] = ...;
dvar float+ Inside[Products][Periods];
dvar float+ Outside[Products][Periods];
dvar float+ Inv[Products][0..NbPeriods];
minimize
sum( p in Products, t in Periods )
(InsideCost[p]*Inside[p][t] +
OutsideCost[p]*Outside[p][t] +
InvCost[p]*Inv[p][t]);
subject to {
forall( r in Resources, t in Periods )
ctCapacity:
sum( p in Products )
Consumption[r][p] * Inside[p][t]
<= Capacity[r];
forall( p in Products , t in Periods )
ctDemand:
Inv[p][t-1] + Inside[p][t] +
Outside[p][t] == Demand[p][t] + Inv[p]
[t];
forall( p in Products )
ctInventory:
Inv[p][0] == Inventory[p];
};
tuple plan {
float inside;
float outside;
float inv;
}
plan Plan[p in Products][t in Periods] =
<Inside[p,t],Outside[p,t],Inv[p,t]>;
execute DISPLAY {
writeln("plan=",Plan);
}
Code Sample 5.3 A Multi-Period Production-Planning
Problem (mulprod.mod)
Products = { kluski capellini fettucine
};
Resources = { flour eggs };
NbPeriods = 3;
Consumption = [
[ 0.5, 0.4, 0.3 ],
[ 0.2, 0.4, 0.6 ]
];
Capacity = [ 20, 40 ];
Demand = [
[ 10 100 50 ]
[ 20 200 100]
[ 50 100 100]
];
Inventory = [ 0 0 0];
InvCost = [ 0.1 0.2 0.1];
InsideCost = [ 0.4, 0.6, 0.1 ];
OutsideCost = [ 0.8, 0.9, 0.4 ];
Code Sample 5.4 Instance Data for Multi-Period
Production-Planning Problem (mulprod.dat)
Most of the model generalizes smoothly. For instance, the capacity constraints stated for
all resources and all periods become
tuple plan {
float inside;
float outside;
float inv;
}
When a .mod file contains a main block, the IDE (or the
oplrun command) starts the execution of this model by
running that main block first.
Note that the two optimization models can use the same
model definition (that is, the same .mod file) as is the case
in this example.
if ( cplex.solve() ) {
curr = cplex.getObjValue();
writeln();
writeln("OBJECTIVE: ",curr);
ofile.writeln("Objective with
capFlour = ", capFlour, " is ", curr);
}
else {
writeln("No solution!");
break;
}
You can access and modify some data in the data elements
obtained from the current OPL model, as follows:
data.capacity["flour"] = capFlour;
Note
1. Only data external to the model can be modified. Data
elements that are defined inline within the model file
cannot be modified.
2. You can also use data elements to add new elements,
but only for scalar types.
The cplex instance is filled with the new optimization model which corresponds
to the same model definition but uses the modified data elements.
Note
The cplex instance that was also used for the original model
does not contain the original optimization model anymore.