You are on page 1of 58

Introduction

to CPLEX OPL
Operations Research Lab
Department of Industrial and
Systems Engineering
Course content
• CPLEX installation
• CPLEX introduction with examples
• Product mix problem
• Budget allocation problem
• Knapsack problem
• Assignment problem
• N queen problem
• Travelling salesman problem
CPLEX
Installation
Operations Research Lab
Department of Industrial and
Systems Engineering
Fill all the require credential

Institute Mail Id
After verification code select create account
Select any cos installer
After download installation
Run as administrator
Check space
requirement
Types of Programming

Using CP in OPL model:


Optimizer Software Modeling Languages
Function: Optimizer software, such as IBM CPLEX, Function: Optimization modeling languages like OPL,
Gurobi, or SCIP, are dedicated tools designed to solve AMPL, GAMS, AIMMS, and JuMP are designed to
optimization problems. They provide the algorithms specify optimization problems in a high-level, human-
and computational engines required to find optimal readable format.
solutions to mathematical programming problems.
Purpose: These software packages focus on Purpose: They allow users to describe the optimization
implementing various optimization algorithms (e.g., problem using mathematical equations, constraints,
linear programming, mixed-integer programming) decision variables, and objective functions in a more
efficiently. They handle the computational intuitive and abstract way, without delving into the
complexities and numerical aspects of solving specifics of algorithms or implementation details.
optimization problems.
Usage: Optimizer software is primarily used for Usage: Optimization modeling languages act as a
executing and solving models created in optimization bridge between the problem formulation (in a
modeling languages. They provide APIs (Application mathematical representation) and the optimization
Programming Interfaces) or interfaces in different software. Users write optimization models using these
programming languages (Python, C++, etc.) to interact languages, which are then translated or passed onto
with the optimization engines. the optimizer software to be solved.
• System for solving optimization problems
• OPL: Optimization Programming Language
• CPLEX: “Simplex in C”

Describing an optimization problem:


1. Constants used in the problem 2.Variables used in the problem.
3. The linear objective function 4. The linear inequalities defining the feasible region.

Representing a problem
OPL separates the model and its instance

Model: .mod extension, describes the structure of a problem.


Instance: .dat extension, describes the data in a problem.

In the OPL IDE, a model and data file are associated in a run configuration.
OPL has two main kinds of data: constants and decision variables.

Constants Decision Variables

float dvar float


•float a = 6.5; •dvar float x;
•float a = …; •dvar float+ x;
•dvar float- x;
int dvar int
•int a = 6; •dvar int x;
•int a = …; •dvar int+ x;
•dvar int- x;
string
•{string} a = ...; dvar boolean
•{string} a = { a1, a2, a3}; •dvar boolean y;

data as arrays Expressions


range cols = 1..a; float x[cols] =[1, 2, 3, 4]; dvar int+ y[cols]; dexpr float z = 3*x + 4*y;
File  New  OPL Project
Enter Project name and select
create Model, create Data 
Finish

© K N Reddy Email: knreddy@iitkgp.ac.in


Project Explorer Model Code Outline

Results
Debug
© K N Reddy Email: knreddy@iitkgp.ac.in
Objective function
m

Maximize z = c x i i
i1

Subject to: Constraints

a x  bj
i, j i for j = 1, …, n
i1

xi  0
Non negativity
for i = 1, …, m
Constraint

© K N Reddy Email: knreddy@iitkgp.ac.in


Objective function
Maximize z = 3 0 x1  5 5 x 2  2 3 x 3
Maximize total profit

Subject to: Constraint

2x1  2x2  x3  400 Resource/Capacity


Constraints
3x1  x2  x3  450
x1  4x2  390 Non negativity
Constraint
x1  0, x2  0, x3  0
Production quantity
must be non negative

© K N Reddy Email: knreddy@iitkgp.ac.in


/*********************************************
* OPL 12.5 Model
* Author: user
* Creation Date: Sep 21, 2017 at 4:32:58 PM
*********************************************/
/* Without range */

dvar float+ x1; Decision Variables


dvar float+ x2;
dvar float+ x3;

dexpr float z = 30*x1+55*x2+23*x3; Objective function


maximize z;

subject to {
Constraint_1:
2*x1+2*x2+x3 <= 400;
Constraints
Constraint_2:
3*x1+x2+x3 <= 450;
Constraint_3:
x1+4*x2 <= 390;
}

© K N Reddy Email: knreddy@iitkgp.ac.in


Add .mod, .dat files
to configuration

© K N Reddy Email: knreddy@iitkgp.ac.in


Solution

execute PARAMS {
cplex.tilim = 10;
}

// Post processing to print results.


execute {
writeln("Maximum Contribution Possible = Rs. ", cplex.getObjValue());
writeln("\n----- RESULT: Optimal Product Mix---------");
}

© K N Reddy Email: knreddy@iitkgp.ac.in


n
Z  cT x   ci xi
Objective function
Maximize
i1 Maximize total profit

Subject to: Constraint


