Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Section
Column
width300px
Page Tree
root15DOTs60ia13:Tutorial
Column

Using CPLEX in TspIpSolver

First, we need to set up the objective and the degree constraints. First, add the following fields to the class

Code Block
private IloCplex cplex;
private TspInstance<V,E> tspInstance;
private final ImmutableBiMap<E,IloIntVar> edgeVariables;

and initialize them, as below.

Code Block
	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		
	}

The constraints and objective still need to be added to the cplex object. Try adding them yourself! The following methods (as defined in Solver Specification and Java Style for CPLEX) should be useful for making the constraints:

  • From Util, the static method integerSum(IloCplex cplex, BiMap<T,IloIntVar> variables, Iterable<T> set)
  • From IloCplex, the method addEq(IloNumExpr e, double v)
  • From UndirectedGraph<V,E>, the method getIncidentEdges(V vertex)

If you are unfamiliar with Java, consider viewing the solution for the constraint, then trying the objective yourself.

Toggle Cloak
idConstraintsSolution
Solution

Cloak
visiblefalse
idConstraintsSolution

For the objective, we need the functions:

  • From Util, the static method sum(IloCplex cplex, BiMap<T,IloIntVar> variables, Iterable<T> set, Function<? super T,? extends Number> coefficients)
  • From UndirectedGraph<V,E>, the method getEdges()
  • From TspInstance, the method getEdgeWeights

Toggle Cloak
idObjectiveSolution
Solution

Cloak
visiblefalse
idObjectiveSolution