Beruflich Dokumente
Kultur Dokumente
1. C++
* Creating a new project
* Program structure
* Defining variables
* Arrays
* Pointers
* Basic Operations
* Loops
* Functions
* Input/Output
* Debugging
* General coding tips
2. CPLEX
* Linking to Visual C++ 2008
* Initialization
* Constructing constraints
* Constructing constraint arrays
* Extracting the model
* Retrieving information
3. Additional Resources
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.
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.
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
==.
While loops, like for loops, perform multiple iterations of a task. The
difference is that while loops can perform an unspecified number of
iterations, e.g.
while(number1<>7)
{expresssion}
performs the task until number1 is 7, at which point it exits the loop.
Infinite while loops can be constructed by while(1) {expression} and can
be broken by the command break. For example,
while (1)
{runprogram();
if(status == optimal)
{break;}}
will run the task indefinitely until the optimal solution is found.
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.
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.
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.
Note: The C++ file must include the header filesilcplex\cplex.h and
ilcplex\ilocplex.h.
for(i=0;i<4;i++) {
IloExpr xpr(env);
for(j=0;j<3;j++) {
xpr += (i+1)*var x[j];
}
sprintf(temp,cst a(%d),i+1);
cst a.add(IloRange(env,0,xpr,+IloInfinity,temp));
xpr.end();
}
Note: To add the constraint array to the model, use model.add(cst a).
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.