You are on page 1of 52

Introduction to CPLEX

Đỗ Vĩnh Trúc
CONTENTS
• I. Introduction to CPLEX
• II. Structure of a CPLEX Model
• III. Practice with code
• IV. Works in class

Đỗ Vĩnh Trúc
I. Introduction
• CPLEX Optimization Studio is composed of:
– OPL, the Optimization Programming Language, used
to write mathematical models.
– An integrated development environment (IDE) that
enables you to develop and test the models.
– The CPLEX Optimizer engine, to find solutions to
models that require mathematical programming
techniques.
– The CP Optimizer engine, to find solutions to models
that require constraint programming techniques.
Đỗ Vĩnh Trúc
I. Download and Installation
• Register a member at
http://www-304.ibm.com/ibm/university/academ
ic/pub/page/academic_initiative

Đỗ Vĩnh Trúc
Download and Installation
• Login to IBM Academic Initiative
• Download IBM ILOG CPLEX
OPTIMIZATION STUDIO 12.6.2 for
Windows 32 , or 64, or MAC

Đỗ Vĩnh Trúc
Download and Installation
• Download and installation

Đỗ Vĩnh Trúc
Main Screen

Đỗ Vĩnh Trúc
CREATE A PROJECT
• Click File->New->OPL Project
• Project name:
– Enter name of project
• Create Model:
– Add a new model
• Create Data
– Use a data file

Đỗ Vĩnh Trúc
Đỗ Vĩnh Trúc
II. Structure of a CPLEX Project
• Model File: *.mod
• Data File: *.dat (optional)
• External Data File: Excel file, Access File
(optional) (licensing version )
• Text File: Result file (optional)
These files must be stored in the same folder

Đỗ Vĩnh Trúc
Define Linear Optimization Problem

• Declare:
– The size of indexes (int)
– Range of indexes (range)
– Constant (float , float+ , int, int+, string)
– Decision variables (dvar; data type float+, int+,
Boolean)
– Objective function (minimize, maximize)
– Constraints (sum(), forall())

Đỗ Vĩnh Trúc
Example 1: Linear Programing Problem

Min
Subject to:

Đỗ Vĩnh Trúc
Declare constants and variables in CPLEX

Đỗ Vĩnh Trúc
Input the program

Đỗ Vĩnh Trúc
How to run the model?
• Drag and drop example01.mod to Run
Configurations, and you see

Đỗ Vĩnh Trúc
How to run the model?
• Right click on Configuration1 -> Run this
• See the result

Đỗ Vĩnh Trúc
Modify the example01
• In example01 project, click
• File->New->Model
• Enter example02
• Copy example01 and
• Paste to example02

Đỗ Vĩnh Trúc
• Add the following code
• Drag and drop to Run Configurations
• Configuration2 is added
• Run the model

Đỗ Vĩnh Trúc
Result

Đỗ Vĩnh Trúc
Example 2
Max
Subject

4
40
II. Structure of a CPLEX Project
• Model File: *.mod
• Data File: *.dat (optional)
• External Data File: Excel file, Access File
(optional)
• Text File: Result file (optional)
These files must be stored in the same folder

Đỗ Vĩnh Trúc
Model File
• Declare:
– The size of indexes (int)
– Range of indexes (range)
– Parameters (float, int)
– Decision variables (dvar; data type float+, int+,
Boolean)
– Objective function (minimize, maximize)
– Constraints (sum(), forall())

Đỗ Vĩnh Trúc
Data Types

• Integer
– int m=1;
• Float
– Float a=1.5;
• String
– {string} Tasks =
{"masonry","carpentry","plumbing","ceiling","roofing","painting","win
dows","facade","garden","moving"};
• Range
– int n=8
– range rows= n+1..2*n+1;
Data Types
Arrays
– One-dimensional arrays

int a[1..4] = [10, 20, 30, 40];


float f[1..4] = [1.2, 2.3, 3.4, 4.5];
string d[1..2] = [“ Monday” , “ Wednesday” ];
– Multidimensional arrays

int my2DArray[1..2][1..3] = [ [5, 2], [4, 4], [3, 6] ];

int my2DArray[1..2][1..3] = ...;


and the initialization in the data file.
Model File
Data File
• Declare value of variables in Model file
• Connect to Excel, Access file
• Read/Write data from/to Excel, Access file

Đỗ Vĩnh Trúc
External Data File
• Excel/Access:
– Store data
– Write result

Đỗ Vĩnh Trúc
Text File
• Write more details from solution to a text file,
for example t01.txt, t02.txt,…

Đỗ Vĩnh Trúc
III. Practice with CODE
• Machine Assignment Problem
• Machineco has four machines and four jobs to be
completed. Each machine must be assigned to complete
one job. The time required to set up each machine for
completing each job is shown in Table. Machineco wants
to minimize the total setup time needed to complete the
four jobs.
• Use CPLEX to solve this problem.
•Machineco must determine which machine should be assigned to each
job. Let i, j = 1, 2, 3, 4 be the number of machines and jobs.
•xij =1 if machine i is assigned to meet the demands of job j
xij =0 if machine i is not assigned to meet the demands of job j
•min z = 14x11 + 5x12 + 8x13 + 7x14 + 2x21 + 12x22 + 6x23 + 5x24
+ 7x31 + 8x32 + 3x33 + 9x34 + 2x41 + 4x42 + 6x43 + 10x44
Formulate Min:
• sij -the setup time for machine i to job j
• xij – machine i assigns to job j
• Constraints:
• Each machine only assigns to one job

• One job can be occupied by one machine


Case 1: Only *.mod
Case 2: *.Mod and *.dat
*.dat file content
Case 3: *.mod, *.dat, *.xlsx
*.dat file
*.xlsx file
Transportation Problem
• Powerco has three electric power plants that supply the needs
of four cities. Each power plant can supply the following
numbers of kilowatt-hours (kwh) of electricity: plant 1-35
million; plant 2-50 million; plant 3-40 million (see Table). The
peak power demands in these cities, which occur at the same
time (2 P.M.), are as follows (in kwh): city 1-45 million; city
2-20 million; city 3-30 million; city 4-30 million. The costs of
sending 1 million kwh of electricity from plant to city depend
on the distance the electricity must travel. Formulate an LP to
minimize the cost of meeting each city’s peak power demand.
• xij number of (million) kwh produced at plant i and sent to
city j
III. Practice with CODE (cont)

Đỗ Vĩnh Trúc
Mathematical Model
0 If arc i  j is not included
Let xi j   
1 If arc i  j is included

Min  c i j
ij x ij

Subje ct t o :
1 if i  1

x -x
j
ij
k
ki  0 if i  2 , m - 1
-1 if i  m

x ij  0 or 1

Đỗ Vĩnh Trúc
IV. Works in Class
• 1.Using Cplex to solve

Đỗ Vĩnh Trúc
Đỗ Vĩnh Trúc
2. Using cplex to solve

Đỗ Vĩnh Trúc
Đỗ Vĩnh Trúc
Đỗ Vĩnh Trúc
Thank you

Đỗ Vĩnh Trúc

You might also like