Professional Documents
Culture Documents
Linear Programming: Short Introduction To CPLEX® Studio
Linear Programming: Short Introduction To CPLEX® Studio
MS-E2140
Disclaimer: This tutorial is not a complete introduction to the full features and
potentiality of CPLEX® Optimization Studio. More detailed documentation is
available at http://pic.dhe.ibm.com/infocenter/cosinfoc/v12r5/index.jsp
Features
CPLEX studio is an optimization package which includes
• A modeling development tool: IBM ILOG OPL
• A powerful optimization package: IBM ILOG CPLEX
OPL is a modeling language for optimization problems with intutitive syntax that
facilitates the implementation of mathematical models
CPLEX is one of the state-of-the-art commercial solvers for various types of
optimization problems including:
• Linear programming problems
• Integer and mixed integer prgramming problems
• Quadratic programming problems
CPLEX studio provides a development environment that facilitates the creation
and solution of mathematical programming models and the analysis of the results
Models are independent of the data they use meaning, e.g., that the model code
needs not be changed for solving different instances of the same problem
Resources
A full version of CPLEX studio is installed in the lab machines
A free trial version (90 days evaluation period and with limitations on the
maximum size of solvable problems) is available through the IBM website
http://www-01.ibm.com/software/websphere/products/optimization/cplex-
studio-preview-edition/
Full official documentation is available at
http://pic.dhe.ibm.com/infocenter/cosinfoc/v12r5/index.jsp
Examples. Several examples of projects can be opened and explored within
CPLEX studio. To load them from the start window main menu click:
”File -> Import -> Example...“ (then double click “IBM ILOG CPLEX Examples”)
OPL keywords. From the start window: ”Help -> Search” (type OPL keywords)
OPL functions. From the start window: ”Help -> Search” (type OPL functions)
Creating an empty project
The main working unit in CPLEX studio is the ”project” which groups one or
more models together with data. It is composed of three types of files
• Model files (.mod): data declarations, model definition (decision variables,
objective function, constraints);
• Data files (.dat): Actual values for the data declared in model files;
• Settings files (.ops): Specify changes to the default settings in OPL,
(parameters for the solution algorithm, display options, ...)
To create a new project select ”File -> New -> OPL Project” from the main
window’s menu. Then choose a name and location folder and click ”Finish”.
A project contains one or more ”run configurations” each specifying a model
instance by linking the model with a particular set of data (and, optionally,
settings)
Project name To create new model or data
Run configuration files in a project right-click on
the project name and then
Model file select ”new -> model” or ”new
-> data”
Data file
where
𝑦𝑖 : Total number of overtime hours for department 𝑖
𝑥𝑗 : Total number of units produced of product 𝑗
A data specification is called data item and consists of a ”data type”
• Integer (OPL keyword int)
• Real (OPL keyword float)
• String (OPL keyword string)
and a ”data structure” ( scalar, range, set, array, or tuple)
In our model we will need to specify the following data items
Model data Data Data OPL Syntax
type struct.
Set of department names string Set {string} Departments = ...;
Set of product names string Set {string} Products = ...;
Departments manufacturing float 2-d array float Manuf_Time[ ][ ] = ...;
time for each product (matrix)
Departments time capacity float 1-d array float Time_Capacity[ ] = ...;
Products unit revenue float 1-d array float Revenue[ ] = ...;
Max. Departments overtime float 1-d array float Max_Overtime[ ] = ...;
Overtime cost for each Dept. float 1-d array float Overtime_Cost[ ] = ...;
Set data structures
The following is the declaration of the Departments set
{string} Departments = ...;
The curly brackets { } indicate that the structure ”Departments” is a set, and the
keyword string specifies the type of its elements
Array data structures
The following is the declaration of the structure specifying the manufacturing
time for each product
float Manuf_Time[Departments][Products] = ...;
The double brackets [][] indicate that the structure ”Manuf_Time” is a matrix, and
the keyword float specifies the type of its elements
The sets ”Departments” and ”Products” are used to index the array. With this
declaration each element 𝑖, 𝑗 of ”Manuf_Time” is associated with element 𝑖 of the
set ”Departments”, and element 𝑗 of set ”Products”
To define a one dimensional array just put one single pair of brackets []
Note: All declarations (and other instructions) must end with a ”;”
Note that all data declarations ended with the syntax ” = ⋯ ; ”
This indicates that the actual values of the elements in the data item
are not specified in the declaration, but somewhere else (e.g., in the
data file)
-> Double-click on the model file and type the following declarations:
Data initializations
Data items whose declaration ends with the syntax ” = ⋯ ; ” in the
model file can be initialized in the data file
Alternatively data items can be intialized directly in the model file by
replacing ” = ⋯ ; ” with the initialization when they are declared
Set intialization
The following is the initialization of the ”Departments” set
Departments = {”A”, ”B”, ”C”};
The initialization just specifies the element values within the brackets.
Note that the value types must match those defined in the declaration.
In this case, elements are strings, and must be enclosed in ” ”, i.e. the
syntax for any element s of type string is ”s”.
Similarly, we intialize the set Products:
Products = {"Prod_1", "Prod_2"};
Array initialization
For 1-d arrays values of the elements can be simply inserted
inside the brackets separated by commas
The following is the initialization of the ” Time_Capacity” array
Time_Capacity = [100, 36, 50];
Note the use of the sintax ”:” to separate the filtering condition from
the indexing expession inside the parentheses
Common aggregators for integer and float expressions
• sum (summations)
• prod (products)
• minima (min of a collection of related expressions)
• maxima (max of a collection of related expressions)
Objective function
An obj. function consists of a keyword ”maximize” or ”minimize”
followed by an expression to optimize
Since in our model we are maximizing the profit, which we already
defined as a dexpr, we can just write
maximize profit;
If variables 𝑦 are declared in this way, then the constraints on the max.
overtime for each Department of the previous slide are not needed
-> This is generally a preferable way of specifying maximum (or
minimum) values for a set of variables
Recall that each constraint definition ends with ”;”, and that all
constraints have to be included inside a block like this
subject to{ ... }
-> Add the objective function and constraints to the model file
The model can now be solved
-> Right-click on the run configuration icon and select ”Run this”
After the problem is solved the obj. function value will be displayed here
The values of the decision variables and expressions are displayed here,
or alternatively you can click here
The optimal solution is 𝑥1 = 82, 𝑥2 = 80, 𝑦1 = 10, 𝑦2 = 4.6, 𝑦3 = 6.4 with profit 16,797
Other useful commands
Here we briefly examine some other useful features and structures of OPL
that were not used in the previous example
The above declares a range named “Rows” with bounds 1 and 10 (i.e., the
interval [1, … , 10]). The lower and upper bounds can also be expressions
int n = 8;
range Rows = n+1..2*n+1;
Typical uses of ranges are
as an array index in an array declaration
For example, in our model we could define Departments as a range
rather than a set, and still use it in the declaration of the arrays
range Departments = 1..3;
float Time_Capacity[Departments];
equivalently
float Time_Capacity[1..3];
One advantage over using sets, is that CPLEX does not store explicitly
all the elements of the range, but it does store all elements of a set
to define the domain of a variable
We already discussed how to define the domain of a variable or an
array of variables
as an iteration range
For example, for defining constraints
range Departments = 1..3;
range Products = 1..2;
forall(i in Departments)
sum(j in Products) Manuf_Time[i][j]*Production[j] –
Overtime_h[i] <= Time_Capacity[i];
The syntax is the same as if Departments and Products were sets
One advantage is that now Departments and Products are numbers,
and we can use arithmetic operators in the filtering condition. Example
For a description of operators see the summary at
forall(i in Departments : i mod 2 == 0) the end or official CPLEX studio documentation
sum(j in Products : j < 2 )
Manuf_Time[i][j]*Production[j] –
Overtime_h[i] <= Time_Capacity[i];
More ways of initializing sets
Ranges can be converted into sets by using the keyword ”asSet”
For example, the following declarations (in the model file)
range Departments = 1..3
{int} Departments_Sets = asSet(Departments)
Important
Get used to the help system included in CPLEX studio:
-> Click Help from the main menu and then select ”Search” or
”Help Contents”