@Test public final void testPerformOperator() { Map<Integer, Integer[]> correct = buildIsCorrect(); StringingT2 stringT2 = new StringingT2(); double[][] tour = getSimpleTour(); for (int i = 0; i < tour.length; i++) { int valueToRemoveInsert = i; Integer[] intTour = remove(transform(tour), valueToRemoveInsert); Integer[] result = stringT2.performOperator(intTour, valueToRemoveInsert, 4, getDistancecalculator(tour)).getTour(); logger.debug("Result tour for index {} is {}", i, Arrays.toString(result)); if (!isOrderCorrect(i, result, correct)) { fail("False result for index " + i + "."); } } }
@Override public OpResult performOperator(Integer[] tour, Integer toString, int neighbourhoodSize, IDistanceCalculator distanceCalculator) { int counterNull = 0; int counterNotNull = 0; int[] neigbourHoodJ = Util.getNeighbourhoodIndexesFromValue(tour, toString, neighbourhoodSize, distanceCalculator); Integer[] bestTour = null; double bestCost = Double.MAX_VALUE; for (int i = 0; i < tour.length; i++) { int iNext = (i + 1) % tour.length; iNext = (iNext < 0) ? iNext + tour.length : iNext; int[] neigbourHoodK = Util.getNeighbourhoodIndexesFromIndexInTour(tour, iNext, neighbourhoodSize, distanceCalculator); for (int j : neigbourHoodJ) { if (i == j) { Map<Integer, OpNode> tourModel = Util.constrcutOpNodes(tour); int indexToString = tour.length; tourModel.put(indexToString, new OpNode(toString)); tourModel.get(i).insert(tourModel.get(indexToString)); tourModel.get(indexToString).insert(tourModel.get(iNext)); double cost = Util.getDistance(tourModel, distanceCalculator); if (cost < bestCost) { bestCost = cost; bestTour = Util.getTour(tourModel, toString).getTour(); } } else { int jNext = (j + 1) % tour.length; jNext = (jNext < 0) ? jNext + tour.length : jNext; int[] neigbourHoodL = Util.getNeighbourhoodIndexesFromIndexInTour(tour, jNext, neighbourhoodSize, distanceCalculator); for (int k : neigbourHoodK) { if (k != j && k != jNext) { for (int l : neigbourHoodL) { if (l != i && i != iNext) { Map<Integer, OpNode> workWithTourModel = Util.constrcutOpNodes(tour); int indexToString = tour.length; workWithTourModel.put(indexToString, new OpNode(toString)); int kPrevious = (k - 1) % tour.length; kPrevious = (kPrevious < 0) ? kPrevious + tour.length : kPrevious; int lPrevious = (l - 1) % tour.length; lPrevious = (lPrevious < 0) ? lPrevious + tour.length : lPrevious; workWithTourModel.get(i).deleteTo(); workWithTourModel.get(lPrevious).deleteTo(); workWithTourModel.get(j).deleteTo(); workWithTourModel.get(kPrevious).deleteTo(); workWithTourModel.get(i).insert(workWithTourModel.get(indexToString)); workWithTourModel.get(indexToString).insert(workWithTourModel.get(j)); workWithTourModel.get(l).insert(workWithTourModel.get(jNext)); workWithTourModel.get(kPrevious).insert(workWithTourModel.get(lPrevious)); workWithTourModel.get(iNext).insert(workWithTourModel.get(k)); TourResult result = Util.getTour(workWithTourModel, toString); if (result.isValidTour()) { counterNotNull++; double cost = Util.getDistance(result.getTour(), distanceCalculator); if (cost < bestCost) { bestCost = cost; bestTour = result.getTour(); } } else { counterNull++; } } } } } } } } return new OpResult(bestTour, bestCost); }
StringingT2 implements IStringingOperator { @Override public OpResult performOperator(Integer[] tour, Integer toString, int neighbourhoodSize, IDistanceCalculator distanceCalculator) { int counterNull = 0; int counterNotNull = 0; int[] neigbourHoodJ = Util.getNeighbourhoodIndexesFromValue(tour, toString, neighbourhoodSize, distanceCalculator); Integer[] bestTour = null; double bestCost = Double.MAX_VALUE; for (int i = 0; i < tour.length; i++) { int iNext = (i + 1) % tour.length; iNext = (iNext < 0) ? iNext + tour.length : iNext; int[] neigbourHoodK = Util.getNeighbourhoodIndexesFromIndexInTour(tour, iNext, neighbourhoodSize, distanceCalculator); for (int j : neigbourHoodJ) { if (i == j) { Map<Integer, OpNode> tourModel = Util.constrcutOpNodes(tour); int indexToString = tour.length; tourModel.put(indexToString, new OpNode(toString)); tourModel.get(i).insert(tourModel.get(indexToString)); tourModel.get(indexToString).insert(tourModel.get(iNext)); double cost = Util.getDistance(tourModel, distanceCalculator); if (cost < bestCost) { bestCost = cost; bestTour = Util.getTour(tourModel, toString).getTour(); } } else { int jNext = (j + 1) % tour.length; jNext = (jNext < 0) ? jNext + tour.length : jNext; int[] neigbourHoodL = Util.getNeighbourhoodIndexesFromIndexInTour(tour, jNext, neighbourhoodSize, distanceCalculator); for (int k : neigbourHoodK) { if (k != j && k != jNext) { for (int l : neigbourHoodL) { if (l != i && i != iNext) { Map<Integer, OpNode> workWithTourModel = Util.constrcutOpNodes(tour); int indexToString = tour.length; workWithTourModel.put(indexToString, new OpNode(toString)); int kPrevious = (k - 1) % tour.length; kPrevious = (kPrevious < 0) ? kPrevious + tour.length : kPrevious; int lPrevious = (l - 1) % tour.length; lPrevious = (lPrevious < 0) ? lPrevious + tour.length : lPrevious; workWithTourModel.get(i).deleteTo(); workWithTourModel.get(lPrevious).deleteTo(); workWithTourModel.get(j).deleteTo(); workWithTourModel.get(kPrevious).deleteTo(); workWithTourModel.get(i).insert(workWithTourModel.get(indexToString)); workWithTourModel.get(indexToString).insert(workWithTourModel.get(j)); workWithTourModel.get(l).insert(workWithTourModel.get(jNext)); workWithTourModel.get(kPrevious).insert(workWithTourModel.get(lPrevious)); workWithTourModel.get(iNext).insert(workWithTourModel.get(k)); TourResult result = Util.getTour(workWithTourModel, toString); if (result.isValidTour()) { counterNotNull++; double cost = Util.getDistance(result.getTour(), distanceCalculator); if (cost < bestCost) { bestCost = cost; bestTour = result.getTour(); } } else { counterNull++; } } } } } } } } return new OpResult(bestTour, bestCost); } }
StringingT2 implements IStringingOperator { @Override public OpResult performOperator(Integer[] tour, Integer toString, int neighbourhoodSize, IDistanceCalculator distanceCalculator) { int counterNull = 0; int counterNotNull = 0; int[] neigbourHoodJ = Util.getNeighbourhoodIndexesFromValue(tour, toString, neighbourhoodSize, distanceCalculator); Integer[] bestTour = null; double bestCost = Double.MAX_VALUE; for (int i = 0; i < tour.length; i++) { int iNext = (i + 1) % tour.length; iNext = (iNext < 0) ? iNext + tour.length : iNext; int[] neigbourHoodK = Util.getNeighbourhoodIndexesFromIndexInTour(tour, iNext, neighbourhoodSize, distanceCalculator); for (int j : neigbourHoodJ) { if (i == j) { Map<Integer, OpNode> tourModel = Util.constrcutOpNodes(tour); int indexToString = tour.length; tourModel.put(indexToString, new OpNode(toString)); tourModel.get(i).insert(tourModel.get(indexToString)); tourModel.get(indexToString).insert(tourModel.get(iNext)); double cost = Util.getDistance(tourModel, distanceCalculator); if (cost < bestCost) { bestCost = cost; bestTour = Util.getTour(tourModel, toString).getTour(); } } else { int jNext = (j + 1) % tour.length; jNext = (jNext < 0) ? jNext + tour.length : jNext; int[] neigbourHoodL = Util.getNeighbourhoodIndexesFromIndexInTour(tour, jNext, neighbourhoodSize, distanceCalculator); for (int k : neigbourHoodK) { if (k != j && k != jNext) { for (int l : neigbourHoodL) { if (l != i && i != iNext) { Map<Integer, OpNode> workWithTourModel = Util.constrcutOpNodes(tour); int indexToString = tour.length; workWithTourModel.put(indexToString, new OpNode(toString)); int kPrevious = (k - 1) % tour.length; kPrevious = (kPrevious < 0) ? kPrevious + tour.length : kPrevious; int lPrevious = (l - 1) % tour.length; lPrevious = (lPrevious < 0) ? lPrevious + tour.length : lPrevious; workWithTourModel.get(i).deleteTo(); workWithTourModel.get(lPrevious).deleteTo(); workWithTourModel.get(j).deleteTo(); workWithTourModel.get(kPrevious).deleteTo(); workWithTourModel.get(i).insert(workWithTourModel.get(indexToString)); workWithTourModel.get(indexToString).insert(workWithTourModel.get(j)); workWithTourModel.get(l).insert(workWithTourModel.get(jNext)); workWithTourModel.get(kPrevious).insert(workWithTourModel.get(lPrevious)); workWithTourModel.get(iNext).insert(workWithTourModel.get(k)); TourResult result = Util.getTour(workWithTourModel, toString); if (result.isValidTour()) { counterNotNull++; double cost = Util.getDistance(result.getTour(), distanceCalculator); if (cost < bestCost) { bestCost = cost; bestTour = result.getTour(); } } else { counterNull++; } } } } } } } } return new OpResult(bestTour, bestCost); }  }
StringingT2 implements IStringingOperator { @Override public OpResult performOperator(Integer[] tour, Integer toString, int neighbourhoodSize, IDistanceCalculator distanceCalculator) { int counterNull = 0; int counterNotNull = 0; int[] neigbourHoodJ = Util.getNeighbourhoodIndexesFromValue(tour, toString, neighbourhoodSize, distanceCalculator); Integer[] bestTour = null; double bestCost = Double.MAX_VALUE; for (int i = 0; i < tour.length; i++) { int iNext = (i + 1) % tour.length; iNext = (iNext < 0) ? iNext + tour.length : iNext; int[] neigbourHoodK = Util.getNeighbourhoodIndexesFromIndexInTour(tour, iNext, neighbourhoodSize, distanceCalculator); for (int j : neigbourHoodJ) { if (i == j) { Map<Integer, OpNode> tourModel = Util.constrcutOpNodes(tour); int indexToString = tour.length; tourModel.put(indexToString, new OpNode(toString)); tourModel.get(i).insert(tourModel.get(indexToString)); tourModel.get(indexToString).insert(tourModel.get(iNext)); double cost = Util.getDistance(tourModel, distanceCalculator); if (cost < bestCost) { bestCost = cost; bestTour = Util.getTour(tourModel, toString).getTour(); } } else { int jNext = (j + 1) % tour.length; jNext = (jNext < 0) ? jNext + tour.length : jNext; int[] neigbourHoodL = Util.getNeighbourhoodIndexesFromIndexInTour(tour, jNext, neighbourhoodSize, distanceCalculator); for (int k : neigbourHoodK) { if (k != j && k != jNext) { for (int l : neigbourHoodL) { if (l != i && i != iNext) { Map<Integer, OpNode> workWithTourModel = Util.constrcutOpNodes(tour); int indexToString = tour.length; workWithTourModel.put(indexToString, new OpNode(toString)); int kPrevious = (k - 1) % tour.length; kPrevious = (kPrevious < 0) ? kPrevious + tour.length : kPrevious; int lPrevious = (l - 1) % tour.length; lPrevious = (lPrevious < 0) ? lPrevious + tour.length : lPrevious; workWithTourModel.get(i).deleteTo(); workWithTourModel.get(lPrevious).deleteTo(); workWithTourModel.get(j).deleteTo(); workWithTourModel.get(kPrevious).deleteTo(); workWithTourModel.get(i).insert(workWithTourModel.get(indexToString)); workWithTourModel.get(indexToString).insert(workWithTourModel.get(j)); workWithTourModel.get(l).insert(workWithTourModel.get(jNext)); workWithTourModel.get(kPrevious).insert(workWithTourModel.get(lPrevious)); workWithTourModel.get(iNext).insert(workWithTourModel.get(k)); TourResult result = Util.getTour(workWithTourModel, toString); if (result.isValidTour()) { counterNotNull++; double cost = Util.getDistance(result.getTour(), distanceCalculator); if (cost < bestCost) { bestCost = cost; bestTour = result.getTour(); } } else { counterNull++; } } } } } } } } return new OpResult(bestTour, bestCost); }  @Override OpResult performOperator(Integer[] tour, Integer toString, int neighbourhoodSize, IDistanceCalculator distanceCalculator); }
StringingT2 implements IStringingOperator { @Override public OpResult performOperator(Integer[] tour, Integer toString, int neighbourhoodSize, IDistanceCalculator distanceCalculator) { int counterNull = 0; int counterNotNull = 0; int[] neigbourHoodJ = Util.getNeighbourhoodIndexesFromValue(tour, toString, neighbourhoodSize, distanceCalculator); Integer[] bestTour = null; double bestCost = Double.MAX_VALUE; for (int i = 0; i < tour.length; i++) { int iNext = (i + 1) % tour.length; iNext = (iNext < 0) ? iNext + tour.length : iNext; int[] neigbourHoodK = Util.getNeighbourhoodIndexesFromIndexInTour(tour, iNext, neighbourhoodSize, distanceCalculator); for (int j : neigbourHoodJ) { if (i == j) { Map<Integer, OpNode> tourModel = Util.constrcutOpNodes(tour); int indexToString = tour.length; tourModel.put(indexToString, new OpNode(toString)); tourModel.get(i).insert(tourModel.get(indexToString)); tourModel.get(indexToString).insert(tourModel.get(iNext)); double cost = Util.getDistance(tourModel, distanceCalculator); if (cost < bestCost) { bestCost = cost; bestTour = Util.getTour(tourModel, toString).getTour(); } } else { int jNext = (j + 1) % tour.length; jNext = (jNext < 0) ? jNext + tour.length : jNext; int[] neigbourHoodL = Util.getNeighbourhoodIndexesFromIndexInTour(tour, jNext, neighbourhoodSize, distanceCalculator); for (int k : neigbourHoodK) { if (k != j && k != jNext) { for (int l : neigbourHoodL) { if (l != i && i != iNext) { Map<Integer, OpNode> workWithTourModel = Util.constrcutOpNodes(tour); int indexToString = tour.length; workWithTourModel.put(indexToString, new OpNode(toString)); int kPrevious = (k - 1) % tour.length; kPrevious = (kPrevious < 0) ? kPrevious + tour.length : kPrevious; int lPrevious = (l - 1) % tour.length; lPrevious = (lPrevious < 0) ? lPrevious + tour.length : lPrevious; workWithTourModel.get(i).deleteTo(); workWithTourModel.get(lPrevious).deleteTo(); workWithTourModel.get(j).deleteTo(); workWithTourModel.get(kPrevious).deleteTo(); workWithTourModel.get(i).insert(workWithTourModel.get(indexToString)); workWithTourModel.get(indexToString).insert(workWithTourModel.get(j)); workWithTourModel.get(l).insert(workWithTourModel.get(jNext)); workWithTourModel.get(kPrevious).insert(workWithTourModel.get(lPrevious)); workWithTourModel.get(iNext).insert(workWithTourModel.get(k)); TourResult result = Util.getTour(workWithTourModel, toString); if (result.isValidTour()) { counterNotNull++; double cost = Util.getDistance(result.getTour(), distanceCalculator); if (cost < bestCost) { bestCost = cost; bestTour = result.getTour(); } } else { counterNull++; } } } } } } } } return new OpResult(bestTour, bestCost); }  @Override OpResult performOperator(Integer[] tour, Integer toString, int neighbourhoodSize, IDistanceCalculator distanceCalculator);  }