Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin
{
Wiki Markup
Composition Setup
Section
Column
width300px
Page Tree
rootTutorial
Column

This article is a tutorial on using callbacks, a group of advanced CPLEX features. The Traveling Salesmen Problem (TSP) will be used as a running example. CPLEX will be accessed through the Java Concert Technology interface. It will be assumed that the reader has completed all of the prerequisites given here. To navigate, use the menu on the left. Click on Getting Started.

} {composition-setup} {pagetree:root=Tutorial} h1. The Task & Project Organization You are about to build a TSP solver capable of solving TSP instances with over 500 cities to optimality. The solver will require a variety of algorithms for known combinatorial problems and efficient data structures. These algorithms and data structures have been provided for you, either through libraries (JARs) distributed with the project, or code that has already been written for you. The libraries distributed with the project are located in the folder _tspSolver/lib/_. We now summarize their functions: * The file _cplex.jar_ provides a Java interface to CPLEX. * The file _guava-14.0-rc1.jar_ contains the [Guava|http://code.google.com/p/guava-libraries/wiki/GuavaExplained?tm=6] libary. Guava provides some extra features not distributed in the base Java libraries. In particular, it gives a few extra data structures which we will use, most notably the [Bimap|http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/BiMap.html]. * The subdirectory _jung/_ contains the collection of JARs needed to use the [JUNG library|http://jung.sourceforge.net/], which provide the graph data structure we will use to store our TSP instances and algorithms for solving many classical combinatorial problems on graphs including max flow min cut, minimum spanning tree, and connected components. * The subdirectory _jgrapht/_ contains the collection of JARs needed to use the [JGraphT library|http://jgrapht.org/], another graphical library similar to JUNG. It is no longer under active development (so generally it is better to use JUNG), but it contains a few tools not included in JUNG that we will need for extracting [Eulerian tours|http://en.wikipedia.org/wiki/Eulerian_path]. In algorithms and data structures implemented for you are located in the directories _tspSolver/src/instances_ and _tspSolver/src/solver/_. Most importantly, the code here includes * _src/instances/TspInstance.java_ provides the data structures used the store the graph and edge weights that are the inputs for solving a TSP problem. Some utility methods for computing properties of cuts and tours are also provided. * _solver/Util.java_ provides a few methods for making the CPLEX API more Java friendly and integrating the various libraries we are using. * _src/solver/MinCutSolver.java_ provides a light weight wrapper around [the JUNG implementation of the Edmonds Karp algorithm|http://jung.sourceforge.net/doc/api/edu/uci/ics/jung/algorithms/flows/EdmondsKarpMaxFlow.html] for solving max flow min cut. * _src/solver/ChristofidesSolver.java_ provides an implementation of Christofides algorithm that can additionally take a set of suggested edges as a _hint_. * _src/solver/TwoOpt.java_ provides a naïve implementation of the the [two opt|http://www2.research.att.com/~dsj/papers/TSPchapter.pdf] local search algorithm. To complete the tutorial, all you need to do is to put everything together. The file _tspSolver/src/solver/TspIpSolver.java_ is the only file you will need to modify. It begins almost blank, and we provide step by step instructions on how to complete it. You will test your code on small hand coded test instances using the [JUnit test framework|http://www.vogella.com/articles/JUnit/article.html], located in _tspSolver/test/solver/_, and on larger instances in from the (famous) [TSPLIB|http://comopt.ifi.uni-heidelberg.de/software/TSPLIB95/] test set. The project is distributed with the TSPLIB data in the directory _tspSolver/sampleData/TSPLIB_. The code to create the {{TspInstance}} objects by parsing the data files is in _tspSolver/src/tspLib/TspLibParser.java_, but has already been called for you from _tspSolver/src/main/Main.java_, the entry point for the program. h1. Setting up the problem Open the file _src/solver/TspIpSolver.java_ by double clicking it from the Project Explorer. This is the file we will be primarily editing. It should contain the following (after the imports): {toggle-cloak:id=TspIpSolverStart} _Toggle TspIpSolver.java_ {cloak:id=TspIpSolverStart|visible=true} {code} public class TspIpSolver<V,E> { public static enum Option{ lazy,userCut, randomizedUserCut, christofidesApprox, christofidesHeuristic,twoOpt,incumbent; } public TspIpSolver(TspInstance<V,E> tspInstance) throws IloException{ this(tspInstance,EnumSet.of(Option.lazy, Option.userCut, Option.christofidesApprox, Option.christofidesHeuristic)); } public TspIpSolver(TspInstance<V,E> tspInstance, EnumSet<Option> options) throws IloException{} public void solve() throws IloException{ } public ImmutableSet<E> getEdgesInOpt(){ return null; } public double getOptVal(){ return 0; } } {code} {cloak} The {{Option}} arguments will be needed later. First, we need to set up the objective and the degree constraints. First, add the following fields to the class {code} private IloCplex cplex; private TspInstance<V,E> tspInstance; private final ImmutableBiMap<E,IloIntVar> edgeVariables; {code} and initialize them, as below. {code} public TspIpSolver(TspInstance<V,E> tspInstance, EnumSet<Option> options) throws IloException{ this.options = options; this.tspInstance = tspInstance; this.cplex = new IloCplex(); UndirectedGraph<V,E> graph = tspInstance.getGraph(); //for convenience, we will be using this a lot this.edgeVariables = Util.makeBinaryVariables(cplex, graph.getEdges()); //the degree constraints //the objective } {code} The constraints and objective still need to be added to the {{cplex}} object. Try adding them yourself! The following methods should be useful for making the constraints: * From {{Util}}, {{public static <T> IloLinearIntExpr integerSum(IloCplex cplex, BiMap<T,IloIntVar> variables, Iterable<T> set)}} ** For each element {mathinline}e{mathinline} of {{set}}, finds the corresponding variable {mathinline}x_e{mathinline} and returns {mathinline}\sum_{e \in \text{set}} x_e {mathinline} * From {{IloCplex}}, {{public IloRange addEq(IloNumExpr e, double v)}} ** Adds the equality constraint e = v * From {{UndirectedGraph<V,E>}}, {{public Collection<E> getIncidentEdges(V vertex)}} ** returns the edges of the graph that are incident to vertex If you are unfamiliar with Java, consider viewing the solution for the constraint, then trying the objective yourself. {toggle-cloak:id=ConstraintsSolution} _Solution_ {cloak:id=ConstraintsSolution|visible=false} {code} //the degree constraints for(V vertex: graph.getVertices()){ cplex.addEq(Util.integerSum(cplex, edgeVariables, graph.getIncidentEdges(vertex)), 2); } {code} {cloak} For the objective, we need the functions: * From {{Util}}, {{public static <T> IloLinearNumExpr sum(IloCplex cplex, BiMap<T,IloIntVar> variables, Iterable<T> set, Function<? super T,? extends Number> coefficients)}} ** For every element {mathinline}e{mathinline} of {{set}}, gets the corresponding variable {mathinline}x_e{mathinline} from {{variables}} and the number {mathinline}d_e{mathinline} from {{coefficients}} and returns an expression for {mathinline}\sum_{e \in \text{set}} d_e x_e {mathinline}. {toggle-cloak:id=ObjectiveSolution} _Solution_ {cloak:id=ObjectiveSolution|visible=false} {code} //the objective cplex.addMinimize(Util.integerSum( cplex, edgeVariables, graph.getEdges(),tspInstance.getEdgeWeights())); {code} {cloak} h1. A First Solution by LazyConstraintCallback h1. Interesting TSP References. On solving TSPs * [http://www.seas.gwu.edu/~simhaweb/champalg/tsp/tsp.html] * On Construction and Improvement Heuristics: [http://www2.research.att.com/~dsj/papers/TSPchapter.pdf] TSP Problem Instances * [http://comopt.ifi.uni-heidelberg.de/software/TSPLIB95/]