Resource/Capacity
AxT  b  Aij xiT  b j i1..n, j1..m Constraints

xi  0 i
1..n
Non negativity
Constraint
Where

© K N Reddy Email: knreddy@iitkgp.ac.in


/*********************************************
* OPL 12.5 Model
* Author: user
* Creation Date: Sep 21, 2017 at 4:32:58 PM
*********************************************/
/* With range */
int m = ...; // No. of Variables
int n = ...; // No. of Constraints
Constants
range vars = 1 .. m;
range consts = 1 .. n;
int c[vars] = ...;
int A[consts][vars] = ...;
int b[consts] = ...;

dvar float + x[vars]; Decision Variables

dexpr float z = sum(i in vars) (c[i]*x[i]);


maximize z; Objective function

© K N Reddy Email: knreddy@iitkgp.ac.in


subject to {
Constraint:
forall (j in consts) { Constraints
sum(i in vars) (A[j][i]*x[i]) <= b[j];
}
}

m = 3; // No. of Variables
n = 3; // No. of Constraints
c = [30, 55, 23]; // Objective Coefficients
A = [[2, 2, 1], [3, 1, 1], [1, 4, 0]]; // Constraint
Coefficients
b = [400, 450, 390]; // RHS

© K N Reddy Email: knreddy@iitkgp.ac.in


Solution

© K N Reddy Email: knreddy@iitkgp.ac.in


Objective function
Minimize z = 3 x1  2 x 2
Minimize total cost

Subject to: Constraints

x1  2x2  12

2x1  3x2 = 12
≥8
2x1  x2
Non negativity
x1  0, x2  0 Constraint

© K N Reddy Email: knreddy@iitkgp.ac.in


/*********************************************
* OPL 12.5 Model
* Author: user
* Creation Date: Sep 21, 2017 at 4:32:58 PM
*********************************************/
/* With range */
int m = ...; // No. of Variables
int n = ...; // No. of Constraints
Constants
range vars = 1 .. m;
range consts = 1 .. n;
int c[vars] = ...;
int A[consts][vars] = ...;
int b[consts] = ...;

dvar float + x[vars]; Decision Variables

dexpr float z = sum(i in vars) (c[i]*x[i]);


minimize z; Objective function

© K N Reddy Email: knreddy@iitkgp.ac.in


subject to {
Constraint_1:
sum(i in vars) (A[1][i]*x[i]) <= b[1]; Constraints

Constraint_2:
sum(i in vars) (A[2][i]*x[i]) == b[2];

Constraint_3:
sum(i in vars) (A[3][i]*x[i]) >= b[3];
}

m = 2; // No. of Variables
n = 3; // No. of Constraints
c = [3, 2]; // Objective Coefficients
A = [[1, 2], [2, 3], [2, 1]]; // Constraint
Coefficients
b = [12, 12, 8]; // RHS

© K N Reddy Email: knreddy@iitkgp.ac.in


Solution

Statistics

© K N Reddy Email: knreddy@iitkgp.ac.in


A problem instance properly modeled in OPL consists of:
• A model file containing:
1. Constant definitions ( float b[vars] = ...; )
2. Decision variable definitions (dvar float+ x;)
3. An objective definition (maximize (...))
4. Constraints (subject to { (...) })
• A data file containing those values defined with = ...; in the
model file.
• Optionally, other configuration options controlling the
optimization (likely time limit ).

© K N Reddy Email: knreddy@iitkgp.ac.in


Assignment examples
Q1. A company produces two products, A and B. The sales volume
for A is at least 80% of the total sales of both A and B. However,
the company cannot sell more than 110 units of A per day. Both
products use one raw material, of which the maximum daily
availability is 300 lb. The usage rates of the raw material are 2 lb
per unit of A, and 4 lb per unit of B. The profit units for A and B
are $40 and $90, respectively. Determine the optimal product mix
for the company.
X1 = No of units of A produced
X2 = No of units of B produced

Objective Function:

Maximize Profit (Z) = 40 * X1 + 90 * X2

Constraint 1 :
Sales Volume of A is 80% of total sales
X1 >= 0.8 * (X1 + X2)

Constraint 2 :
Cannot sell more than 110 units of A
X1 <= 110

Constraint 3:
Raw material availability
(2 * X1 + 3 * X2) <= 300

Non-negativity constraints:
X1, X2 >= 0
Q2. Alumco manufactures aluminum sheets and aluminum bars.
The maximum production capacity is estimated at either 800
sheets or 600 bars per day. The maximum daily demand is 550
sheets and 560 bars. The profit per ton is $40 per sheet and $35
per bar. Determine the optimal daily production mix.
X1 = No of sheets manufactured per day
X2 = No of bars manufactured per day

Objective Function:
Maximize Profit (Z) = 40 * X1 + 35 * X2

Constraints:
Production Capacity constraint
X1 / 800 + X2 / 600 <= 1

Maximum daily demand


X1 <= 550
X2 <= 560

Non-negativity constraints
X1, X2 >= 0
DONE

You might also like