0% found this document useful (0 votes)
3 views5 pages

Computers

This document contains practice exercises for IBM ILOG CPLEX Optimization Studio, focusing on OPL programming. It covers topics such as declarations, decision variables, objective functions, constraints, ranges, arrays, tuple structures, and scripting. Additionally, it provides answers to the exercises, demonstrating practical applications of the concepts discussed.

Uploaded by

khoi.huynhlog
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Computers

This document contains practice exercises for IBM ILOG CPLEX Optimization Studio, focusing on OPL programming. It covers topics such as declarations, decision variables, objective functions, constraints, ranges, arrays, tuple structures, and scripting. Additionally, it provides answers to the exercises, demonstrating practical applications of the concepts discussed.

Uploaded by

khoi.huynhlog
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

IBM ILOG CPLEX Optimization Studio

OPL Programming Practice Exercises

Khôi
March 27, 2026

Contents
1 Exercises 2
1.1 Declarations & Decision Variables . . . . . . . . . . . . . . . . . . . . . . 2
1.2 Objective Function & Constraints . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Ranges, Arrays & Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 Tuple Structures and Tuple Sets . . . . . . . . . . . . . . . . . . . . . . . 2
1.5 IBM ILOG Script (Scripting) . . . . . . . . . . . . . . . . . . . . . . . . 3
1.6 Data Separation and Spreadsheet Connections . . . . . . . . . . . . . . . 3

2 Answers 4
2.1 Declarations & Decision Variables . . . . . . . . . . . . . . . . . . . . . . 4
2.2 Objective Function & Constraints . . . . . . . . . . . . . . . . . . . . . . 4
2.3 Ranges, Arrays & Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.4 Tuple Structures and Tuple Sets . . . . . . . . . . . . . . . . . . . . . . . 5
2.5 IBM ILOG Script (Scripting) . . . . . . . . . . . . . . . . . . . . . . . . 5
2.6 Data Separation and Spreadsheet Connections . . . . . . . . . . . . . . . 5

1
1 Exercises
1.1 Declarations & Decision Variables
Exercise 1: Write OPL code to declare and initialize an integer parameter demand with
a value of 500, a float parameter cost with a value of 12.5, and a string productName
with a value of "Bicycle".
Exercise 2: Write code to declare a non-negative integer decision variable named
productionQuantity.
Exercise 3: Declare a float decision variable named inputQuantity bounded between
2.2 and 3.2.

1.2 Objective Function & Constraints


Exercise 4: Using the productionQuantity variable from Exercise 2, write an objective
function to maximize profit. Assume each unit yields a profit of 25.0.
Exercise 5: Write a constraint block (subject to) including 2 conditions: productionQuantity
must not exceed demand (declared in Exercise 1), and productionQuantity must be
greater than or equal to 100. Name the first constraint ct_demand.

1.3 Ranges, Arrays & Sets


Exercise 6: Declare an integer constant nDays equal to 7. Then create a range named
days from 1 to nDays.
Exercise 7: Declare an integer array dailyDemand indexed by the range days (from
Exercise 6) and initialize it explicitly with the list of values: 10, 15, 12, 20, 18, 22, 16.
Exercise 8: Declare a float array capacity indexed by days. Initialize this array using
a generic array formula where the capacity for day i is i * 10 + 50.
Exercise 9: Declare a set of strings named machines containing 3 elements: "M1",
"M2", and "M3".
Exercise 10: Given two sets of integers: {int} setA = {1, 2, 3, 4}; and {int}
setB = {3, 4, 5, 6};. Write code to create setC as the intersection of the two sets
and setD as the union.
Exercise 11: Write a block of code using the forall quantifier and the sum aggregate
operator to define a constraint: The total production (produce[m]) across all machines (m
in machines) must be greater than or equal to 1000. Assume the variable array produce
needs to be declared first.

1.4 Tuple Structures and Tuple Sets


Exercise 12: Define a tuple structure named Point containing 2 integer attributes x
and y. Then initialize a variable p1 of type Point with the value (3, 4).

