Actually, what I'm trying is the selective TSP which connects tour with not entire nodes under specific condition.(Example: if there are 20 nodes, this problem will select 12 nodes to make tour)
And I copied this subtour elimination code from https://www.gurobi.com/documentation/7.0/examples/tsp_java.html.
protected void callback() {
try {
if (where == GRB.CB_MIPSOL) {
// Found an integer feasible solution - does it visit every
// node?
int n = vars.length;
int[] tour = findsubtour(getSolution(vars));
if (tour.length < n && tour.length>=2) {
// Add subtour elimination constraint
GRBLinExpr expr = new GRBLinExpr();
for (int i = 0; i < tour.length; i++)
for (int j = i + 1; j < tour.length; j++)
expr.addTerm(1.0, vars[tour[i]][tour[j]]);
addLazy(expr, GRB.LESS_EQUAL, tour.length - 1);
addLazy(expr, GRB.GREATER_EQUAL, 1);
}
}
} catch (GRBException e) {
System.out.println("Error code: " + e.getErrorCode() + ". " + e.getMessage());
e.printStackTrace();
}
}
and it loads upper part in Main function as
if (model.get(GRB.IntAttr.SolCount) > 0) {
int[] tour = findsubtour(model.get(GRB.DoubleAttr.X, y_j1_j2));
assert tour.length == cent_x.length;
System.out.print("Tour: ");
for (int i = 0; i < tour.length; i++)
System.out.print(String.valueOf(tour[i]) + " ");
System.out.println();
}
However, when I run this code, it keeps making solutions with subtour. Is there any way to eliminate the subtour? Sorry for my english skills, if there are difficult parts to understand, please add a comment. I really want to fix this problem.