public List <List <int> > Execute(bool withSwap = false) { ResetDataset(); ResetCapacityAndTime(); var initialResult = new List <List <int> >(); for (int z = 0; z < vehicleCount && !dataset.All(item => item.IsDone); z++) { initialResult.Add(greedyAlgorithm.Run(vehicleCapacity, currentVehicleCapasity, z)); } var bestSolution = GRASPHelper.CopySolution(initialResult); var currentRoute = GRASPHelper.CopySolution(initialResult); int iterator = 0; while (iterator < Parameters.MAX_GRASP_ITERATION) { if (iterator != 0) { ResetDataset(); ResetCapacityAndTime(); currentRoute.Clear(); for (int z = 0; z < vehicleCount && !dataset.All(item => item.IsDone); z++) { currentRoute.Add(greedyAlgorithm.Run(vehicleCapacity, currentVehicleCapasity, z)); } } var tmp_route = localSearchTwoOpt.Run(dataset, currentRoute, evaluator, distanceCalculator, penaltyCalculator); if (withSwap) { tmp_route = GRASPHelper.CopySolution(localSearchSwap.Run(dataset, currentRoute, evaluator, distanceCalculator, penaltyCalculator)); } double oldQuality = evaluator.EvaluateGlobalRoute(dataset, bestSolution, distanceCalculator, penaltyCalculator); double currentQuality = evaluator.EvaluateGlobalRoute(dataset, tmp_route, distanceCalculator, penaltyCalculator); if (currentQuality < oldQuality) { bestSolution = GRASPHelper.CopySolution(tmp_route); currentRoute = GRASPHelper.CopySolution(tmp_route); } iterator++; } return(bestSolution); }
public List <List <int> > Run(List <Instance> dataset, List <List <int> > currentRoute, IEvaluator evaluator, IDistanceCalculator distanceCalculator, IPenaltyCalculator penaltyCalculator) { int k = 0; while (k < Parameters.MAX_LOCAL_SEARCH_TWO_OPT_ITERATION) { var oldRouteBeforeLocalSearch = GRASPHelper.CopySolution(currentRoute); for (int g = 0; g < currentRoute.Count; g++) { for (int i = 1; i < currentRoute[g].Count - 2; i++) { for (int j = i + 1; j < currentRoute[g].Count; j++) { double oldRouteQuality = evaluator.EvaluateRoute(dataset, currentRoute[g], distanceCalculator, penaltyCalculator); var newRoute = twoOptSwap(currentRoute[g], i, j); double newRouteQuality = evaluator.EvaluateRoute(dataset, newRoute, distanceCalculator, penaltyCalculator); if (newRouteQuality < oldRouteQuality && evaluator.CanExistWithCurrentTimeWindows(dataset, newRoute, distanceCalculator)) { currentRoute[g] = newRoute; } } } } if (evaluator.isRoutesSame(currentRoute, oldRouteBeforeLocalSearch)) { break; } k++; } return(currentRoute); }
public List <List <int> > Run(List <Instance> dataset, List <List <int> > currentRoute, IEvaluator evaluator, IDistanceCalculator distanceCalculator, IPenaltyCalculator penaltyCalculator) { int k = 0; while (k < Parameters.MAX_LOCAL_SEARCH_SWAP_ITERATION) { var oldRouteBeforeLocalSearchCopy = GRASPHelper.CopySolution(currentRoute); double oldQuality = evaluator.EvaluateGlobalRoute(dataset, oldRouteBeforeLocalSearchCopy, distanceCalculator, penaltyCalculator); bool WasIpmroved = false; for (int g = 0; g < currentRoute.Count && !WasIpmroved; g++) { if (currentRoute[g].Count <= 2) { continue; } for (int z = 0; z < currentRoute.Count && !WasIpmroved; z++) { if (g == z) { continue; } //current route for (int i = 1; i < currentRoute[g].Count && !WasIpmroved; i++) { int pointFromDiffRoute = currentRoute[g][i]; //next route for (int j = 1; j < currentRoute[z].Count && !WasIpmroved; j++) { var tmp_copy = GRASPHelper.CopySolution(oldRouteBeforeLocalSearchCopy); tmp_copy[z].Insert(j, pointFromDiffRoute); if (!evaluator.CanExistWithCurrentTimeWindows(dataset, tmp_copy[z], distanceCalculator)) { continue; } tmp_copy[g].RemoveAt(i); double localQuality = evaluator.EvaluateGlobalRoute(dataset, tmp_copy, distanceCalculator, penaltyCalculator); if (localQuality < oldQuality) { currentRoute = tmp_copy; WasIpmroved = true; } } } } } if (!WasIpmroved) { break; } k++; } return(currentRoute); }