@Test public final void testGetNeighbourhoodIndexesAfterDistance() { double[][] tour = getSimpleTour(); int[] neighbourhood3 = Util.getNeighbourhoodIndexesAfterDistance(transform(tour), 1, 4, getDistancecalculator(tour)); checkIfIsIn(neighbourhood3, new int[] { 2, 0, 7, 6 }); int[] neighbourhood4 = Util.getNeighbourhoodIndexesAfterDistance(transform(tour), 7, 4, getDistancecalculator(tour)); checkIfIsIn(neighbourhood4, new int[] { 0, 6, 1, 5 }); }
public static int[] getNeighbourhoodIndexesAfterDistance(Integer[] tour, int neighbourhoodFrom, int neighbourhoodSize, IDistanceCalculator distanceCalculator) { neighbourhoodFrom = neighbourhoodFrom % tour.length; neighbourhoodFrom = (neighbourhoodFrom < 0) ? neighbourhoodFrom + tour.length : neighbourhoodFrom; int[] neighbourhood = null; if (tour.length > neighbourhoodSize) { neighbourhood = new int[neighbourhoodSize]; int left = 1; int right = 1; for (int i = 0; i < neighbourhoodSize; i++) { int indexLeft = (neighbourhoodFrom - left) % tour.length; indexLeft = (indexLeft < 0) ? indexLeft + tour.length : indexLeft; int indexRight = (neighbourhoodFrom + right) % tour.length; indexRight = (indexRight < 0) ? indexRight + tour.length : indexRight; double distanceLeft = distanceCalculator.getDistance(tour[neighbourhoodFrom], tour[indexLeft]); double distanceRight = distanceCalculator.getDistance(tour[neighbourhoodFrom], tour[indexRight]); if (distanceLeft < distanceRight) { neighbourhood[i] = indexLeft; left += 1; } else { neighbourhood[i] = indexRight; right += 1; } } } else { neighbourhood = new int[tour.length - 1]; int filled = 0; for (int i = 0; i < tour.length; i++) { if (i != neighbourhoodFrom) { neighbourhood[filled++] = tour[i]; } } } return neighbourhood; }
Util { public static int[] getNeighbourhoodIndexesAfterDistance(Integer[] tour, int neighbourhoodFrom, int neighbourhoodSize, IDistanceCalculator distanceCalculator) { neighbourhoodFrom = neighbourhoodFrom % tour.length; neighbourhoodFrom = (neighbourhoodFrom < 0) ? neighbourhoodFrom + tour.length : neighbourhoodFrom; int[] neighbourhood = null; if (tour.length > neighbourhoodSize) { neighbourhood = new int[neighbourhoodSize]; int left = 1; int right = 1; for (int i = 0; i < neighbourhoodSize; i++) { int indexLeft = (neighbourhoodFrom - left) % tour.length; indexLeft = (indexLeft < 0) ? indexLeft + tour.length : indexLeft; int indexRight = (neighbourhoodFrom + right) % tour.length; indexRight = (indexRight < 0) ? indexRight + tour.length : indexRight; double distanceLeft = distanceCalculator.getDistance(tour[neighbourhoodFrom], tour[indexLeft]); double distanceRight = distanceCalculator.getDistance(tour[neighbourhoodFrom], tour[indexRight]); if (distanceLeft < distanceRight) { neighbourhood[i] = indexLeft; left += 1; } else { neighbourhood[i] = indexRight; right += 1; } } } else { neighbourhood = new int[tour.length - 1]; int filled = 0; for (int i = 0; i < tour.length; i++) { if (i != neighbourhoodFrom) { neighbourhood[filled++] = tour[i]; } } } return neighbourhood; } }
Util { public static int[] getNeighbourhoodIndexesAfterDistance(Integer[] tour, int neighbourhoodFrom, int neighbourhoodSize, IDistanceCalculator distanceCalculator) { neighbourhoodFrom = neighbourhoodFrom % tour.length; neighbourhoodFrom = (neighbourhoodFrom < 0) ? neighbourhoodFrom + tour.length : neighbourhoodFrom; int[] neighbourhood = null; if (tour.length > neighbourhoodSize) { neighbourhood = new int[neighbourhoodSize]; int left = 1; int right = 1; for (int i = 0; i < neighbourhoodSize; i++) { int indexLeft = (neighbourhoodFrom - left) % tour.length; indexLeft = (indexLeft < 0) ? indexLeft + tour.length : indexLeft; int indexRight = (neighbourhoodFrom + right) % tour.length; indexRight = (indexRight < 0) ? indexRight + tour.length : indexRight; double distanceLeft = distanceCalculator.getDistance(tour[neighbourhoodFrom], tour[indexLeft]); double distanceRight = distanceCalculator.getDistance(tour[neighbourhoodFrom], tour[indexRight]); if (distanceLeft < distanceRight) { neighbourhood[i] = indexLeft; left += 1; } else { neighbourhood[i] = indexRight; right += 1; } } } else { neighbourhood = new int[tour.length - 1]; int filled = 0; for (int i = 0; i < tour.length; i++) { if (i != neighbourhoodFrom) { neighbourhood[filled++] = tour[i]; } } } return neighbourhood; }  }
Util { public static int[] getNeighbourhoodIndexesAfterDistance(Integer[] tour, int neighbourhoodFrom, int neighbourhoodSize, IDistanceCalculator distanceCalculator) { neighbourhoodFrom = neighbourhoodFrom % tour.length; neighbourhoodFrom = (neighbourhoodFrom < 0) ? neighbourhoodFrom + tour.length : neighbourhoodFrom; int[] neighbourhood = null; if (tour.length > neighbourhoodSize) { neighbourhood = new int[neighbourhoodSize]; int left = 1; int right = 1; for (int i = 0; i < neighbourhoodSize; i++) { int indexLeft = (neighbourhoodFrom - left) % tour.length; indexLeft = (indexLeft < 0) ? indexLeft + tour.length : indexLeft; int indexRight = (neighbourhoodFrom + right) % tour.length; indexRight = (indexRight < 0) ? indexRight + tour.length : indexRight; double distanceLeft = distanceCalculator.getDistance(tour[neighbourhoodFrom], tour[indexLeft]); double distanceRight = distanceCalculator.getDistance(tour[neighbourhoodFrom], tour[indexRight]); if (distanceLeft < distanceRight) { neighbourhood[i] = indexLeft; left += 1; } else { neighbourhood[i] = indexRight; right += 1; } } } else { neighbourhood = new int[tour.length - 1]; int filled = 0; for (int i = 0; i < tour.length; i++) { if (i != neighbourhoodFrom) { neighbourhood[filled++] = tour[i]; } } } return neighbourhood; }  static boolean isNull(Map<Integer, OpNode> structure); static int adaptIndex(int index, int tourLength); static String toString(Map<Integer, OpNode> structure); static Map<Integer, OpNode> constrcutOpNodes(Integer[] tour); static Integer[] removeAndCopy(Integer[] tour, int toRemoveIndex); static Integer[] remove(Integer[] tour, int indexI); static double getDistance(Map<Integer, OpNode> structure, IDistanceCalculator distanceCalculator); static Integer[] getTour(Map<Integer, OpNode> tour); static TourResult getTour(Map<Integer, OpNode> tour, int startIndex); static double getDistance(Integer[] tour, IDistanceCalculator distanceCalculator); static void revert(Map<Integer, OpNode> tour, int startRevert, int endRevert); static void insert(Map<Integer, OpNode> structure, int from, int to); static Integer[] revertAndCopy(Integer[] tour, int startRevert, int endrevert); static int[] getNeighbourhoodIndexesAfterDistance(Integer[] tour, int neighbourhoodFrom, int neighbourhoodSize,
			IDistanceCalculator distanceCalculator); static int[] getNeighbourhoodIndexesFromIndexInTour(Integer[] tour, int neighbourhoodFromIndex, int neighbourhoodSize,
			IDistanceCalculator distanceCalculator); static int[] getNeighbourhoodIndexesFromValue(Integer[] tour, Integer neighbourhoodFromValue, int neighbourhoodSize,
			IDistanceCalculator distanceCalculator); static int[] getNeighbourhoodIndexesAfterDistanceOnTour(Integer[] tour, int neighbourhoodFrom, int neighbourhoodSize,
			IDistanceCalculator distanceCalculator); static final int[] getIntersection(int[] setA, int[] setB); }
