Using C++ With CPLEX: Daniel Simmons, Dr. Qipeng Phil Zheng
Using C++ With CPLEX: Daniel Simmons, Dr. Qipeng Phil Zheng
D. A. Simmons (IMSE@WVU)
C++/CPLEX
NLP
1 / 28
Overview
1. C++
* Creating a new project
* Program structure
* Defining variables
* Arrays
* Pointers
* Basic Operations
* Loops
* Functions
* Input/Output
* Debugging
* General coding tips
D. A. Simmons (IMSE@WVU)
C++/CPLEX
NLP
2 / 28
Overview, cont.
2. CPLEX
* Linking to Visual C++ 2008
* Initialization
* Constructing constraints
* Constructing constraint arrays
* Extracting the model
* Retrieving information
3. Additional Resources
D. A. Simmons (IMSE@WVU)
C++/CPLEX
NLP
3 / 28
D. A. Simmons (IMSE@WVU)
C++/CPLEX
NLP
4 / 28
The C++ program file has two main partsthe header and the functions.
The command #include loads header files into the program.
#include<file> indicates a system header file, and #includefile
indicates a header file created by the user.
The function int tmain(. . .) is created by Visual Studio and will be the
first to run when debugging the program.
D. A. Simmons (IMSE@WVU)
C++/CPLEX
NLP
5 / 28
Definition
Integer
Double-precision floating point number
Single-precision floating point number
Characters
Boolean (binary) number
Grouped characters
C++/CPLEX
NLP
6 / 28
C++: Arrays
D. A. Simmons (IMSE@WVU)
C++/CPLEX
NLP
7 / 28
C++: Pointers
If the exact size of an array is not known when variables are declared, it
can be defined as a pointer, which allocates a specified amount of
dynamic memory for the contents of the array. A pointer is denoted by an
asterisk after the variable type, e.g.
double* array = new double[variable1];
After using a dynamic array, the program should close it with the delete
command delete [] array; in order to free up memory.
D. A. Simmons (IMSE@WVU)
C++/CPLEX
NLP
8 / 28
The basic mathematical operations can be used in C++ + (addition), (subtraction), * (multiplication), / (division). Variables can be advanced
by one by the ++ operation, i.e. number++; is the same as
number=number+1; Additionally, an operater paired with an equals sign
performs that operation between the variables and/or numbers on either
side of the sign. For example, number1 += number2 is equivalent to
number1 = number1+number2. This is especially helpful to use inside
loops.
D. A. Simmons (IMSE@WVU)
C++/CPLEX
NLP
9 / 28
C++: Loops
The main loops that will occur in basic programs are the if, if else, for,
and while loops. The if loop performs a task when the specified critera
are met and takes this format: if(number1 < number2) {expression}.
If else loops perform one task if the critera are met and another if not, or
it another set of critera are met. For example,
if (number1 == number2) {expression}
else {expression}
or
else if(number 1 < number 2) {expression}
Note: when setting a variable equal to a number or another variable, use
=. When comparing a variable to a number or other variable, use
==.
D. A. Simmons (IMSE@WVU)
C++/CPLEX
NLP
10 / 28
D. A. Simmons (IMSE@WVU)
C++/CPLEX
NLP
11 / 28
C++/CPLEX
NLP
12 / 28
C++: Functions
New functions can be created in the following way: type functionname
(type variable1, type variable2,...). The first type here refers to the
type of the output returned by the function. Any of the variable types
listed earlier can be used, as well as void for a function that does not
return a value. A commonly used type is int, where the program ends with
the command return 0;
The variables listed in parentheses are external inputs required by the
function. By default, these variables are passed by value, which means
that the values of these variables are copied and used in the function,
leaving the original variable unaltered. In order to alter the value of the
external variable from inside the function (to pass the variable by
reference), an ampersand must be added to the variable type, e.g.
int functionname (int& variable1)
D. A. Simmons (IMSE@WVU)
C++/CPLEX
NLP
13 / 28
New functions must be declared in the header after the header files and
before the main function.
type functionname (type variable1, type variable2,...);
The function itself can be started after the main function like this:
type functionname (type variable1, type variable2,...)
{expression}
To call a function, all that is needed is its name and inputs, e.g.
functionname(variable1, variable2); These variable names do not have
to match the names given in the function declaration; they are assigned in
order to the corresponding declared variable.
D. A. Simmons (IMSE@WVU)
C++/CPLEX
NLP
14 / 28
C++: Input/Output
The cout command prints to the terminal window, and the cin command
receives values typed into the terminal as variables. For example,
cin >> arraysize
will enter whatever is typed into the terminal as the value of the variable
array size, and
cout << Array size = << arraysize << endl;
prints the phrase Array size = followed by the value for the variable
array size. The command endl ends the current line and moves to the
next.
Note: Each line of code writing to the screen must start with cout, even if
the output will not be on a new line. Printing to a text file is more
challenging and will be discussed at a later date.
D. A. Simmons (IMSE@WVU)
C++/CPLEX
NLP
15 / 28
C++: Sprintf
D. A. Simmons (IMSE@WVU)
C++/CPLEX
NLP
16 / 28
To build and run a program in Visual C++, click the green triangular
button in the toolbar or press F5 on the keyboard. This starts the
debugging process, which compiles the program, checks for errors, and
executes if no errors are found. If errors are found, they will be listed at
the bottom of the Visual C++ window in the Output frame. Clicking on
an error message will move the cursor to the corresponding line of code.
D. A. Simmons (IMSE@WVU)
C++/CPLEX
NLP
17 / 28
C++: Breakpoints
D. A. Simmons (IMSE@WVU)
C++/CPLEX
NLP
18 / 28
D. A. Simmons (IMSE@WVU)
C++/CPLEX
NLP
19 / 28
D. A. Simmons (IMSE@WVU)
C++/CPLEX
NLP
20 / 28
D. A. Simmons (IMSE@WVU)
C++/CPLEX
NLP
21 / 28
CPLEX: Initialization
In order to use CPLEX, an environment must first be created in C++.
IloEnv env; creates an environment called env. Next, the model that
will be sent to the CPLEX solver must be created. IloModel model(env);
creates model called model inside existing environment env. The
variables used in the model are created through IloNumVar or
IloNumVarArray var x(env,0.0, 40.0, ILOFLOAT);
IloNumVarcreates a single variable, whereas IloNumVarArray creates an
array of variables. The first term in parentheses is the name of the
environment, the second is the lower bound, the third is the upper bound,
and the fourth is the variable type. ILOFLOAT is a continuous floating
point number, ILOINT is an integer, and ILOBOOL is a boolean.
D. A. Simmons (IMSE@WVU)
C++/CPLEX
NLP
22 / 28
D. A. Simmons (IMSE@WVU)
C++/CPLEX
NLP
23 / 28
D. A. Simmons (IMSE@WVU)
C++/CPLEX
NLP
24 / 28
D. A. Simmons (IMSE@WVU)
C++/CPLEX
NLP
25 / 28
After the objective function and all constraints have been added, the
model can be extracted to the CPLEX solver.
IloCplex cplex(env);
cplex.extract(model);
cplex.solve();
This returns a Boolean value that is true if the problem is feasible (not
necessarily optimal) and false if no solution was found. If running multiple
iterations where the model is extracted and solved each time, run
cplex.clear() before the model is extracted to prevent solution
contamination.
D. A. Simmons (IMSE@WVU)
C++/CPLEX
NLP
26 / 28
C++/CPLEX
NLP
27 / 28
Additional resources
D. A. Simmons (IMSE@WVU)
C++/CPLEX
NLP
28 / 28