2
Exercise 13: Define a tuple structure named City with two attributes: name (string
type, acting as the uniquely identifying key) and population (integer type).
Exercise 14: Using the Point tuple (from Exercise 12), create a tuple set named points
containing 3 points: (0,0), (1,5), and (2,3).
Exercise 15: Apply a filter (slicing) to the points tuple set (from Exercise 14). Create
a new set filteredPoints containing only points with an x value greater than 0.

1.5 IBM ILOG Script (Scripting)


Exercise 16: Write an execute block in OPL to print the text "Optimization Started"
to the console (Scripting log).
Exercise 17: Within an execute block, write a for loop (using JavaScript/ILOG Script
syntax) to print the numbers from 1 to 5.
Exercise 18: Within an execute block, call functions from the Math library to calculate
and print the absolute value of -25 and the minimum value between 15 and 30.

1.6 Data Separation and Spreadsheet Connections


Exercise 19: In the .mod file, declare an integer maxCapacity to be initialized externally.
In the .dat file, write the code to explicitly assign the value 1500 to this variable.
Exercise 20: In the .dat file, write a command to open a connection to an Excel file
named "[Link]" (assign the connection name xlsx). Then write a command to read
data from the cell range "Sheet1!A1:A5" into an OPL array named excelData (assuming
this array is already declared with ...; in the .mod file).

3
2 Answers
2.1 Declarations & Decision Variables
Answer 1:
1 int demand = 500;
2 float cost = 12.5;
3 string productName = " Bicycle ";
Answer 2:
1 dvar int + pr odu ct io nQu an ti ty ;
Answer 3:
1 dvar float inputQuantity in 2.2..3.2;

2.2 Objective Function & Constraints


Answer 4:
1 maximize 25.0 * p ro du cti on Qu ant it y ;
Answer 5:
1 subject to {
2 ct_demand : pro du ct ion Qu an tit y <= demand ;
3 p rod uc ti onQ ua nt ity >= 100;
4 }

2.3 Ranges, Arrays & Sets


Answer 6:
1 int nDays = 7;
2 range days = 1.. nDays ;
Answer 7:
1 int dailyDemand [ days ] = [10 , 15 , 12 , 20 , 18 , 22 , 16];
Answer 8:
1 float capacity [ i in days ] = i * 10 + 50;
Answer 9:
1 { string } machines = {" M1 " , " M2 " , " M3 "};
Answer 10:
1 { int } setA = {1 , 2 , 3 , 4};
2 { int } setB = {3 , 4 , 5 , 6};
3 { int } setC = setA inter setB ;
4 { int } setD = setA union setB ;
Answer 11:
1 dvar int + produce [ machines ];
2
3 subject to {
4 sum ( m in machines ) produce [ m ] >= 1000;
5 }

4
2.4 Tuple Structures and Tuple Sets
Answer 12:
1 tuple Point {
2 int x;
3 int y;
4 }
5 Point p1 = <3 , 4 >;
Answer 13:
1 tuple City {
2 key string name ;
3 int population ;
4 }
Answer 14:
1 { Point } points = { <0 ,0 > , <1 ,5 > , <2 ,3 >};
Answer 15:
1 { Point } filteredPoints = { p | p in points : p . x > 0 };

2.5 IBM ILOG Script (Scripting)


Answer 16:
1 execute {
2 writeln (" Optimization Started ") ;
3 }
Answer 17:
1 execute {
2 for ( var i = 1; i <= 5; i ++) {
3 writeln ( i ) ;
4 }
5 }
Answer 18:
1 execute {
2 writeln (" Absolute : " , Math . abs ( -25) ) ;
3 writeln (" Minimum : " , Math . min (15 , 30) ) ;
4 }

2.6 Data Separation and Spreadsheet Connections


Answer 19:
In the .mod file:
1 int maxCapacity = ...;
In the .dat file:
1 maxCapacity = 1500;
Answer 20:
In the .dat file:
1 SheetConnection xlsx (" data . xlsx ") ;
2 excelData from SheetRead ( xlsx , " Sheet1 ! A1 : A5 ") ;

You might also like