You are on page 1of 8

Programming Exercise

CPLEX Tutorial

Framework/Instances

Programming Exercise

CPLEX Tutorial

Framework/Instances

Programming Exercise / ILOG CPLEX Tutorial


Andreas M. Chwatal
Algorithms and Data Structures Group Institute of Computer Graphics and Algorithms Vienna University of Technology

Recall the k-node minimum spanning tree (k-MST) problem from the todays lecture: Given: (undirected) graph G = (V , E , w ) nonnegative weighting function w (e) R Goal: Find a minimum weight tree, spanning exactly k nodes. Programming Exercise: develop a branch-and-bound and a branch-and-cut algorithm for this problem

VU Fortgeschrittene Algorithmen und Datenstrukturen Mai 2009

Programming Exercise

CPLEX Tutorial

Framework/Instances

Programming Exercise

CPLEX Tutorial

Framework/Instances

Your Task (1)


Carefully read this tutorial Formulate the k-MST problem as integer linear program (ILP), based on a
1 2

Your Task (2)

For the implementation you should use a C++ framework (which you can nd on the webpage of the course) Furthermore you can nd test-instances (to described later on), which should be used for the evaluation and analysis of the algorithms Compute the results for each instance for (at least) k = k = 1 |V | with both algorithms 2
1 5 |V |

single commodity ow formulation (SCF) cycle elimination formulation (CE), or, alternatively a formulation based on directed connection cuts (DC) a branch-and-bound algorithm for the SCF formulation a branch-and-cut algorithm based on CE/DC using a dynamic cut-separation procedure

Implement the corresponding algorithms, using ILOG CPLEX:


1 2

and

Programming Exercise

CPLEX Tutorial

Framework/Instances

Programming Exercise

CPLEX Tutorial

Framework/Instances

Your Task (3)


Create a short document (preferably in LaTeX) of no more than three pages containing:
1 2 3 4

Organization
You can work on the exercise alone, or in groups of two You have to work on institute-servers (due to CPLEX licence): Servername: behemoth.ads.tuwien.ac.at If you do not have an account yet, you get one after the lecture, or write an email to me. Send the document per email to me/us one day prior to the (oral) exam Make sure, that a correctly working version of your program is available on an institute-server at the time of your exam

Problem description, used variables SCF and CE/DC formulation Short description of implementation, in particular the cut-generation Result table, including
1 2 3 4 5

objective function value running time number of branch-and-bound nodes node in which optimum has been found For B&C: number of separated cuts

Programming Exercise

CPLEX Tutorial

Framework/Instances

Programming Exercise

CPLEX Tutorial

Framework/Instances

What is CPLEX?
CPLEX: Simplex method and C programming language Commercial optimization software package High performance
integer (linear) programming linear programming quadratic programming

