h1. The Java Interface to CPLEX

The use of CPLEX in Java is based around the class IloCplex (documented [here|http://pic.dhe.ibm.com/infocenter/cosinfoc/v12r5/index.jsp?topic=%2Filog.odms.cplex.help%2Frefjavacplex%2Fhtml%2Filog%2Fcplex%2FIloCplex.html]).  The basic idea is that you create an IloCplex object for your optimization problem, then add variables, the objective, and constraints using methods in the class IloCplex.  The IloCplex object can produce [IloNumVar|http://pic.dhe.ibm.com/infocenter/cosinfoc/v12r5/index.jsp?topic=%2Filog.odms.cplex.help%2Frefjavacplex%2Fhtml%2Filog%2Fconcert%2FIloNumVar.html] objects and their subclass [IloIntVar|http://pic.dhe.ibm.com/infocenter/cosinfoc/v12r5/index.jsp?topic=%2Filog.odms.cplex.help%2Frefjavacplex%2Fhtml%2Filog%2Fconcert%2FIloIntVar.html] objects, when are then used as arguments to further methods from IloCplex to make the objective and constraints.  The IloCplex interface is somewhat confusing.  It is very large, has lots of redundant methods, and lots of methods that appear to be the same but produce very different results.  We now summarize the methods of IloCplex which will be of use to us:

||Name||Return Type||Arguments||Description||
|boolVar|IloIntVar| |Creates and returns a new Boolean variable (domain 0,1).|
|boolVarArray|IloIntVar[]|int n |Creates and returns an array of n new Boolean variables (domain 0,1)|
|linearIntExpr|IloLinearIntExpr| |Creates and returns an integer linear expression initialized as 0 (zero).|
|addGe|IloRange|IloNumExpr e, double v|Creates and returns a range representing the constraint {mathinline} e \geq v{mathinline}|
|addEq|IloRange|IloNumExpr e, double v|Creates and returns a range representing the constraint {mathinline} e = v{mathinline}|
|addMinimize|IloObjective|IloNumExpr e|Creates and returns an objective to minimize the expression and adds it to the invoking model.|

{warning:title=Warning}
For an IloCplex cplex, an IloNumExpr e and a double v, calling cplex.addGe(e,v) and cplex.addGe(v,e) are both allowed but do not produce the same result!  The first gives the constraint {mathinline} e \geq v{mathinline} while the second gives the constraint {mathinline}v \geq e{mathinline}.
{warning}

{warning:title=Warning}
For an IloCplex cplex, an IloNumExpr e and a double v, calling cplex.ge(e,v) and cplex.addGe(e,v) are both allowed but do not produce the same result!  While both return an object for the constraint {mathinline} e \geq v{mathinline}, only the latter adds the constraint to the model!  We will actually have use cplex.ge(e,v) later when we add constraints through callbacks instead of adding them directly to the model.
{warning}

h1. 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}
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}