Professional Documents
Culture Documents
CPLEX Tutorial
Framework/Instances
Programming Exercise
CPLEX Tutorial
Framework/Instances
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
Programming Exercise
CPLEX Tutorial
Framework/Instances
Programming Exercise
CPLEX Tutorial
Framework/Instances
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
and
Programming Exercise
CPLEX Tutorial
Framework/Instances
Programming Exercise
CPLEX Tutorial
Framework/Instances
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
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
13
Programming Exercise
CPLEX Tutorial
Framework/Instances
Programming Exercise
CPLEX Tutorial
Framework/Instances
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
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
Programming Exercise
CPLEX Tutorial
Framework/Instances
Programming Exercise
CPLEX Tutorial
Framework/Instances
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
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
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
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