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

1

2

Programming Exercise

CPLEX Tutorial

Framework/Instances

Programming Exercise

CPLEX Tutorial

Framework/Instances

Carefully read this tutorial Formulate the k-MST problem as integer linear program (ILP), based on a
1 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

3

4

that a correctly working version of your program is available on an institute-server at the time of your exam Problem description. Clique-Cuts.g.ilog.g.at If you do not have an account yet.tuwien. Z or {0. you get one after the lecture. C++. or write an email to me.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. Cover-Cuts. branch-and-cut... column generation. C#.ac. e.. by callback objects 7 8 http://www.. used variables SCF and CE/DC formulation Short description of implementation.ads. in particular the cut-generation Result table.. or in groups of two You have to work on institute-servers (due to CPLEX licence): Servername: behemoth..com/products/cplex/ . What does Concert do for you? Variables restricted to e. . Java) Enables to implement: branch-and-bound.. What can you do for Concert? User-deﬁned cuts generate new variables (pricing) . 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 5 6 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. Mixed-Integer-Cuts Lot’s of problem-speciﬁc cuts. Send the document per email to me/us one day prior to the (oral) exam Make sure. 1} ⇒ branching is automatically performed on these variables ⇒ branch-and-bound Preprocessing/Presolving Cut-Generation: Gomory-Fractional-Cuts.

go in here 1 2 3 10 Programming Exercise CPLEX Tutorial Framework/Instances Programming Exercise CPLEX Tutorial Framework/Instances Basic Program Structure try { env = IloEnv(). myname. } catch (. but if you want to know more.h> Before the class deﬁnition (of the class that will build the model and start the solver) use the following macro: ILOSTLBEGIN Declare/create the following objects: IloCplex cplex.at/manuals/ (password required) Enable the licence: Environmental variable needs to be set.solve().. // add the objective function model. IloBoolVar. } 12 . ‘‘my-first-bool-var’’)..ads.) { .. IloInt Variables: IloNumVar.Programming Exercise CPLEX Tutorial Framework/Instances Programming Exercise CPLEX Tutorial Framework/Instances Prerequisites This tutorial should contain all information to solve the programming exercise.tuwien.7-x64/access.str()). myname << ‘‘y_’’ << i. for (u_int i=0.add(IloMinimize(. have a look at our manual pages: http://www. IloIntVar Example: IloBoolVar x(env. IloBoolVarArray Example: // create array of 5 boolean variables IloBoolVarArray y(env.. i++) { stringstream myname. // create cplex object w/model cplex. // solve the model } catch (IloException& e) { . all constraints etc. } 11 Data-Types Constants: IloNum. y[i] = IloBoolVar(env. // the solver object IloEnv env. cplex = IloCplex(model).ac.. Arrays/Vectors: IloIntVarArray.. Type the following lines at the command line promt: \$ ILOG LICENSE FILE=/home1/share/ILOG/ilm-2.. // creating the model (w/env) // build some variables and constraints // and add them to the model model.. 5). // the environment object IloModel model. // the model.ilm \$ export ILOG LICENSE FILE 9 First steps Include the headerﬁle: #include <ilcplex/ilocplex. IloNumVarArray...)). i<5.add(.). // creating the environment model = IloModel(env). IloBool.

out() << ‘‘obj.? IloAlgorithm::Status algStatus = cplex. value: " << cplex. we have deﬁned 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 it’s time for branching.getStatus().add(myExpr <= 4).. myExpr. if (algStatus != IloAlgorithm::Optimal) { // something went wrong .. } 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 Let’s assume.solve(). if we did not add an exponentially large set of cycle-elimination inequalities) 5 6 This can be achieved by the use of special callback-classes Example: CutCallbackI for case (1) LazyConstraintCallbackI for case (2) 15 16 . inequalities are added by the IloExpr class: Example: y1 + y2 ≤ 4 IloExpr myExpr(env). 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.getObjValue() << endl. myExpr += y[2].end(). cplex. model. Did we succeed .. myExpr += y[1]. equalities. Now we are ready to start the solver: IloCplex cplex(model).. not initially included to the model (e. expr)).g.add(IloMinimize(env. } else { // model solved to optimality env. // 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.Programming Exercise CPLEX Tutorial Framework/Instances Programming Exercise CPLEX Tutorial Framework/Instances Constraints Constraints.

we can easily separate cycle elimination cuts by performing shortest path computations If we have found a valid cut.50 6 0 2 12246.25 12326.. locally: addLocal(cut). Best Node ItCnt 11258. the gap is 7.25 12270. 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. we are ﬁnished when the gap is 0% (proven optimality). 17 18 globally: add(cut). we ﬁrst need to inspect the current values of our variables: IloNum a = getValue(x[i]).11% 7..25..g. indicated with * in the ﬁrst column.25 17 2 4 12338.00 6 24 22 13102. ﬁrst integral solution in line 5 at node 22 of B&B tree. the cycle-elimination-cuts) As we already know from the lecture. build the corresponding expression cut we can add it to the model . Based on this values we can now build/update some data-structures.50 17 . apply some algorithms and detect some cutting-planes (e.25 19 27 23 12463. still in root node of B&B tree..25 12270. 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).50 109 110 112 113 116 123 Cuts/ Gap Variable B NodeID Parent x_[96] D x_[51] U 7. 1 2 Numeric Issues In particular for the cut separation we need to take care about numeric issues.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..0000 Comments: Second line.0000 13109.50 4 * 25 21 integral 0 26 22 12372.)./yourprogram type run to start the program type bt or backtrace to see the execution stack This will show you e. 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). after the separation of 26 internal cuts the LP relaxation is better (higher).75 19 1 3 12311.25 12270. 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.11%.Programming Exercise CPLEX Tutorial Framework/Instances Programming Exercise CPLEX Tutorial Framework/Instances Cut Generation (2) When CPLEX calls such a callback object. the line number in which the error or segmentation fault occurs. .40% 6. The tool valgrind can be used for the detection of memory leaks 19 20 13210.11% 6.25 12270. * 22 22 integral 0 23 22 13083.. the best integer solution value is 13210.11% 7.0000 13109. For developing use -p -g which includes proﬁling and debugging information.0000 13210.getParam(IloCplex::EpInt) and set by cplex.50 18 .0..0000 13210..g. Assume we speciﬁed the constraint x1 + x2 + x3 = 3 A solution might only satisfy (x1 ± ) + (x2 ± ) + (x3 ± ) = 3 for some small .40% 5. whereas the LP-relaxation is 12270.25 27 12270.00 2 User: 26 18 12268. This should be used for the runs for creating the results (otherwise particularily the cut-generation will be too slow).0000 13109. Debugger: start with gdb --args . the last line with * is the node where the optimum has been found. Best Int.setParam(.25 24 12270.

writing to stdout (by providing appropriate operators << and >>) kMST BC: this class should contain the ILP-based algorithms. parameter handling Instance: singleton class. we are actually interested in ﬁnding a k-MST of nodes {1. responsible for reading the data instances. u int> E deﬁnes 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. Tools: provides various useful functions that do not ﬁt to one particular class EdgeIO: reading the edges from the input ﬁle. . |V |} !! Be careful. edge weight) Example: 20 35 0. 1. i. 93.e. 23. 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 . the test instances already include the artiﬁcial root node 0 Hence. converting the data in basic data structures. 2. 5. . to handle this accordingy w. parameters etc. nEdges typedef std::pair<u int. 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. .t the number of connected nodes k! You can download six test instances ranging from 10 to 100 nodes from the course webpage. 3. and providing them to other classes Global: singleton class. 56. 4. target node.r. containing global variables.Programming Exercise CPLEX Tutorial Framework/Instances Programming Exercise CPLEX Tutorial Framework/Instances Test instances To make life easier. 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. 21 22 Programming Exercise CPLEX Tutorial Framework/Instances Programming Exercise CPLEX Tutorial Framework/Instances k-MST Framework Main: starting the program. .

/yourprogram .cpp shows how to use the algorithm src/Makefile compiles test. ⇒ 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 . containing the edge list list<u int> path and the path length double length DheaMaxFlow The class DheaMaxFlow provides an eﬃcient max-ﬂow algorithm.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. Nevertheless. It does not even use a priority queue.. test.-) when working on the server (behemoth) start your programs with nice with lower priority \$ nice . and thus has runtime O(|V |3 ). this is “enough” for the exercise.. which can be used to compute the minimum cut. as they provide all necessary functionality If you want more: code yourself .-) use graph/algorithm libraries like LEDA or boost. but must ﬁt the needs of all participants. The compute server has four cores. which are also available on the servers be nice . 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.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.

Programming Exercise CPLEX Tutorial Framework/Instances Questions ? Good luck for the programming exercise! Have fun coding! If you have any questions. don’t hesitate to ask me (us)! 29 .