Util { public static int[] getNeighbourhoodIndexesAfterDistance(Integer[] tour, int neighbourhoodFrom, int neighbourhoodSize, IDistanceCalculator distanceCalculator) { neighbourhoodFrom = neighbourhoodFrom % tour.length; neighbourhoodFrom = (neighbourhoodFrom < 0) ? neighbourhoodFrom + tour.length : neighbourhoodFrom; int[] neighbourhood = null; if (tour.length > neighbourhoodSize) { neighbourhood = new int[neighbourhoodSize]; int left = 1; int right = 1; for (int i = 0; i < neighbourhoodSize; i++) { int indexLeft = (neighbourhoodFrom - left) % tour.length; indexLeft = (indexLeft < 0) ? indexLeft + tour.length : indexLeft; int indexRight = (neighbourhoodFrom + right) % tour.length; indexRight = (indexRight < 0) ? indexRight + tour.length : indexRight; double distanceLeft = distanceCalculator.getDistance(tour[neighbourhoodFrom], tour[indexLeft]); double distanceRight = distanceCalculator.getDistance(tour[neighbourhoodFrom], tour[indexRight]); if (distanceLeft < distanceRight) { neighbourhood[i] = indexLeft; left += 1; } else { neighbourhood[i] = indexRight; right += 1; } } } else { neighbourhood = new int[tour.length - 1]; int filled = 0; for (int i = 0; i < tour.length; i++) { if (i != neighbourhoodFrom) { neighbourhood[filled++] = tour[i]; } } } return neighbourhood; }  static boolean isNull(Map<Integer, OpNode> structure); static int adaptIndex(int index, int tourLength); static String toString(Map<Integer, OpNode> structure); static Map<Integer, OpNode> constrcutOpNodes(Integer[] tour); static Integer[] removeAndCopy(Integer[] tour, int toRemoveIndex); static Integer[] remove(Integer[] tour, int indexI); static double getDistance(Map<Integer, OpNode> structure, IDistanceCalculator distanceCalculator); static Integer[] getTour(Map<Integer, OpNode> tour); static TourResult getTour(Map<Integer, OpNode> tour, int startIndex); static double getDistance(Integer[] tour, IDistanceCalculator distanceCalculator); static void revert(Map<Integer, OpNode> tour, int startRevert, int endRevert); static void insert(Map<Integer, OpNode> structure, int from, int to); static Integer[] revertAndCopy(Integer[] tour, int startRevert, int endrevert); static int[] getNeighbourhoodIndexesAfterDistance(Integer[] tour, int neighbourhoodFrom, int neighbourhoodSize,
			IDistanceCalculator distanceCalculator); static int[] getNeighbourhoodIndexesFromIndexInTour(Integer[] tour, int neighbourhoodFromIndex, int neighbourhoodSize,
			IDistanceCalculator distanceCalculator); static int[] getNeighbourhoodIndexesFromValue(Integer[] tour, Integer neighbourhoodFromValue, int neighbourhoodSize,
			IDistanceCalculator distanceCalculator); static int[] getNeighbourhoodIndexesAfterDistanceOnTour(Integer[] tour, int neighbourhoodFrom, int neighbourhoodSize,
			IDistanceCalculator distanceCalculator); static final int[] getIntersection(int[] setA, int[] setB);  }