Concert Framework
Interface to CPLEX solver (C, C++, C#, Java) Enables to implement: branch-and-bound, branch-and-cut, column generation, ... What does Concert do for you? Variables restricted to e.g. Z or {0, 1} branching is automatically performed on these variables branch-and-bound Preprocessing/Presolving Cut-Generation: Gomory-Fractional-Cuts, Mixed-Integer-Cuts Lots of problem-specic cuts, e.g. Cover-Cuts, Clique-Cuts,... What can you do for Concert? User-dened cuts generate new variables (pricing) ... by callback objects
7 8

http://www.ilog.com/products/cplex/

Programming Exercise

CPLEX Tutorial

Framework/Instances

Programming Exercise

CPLEX Tutorial

Framework/Instances

Prerequisites
This tutorial should contain all information to solve the programming exercise, but if you want to know more, have a look at our manual pages: http://www.ads.tuwien.ac.at/manuals/ (password required) Enable the licence: Environmental variable needs to be set. Type the following lines at the command line promt: $ ILOG LICENSE FILE=/home1/share/ILOG/ilm-2.7-x64/access.ilm $ export ILOG LICENSE FILE
9

First steps
Include the headerle: #include <ilcplex/ilocplex.h> Before the class denition (of the class that will build the model and start the solver) use the following macro: ILOSTLBEGIN Declare/create the following objects: IloCplex cplex; // the solver object IloEnv env; // the environment object IloModel model; // the model; all constraints etc. go in here

10

Programming Exercise

CPLEX Tutorial

Framework/Instances

Programming Exercise

CPLEX Tutorial

Framework/Instances

Basic Program Structure


try { env = IloEnv(); // creating the environment model = IloModel(env); // creating the model (w/env) // build some variables and constraints // and add them to the model model.add(...); // add the objective function model.add(IloMinimize(...)); cplex = IloCplex(model); // create cplex object w/model cplex.solve(); // solve the model } catch (IloException& e) { ... } catch (...) { ... }
11

Data-Types
Constants: IloNum, IloBool, IloInt Variables: IloNumVar, IloBoolVar, IloIntVar Example: IloBoolVar x(env, my-first-bool-var); Arrays/Vectors: IloIntVarArray, IloNumVarArray, IloBoolVarArray Example: // create array of 5 boolean variables IloBoolVarArray y(env, 5); for (u_int i=0; i<5; i++) { stringstream myname; myname << y_ << i; y[i] = IloBoolVar(env, myname.str()); }
12

Programming Exercise

CPLEX Tutorial

Framework/Instances

Programming Exercise

CPLEX Tutorial

Framework/Instances

Constraints
Constraints, equalities, inequalities are added by the IloExpr class: Example: y1 + y2 4 IloExpr myExpr(env); myExpr += y[1]; myExpr += y[2]; model.add(myExpr <= 4); myExpr.end(); // IMPORTANT It is important to call the IloExpr::end() function to free the memory

Objective Function / Solver


The objective function is handled similar to constraints: model.add(IloMinimize(env, expr)); Now we are ready to start the solver: IloCplex cplex(model); cplex.solve(); Did we succeed ...? IloAlgorithm::Status algStatus = cplex.getStatus(); if (algStatus != IloAlgorithm::Optimal) { // something went wrong ... } else { // model solved to optimality env.out() << obj. value: " << cplex.getObjValue() << endl; }
14

13

Programming Exercise

CPLEX Tutorial

Framework/Instances

Programming Exercise

CPLEX Tutorial

Framework/Instances

What is going on now?


1 2 3 4

Cut Generation
In each node of the branch-and-bound tree we may want to add further cutting-planes in order to
1 2

Lets assume, we have dened some integer variables CPLEX solves the LP-relaxation of our model CPLEX then tries to separate internal cutting-planes If this process is nished we still may end up with an solution containing fractional variables Now its time for branching; One particular variable is selected and two subproblems are created by rounding it up and down respectively One subproblem is selected Step 2

strengthen the LP-relaxation add violated inequalities, not initially included to the model (e.g. if we did not add an exponentially large set of cycle-elimination inequalities)

This can be achieved by the use of special callback-classes Example: CutCallbackI for case (1) LazyConstraintCallbackI for case (2)

15

16

Programming Exercise

CPLEX Tutorial

Framework/Instances

Programming Exercise

CPLEX Tutorial

Framework/Instances

Cut Generation (2)


When CPLEX calls such a callback object, we rst need to inspect the current values of our variables: IloNum a = getValue(x[i]); Based on this values we can now build/update some data-structures, apply some algorithms and detect some cutting-planes (e.g. the cycle-elimination-cuts) As we already know from the lecture, we can easily separate cycle elimination cuts by performing shortest path computations If we have found a valid cut, build the corresponding expression cut we can add it to the model ...
1 2

Numeric Issues
In particular for the cut separation we need to take care about numeric issues. Assume we specied the constraint x1 + x2 + x3 = 3 A solution might only satisfy (x1 ) + (x2 ) + (x3 ) = 3 for some small , or equivalently 3 3 x1 + x2 + x3 3 + 3 Attention: according to this issue we might iteratively add inequalities that are already met (except of the numeric error), or might not nd valid cuts and thus end up with an infeasible solution (if not taking care of it)!!! The value of can be obtained by cplex.getParam(IloCplex::EpInt) and set by cplex.setParam(...).
17 18

globally: add(cut); locally: addLocal(cut);

Remark: Environment can be accessed by function getEnv()

Programming Exercise

CPLEX Tutorial

Framework/Instances

Programming Exercise

CPLEX Tutorial

Framework/Instances

Interpreting the output


Nodes Node Left Objective IInf 0 0 12135.50 6 0 2 12246.75 19 1 3 12311.25 17 2 4 12338.50 17 ... * 22 22 integral 0 23 22 13083.00 6 24 22 13102.50 4 * 25 21 integral 0 26 22 12372.25 19 27 23 12463.50 18 ... Best Int. Best Node ItCnt 11258.00 2 User: 26 18 12268.25 24 12270.25 27 12270.25 12270.25 12270.25 12270.25 12270.25 12326.50 109 110 112 113 116 123 Cuts/ Gap Variable B NodeID Parent x_[96] D x_[51] U 7.11% 7.11% 7.11% 6.40% 6.40% 5.97% x_[82] x_[82] x_[83] x_[86] x_[96] x_[66] D U U U U U 0 1 2 22 23 24 25 26 27 0 1 21 21 23 24 0 26 Depth 0 1 2 18 18 19 20 1 2

Debugging hints
Compiling with -O2 (or even -O3) enables optimization. This should be used for the runs for creating the results (otherwise particularily the cut-generation will be too slow). For developing use -p -g which includes proling and debugging information. Debugger: start with gdb --args ./yourprogram type run to start the program type bt or backtrace to see the execution stack This will show you e.g. the line number in which the error or segmentation fault occurs. The tool valgrind can be used for the detection of memory leaks
19 20

13210.0000 13210.0000 13210.0000 13109.0000 13109.0000 13109.0000

Comments: Second line, still in root node of B&B tree; after the separation of 26 internal cuts the LP relaxation is better (higher); branching starts in line 3: node 1 (with parent 0) is the problem where (boolean) variable x [96] has been set to 0 (D = down); rst integral solution in line 5 at node 22 of B&B tree, indicated with * in the rst column; the best integer solution value is 13210.0, whereas the LP-relaxation is 12270.25; the gap is 7.11%; we are nished when the gap is 0% (proven optimality); the last line with * is the node where the optimum has been found;

Programming Exercise

CPLEX Tutorial

Framework/Instances

Programming Exercise

CPLEX Tutorial

Framework/Instances

Test instances
To make life easier, the test instances already include the articial root node 0 Hence, we are actually interested in nding a k-MST of nodes {1, . . . , |V |} !! Be careful, to handle this accordingy w.r.t the number of connected nodes k! You can download six test instances ranging from 10 to 100 nodes from the course webpage. Compute the results for each instance for (at least) k = 1 |V | and k = 1 |V | with both algorithms 5 2

Instances format
First line: number of nodes Second line: number of edges Subsequent lines: edge list (source node, target node; edge weight) Example: 20 35 0, 4; 23; 2, 3; 93; 1, 5; 56;

21

22

Programming Exercise

CPLEX Tutorial

Framework/Instances

Programming Exercise

CPLEX Tutorial

Framework/Instances

k-MST Framework
Main: starting the program, parameter handling Instance: singleton class, responsible for reading the data instances, converting the data in basic data structures, and providing them to other classes Global: singleton class, containing global variables, parameters etc. Tools: provides various useful functions that do not t to one particular class EdgeIO: reading the edges from the input le, writing to stdout (by providing appropriate operators << and >>) kMST BC: this class should contain the ILP-based algorithms, i.e. a branch-and-bound algorithm for the ow formulation and a branch-and-cut algorithm for the packing formulation
23

class Instance
The class Instance provides rudimentary graph data structures (which should be enough to solve the exercise) Data structures: u int nNodes, nEdges typedef std::pair<u int, u int> E denes an edge vector<E> edges list of edges vector<int> edgeWeights corresponding edge weights vector<set<u int>> adjEdges for each node the set of adj. edges Functions: set<u int> getAdjNodes(u int node) for building directed variables we might want to get a direction set<u int> getOutEdges(u int node) set<u int> getInEdges(u int node)
24

Programming Exercise

CPLEX Tutorial

Framework/Instances

Programming Exercise

CPLEX Tutorial

Framework/Instances

Shortest Path algorithm


The class kMST BC CutCallback provides a very simple (Dijkstra based) shortest path algorithm. It does not even use a priority queue, and thus has runtime O(|V |3 ). Nevertheless, this is enough for the exercise. Usage: The arguments are the indices of the source and target node you want to compute the shortest path for Return type: struct sp result s, containing the edge list list<u int> path and the path length double length

DheaMaxFlow

The class DheaMaxFlow provides an ecient max-ow algorithm, which can be used to compute the minimum cut. test.cpp shows how to use the algorithm src/Makefile compiles test.cpp (for testing purposes)

25

26

Programming Exercise

CPLEX Tutorial

Framework/Instances

Programming Exercise

CPLEX Tutorial

Framework/Instances

Graph/Algorithm libraries (optional)

Further Remarks

It is recommended to use the provided (graph) methods, as they provide all necessary functionality If you want more: code yourself ;-) use graph/algorithm libraries like LEDA or boost, which are also available on the servers

be nice ;-) when working on the server (behemoth) start your programs with nice with lower priority $ nice ./yourprogram ... The compute server has four cores, but must t the needs of all participants. start only one process at the same time for your nal test runs make sure you get a slot for your own (no more than three other processes)

27

28

Programming Exercise

CPLEX Tutorial

Framework/Instances

Questions ? Good luck for the programming exercise! Have fun coding! If you have any questions, dont hesitate to ask me (us)!

29

You might also like