Changeset 6907
- Timestamp:
- 10/12/11 13:41:14 (13 years ago)
- Location:
- branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Crossovers/PotvinRouteBasedCrossover.cs
r6857 r6907 50 50 PotvinEncoding child = parent2.Clone() as PotvinEncoding; 51 51 52 int tourParent1 = random.Next(parent1.Tours.Count); 53 Tour replacing = parent1.Tours[tourParent1].Clone() as Tour; 52 if (parent1.Tours.Count > 0 && child.Tours.Count > 0) { 53 int tourParent1 = random.Next(parent1.Tours.Count); 54 Tour replacing = parent1.Tours[tourParent1].Clone() as Tour; 54 55 55 int tourParent2 = random.Next(child.Tours.Count);56 Tour replaced = child.Tours[tourParent2];56 int tourParent2 = random.Next(child.Tours.Count); 57 Tour replaced = child.Tours[tourParent2]; 57 58 58 child.Tours.Remove(replaced);59 child.Tours.Insert(tourParent2, replacing);59 child.Tours.Remove(replaced); 60 child.Tours.Insert(tourParent2, replacing); 60 61 61 62 Permutation vehicleAssignment = child.VehicleAssignment; … … 70 71 break; 71 72 } 72 } 73 } 73 74 74 foreach (int city in replaced.Stops)75 if (FindRoute(child, city) == null && !child.Unrouted.Contains(city))76 child.Unrouted.Add(city);75 foreach (int city in replaced.Stops) 76 if (FindRoute(child, city) == null && !child.Unrouted.Contains(city)) 77 child.Unrouted.Add(city); 77 78 78 if (Repair(random, child, replacing, ProblemInstance, allowInfeasible) || allowInfeasible) 79 if (Repair(random, child, replacing, ProblemInstance, allowInfeasible) || allowInfeasible) 80 return child; 81 else { 82 if (random.NextDouble() < 0.5) 83 return parent1.Clone() as PotvinEncoding; 84 else 85 return parent2.Clone() as PotvinEncoding; 86 } 87 } else { 79 88 return child; 80 else {81 if(random.NextDouble() < 0.5)82 return parent1.Clone() as PotvinEncoding;83 else84 return parent2.Clone() as PotvinEncoding;85 89 } 86 90 } -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Crossovers/PotvinSequenceBasedCrossover.cs
r6851 r6907 53 53 int cities = ProblemInstance.Cities.Value; 54 54 55 int breakPoint1 = random.Next(1, cities + 1); 56 Tour tour1 = FindRoute(child, breakPoint1); 57 breakPoint1 = tour1.Stops.IndexOf(breakPoint1); 55 if (cities > 0) { 56 int breakPoint1 = random.Next(1, cities + 1); 57 Tour tour1 = FindRoute(child, breakPoint1); 58 breakPoint1 = tour1.Stops.IndexOf(breakPoint1); 58 59 59 for (int i = 0; i < breakPoint1; i++)60 newTour.Stops.Add(tour1.Stops[i]);60 for (int i = 0; i < breakPoint1; i++) 61 newTour.Stops.Add(tour1.Stops[i]); 61 62 62 int breakPoint2 = random.Next(1, cities + 1);63 Tour tour2 = FindRoute(parent2, breakPoint2);64 breakPoint2 = tour2.Stops.IndexOf(breakPoint2);63 int breakPoint2 = random.Next(1, cities + 1); 64 Tour tour2 = FindRoute(parent2, breakPoint2); 65 breakPoint2 = tour2.Stops.IndexOf(breakPoint2); 65 66 66 for (int i = breakPoint2; i < tour2.Stops.Count; i++)67 newTour.Stops.Add(tour2.Stops[i]);67 for (int i = breakPoint2; i < tour2.Stops.Count; i++) 68 newTour.Stops.Add(tour2.Stops[i]); 68 69 69 int tour1Index = child.Tours.IndexOf(tour1);70 child.Tours.Remove(tour1);71 child.Tours.Insert(tour1Index, newTour);70 int tour1Index = child.Tours.IndexOf(tour1); 71 child.Tours.Remove(tour1); 72 child.Tours.Insert(tour1Index, newTour); 72 73 73 foreach (int city in tour1.Stops)74 if (FindRoute(child, city) == null && !child.Unrouted.Contains(city))75 child.Unrouted.Add(city);74 foreach (int city in tour1.Stops) 75 if (FindRoute(child, city) == null && !child.Unrouted.Contains(city)) 76 child.Unrouted.Add(city); 76 77 77 foreach (int city in tour2.Stops)78 if (FindRoute(child, city) == null && !child.Unrouted.Contains(city))79 child.Unrouted.Add(city);78 foreach (int city in tour2.Stops) 79 if (FindRoute(child, city) == null && !child.Unrouted.Contains(city)) 80 child.Unrouted.Add(city); 80 81 81 if (Repair(random, child, newTour, ProblemInstance, allowInfeasible) || allowInfeasible) { 82 if (Repair(random, child, newTour, ProblemInstance, allowInfeasible) || allowInfeasible) { 83 return child; 84 } else { 85 if (random.NextDouble() < 0.5) 86 return parent1.Clone() as PotvinEncoding; 87 else 88 return parent2.Clone() as PotvinEncoding; 89 } 90 } else { 82 91 return child; 83 } else { 84 if (random.NextDouble() < 0.5) 85 return parent1.Clone() as PotvinEncoding; 86 else 87 return parent2.Clone() as PotvinEncoding; 88 } 92 } 89 93 } 90 94 } -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Manipulators/PotvinCustomerRelocationManipulator.cs
r6859 r6907 53 53 Tour route1; 54 54 55 int mode = random.Next(0,3); 56 57 //Try pickup and delivery first 58 IPickupAndDeliveryProblemInstance pdp = ProblemInstance as IPickupAndDeliveryProblemInstance; 59 if (pdp != null && mode == 0) { 60 selectedIndex = SelectRandomTourBiasedByLength(random, individual); 61 route1 = 62 individual.Tours[selectedIndex]; 63 64 List<int> deliveryViolations = new List<int>(); 65 66 Dictionary<int, bool> visited = new Dictionary<int, bool>(); 67 for (int i = 0; i < route1.Stops.Count; i++) { 68 int stop = route1.Stops[i]; 69 if (ProblemInstance.GetDemand(stop) < 0) { 70 int source = pdp.GetPickupDeliveryLocation(stop); 71 72 if (!visited.ContainsKey(source)) 73 deliveryViolations.Add(stop); 74 } 75 76 visited.Add(stop, true); 77 } 78 79 if (deliveryViolations.Count > 0) { 80 int selected = 81 deliveryViolations[random.Next(deliveryViolations.Count)]; 82 83 int source = pdp.GetPickupDeliveryLocation(selected); 84 85 //find route of source 86 Tour found = null; 87 foreach (Tour tour in individual.Tours) { 88 if (tour.Stops.Contains(source)) { 89 found = tour; 90 break; 91 } 92 } 93 94 if (found != null) { 95 double rand = random.NextDouble(); 96 if (rand < 0.33) { 97 route1.Stops.Remove(selected); 98 99 int index = individual.FindBestInsertionPlace(found, selected); 100 found.Stops.Insert(index, selected); 101 performed = true; 102 103 if (route1.Stops.Count == 0) 104 individual.Tours.Remove(route1); 105 } else if (rand < 0.66) { 106 found.Stops.Remove(source); 107 108 int index = individual.FindBestInsertionPlace(route1, source); 109 route1.Stops.Insert(index, source); 110 performed = true; 111 112 if (found.Stops.Count == 0) 113 individual.Tours.Remove(found); 114 } else { 115 found.Stops.Remove(source); 116 route1.Stops.Remove(selected); 117 118 int chosenRoute = random.Next(individual.Tours.Count); 119 Tour tour = individual.Tours[chosenRoute]; 120 121 int index = individual.FindBestInsertionPlace(tour, source); 122 tour.Stops.Insert(index, source); 123 index = individual.FindBestInsertionPlace(tour, selected); 124 tour.Stops.Insert(index, selected); 125 126 if (found.Stops.Count == 0) 127 individual.Tours.Remove(found); 128 if (route1.Stops.Count == 0) 129 individual.Tours.Remove(route1); 130 } 131 } 132 } 133 } 134 135 //then try tw 136 if (!performed && mode == 1) { 137 ITimeWindowedProblemInstance vrptw = ProblemInstance as ITimeWindowedProblemInstance; 138 139 if (vrptw != null) { 55 if (individual.Tours.Count > 0) { 56 int mode = random.Next(0, 3); 57 58 //Try pickup and delivery first 59 IPickupAndDeliveryProblemInstance pdp = ProblemInstance as IPickupAndDeliveryProblemInstance; 60 if (pdp != null && mode == 0) { 140 61 selectedIndex = SelectRandomTourBiasedByLength(random, individual); 141 62 route1 = 142 63 individual.Tours[selectedIndex]; 143 64 144 DoubleArray dueTime = vrptw.DueTime; 145 DoubleArray readyTime = vrptw.ReadyTime; 146 DoubleArray serviceTimes = vrptw.ServiceTime; 147 148 int depot = 0; 149 int depots = 1; 150 151 int tourIndex = individual.GetTourIndex(route1); 152 int vehicle = individual.GetVehicleAssignment(tourIndex); 153 154 if (ProblemInstance is IMultiDepotProblemInstance) { 155 depots = (ProblemInstance as IMultiDepotProblemInstance).Depots.Value; 156 depot = (ProblemInstance as IMultiDepotProblemInstance).VehicleDepotAssignment[vehicle]; 157 } 158 159 List<int> timeWindowViolations = new List<int>(); 160 double time = 0; 161 65 List<int> deliveryViolations = new List<int>(); 66 67 Dictionary<int, bool> visited = new Dictionary<int, bool>(); 162 68 for (int i = 0; i < route1.Stops.Count; i++) { 163 int start = 0; 164 if (i > 0) 165 start = route1.Stops[i - 1]; 166 int end = route1.Stops[i]; 167 168 //drive there 169 double currentDistace = vrptw.GetDistance(start, end, individual); 170 time += currentDistace; 171 172 int endIndex = end + depots - 1; 173 174 //check if it was serviced on time 175 if (time > dueTime[endIndex]) { 176 timeWindowViolations.Add(end); 177 } 178 179 //wait 180 double currentWaitingTime = 0.0; 181 if (time < readyTime[endIndex]) 182 currentWaitingTime = readyTime[endIndex] - time; 183 time += currentWaitingTime; 184 185 if (end > 0) { 186 //service 187 double currentServiceTime = serviceTimes[end - 1]; 188 time += currentServiceTime; 189 } 190 } 191 192 if (timeWindowViolations.Count > 0) { 69 int stop = route1.Stops[i]; 70 if (ProblemInstance.GetDemand(stop) < 0) { 71 int source = pdp.GetPickupDeliveryLocation(stop); 72 73 if (!visited.ContainsKey(source)) 74 deliveryViolations.Add(stop); 75 } 76 77 visited.Add(stop, true); 78 } 79 80 if (deliveryViolations.Count > 0) { 193 81 int selected = 194 timeWindowViolations[random.Next(timeWindowViolations.Count)]; 195 196 int oldIndex = route1.Stops.IndexOf(selected); 197 route1.Stops.Remove(selected); 198 199 int route, place; 200 if (FindInsertionPlace(individual, selected, selectedIndex, allowInfeasible, out route, out place)) { 201 Tour newTour = individual.Tours[route]; 202 newTour.Stops.Insert(place, selected); 203 performed = true; 204 } else { 205 route1.Stops.Insert(oldIndex, selected); 206 } 207 208 if (route1.Stops.Count == 0) 209 individual.Tours.Remove(route1); 82 deliveryViolations[random.Next(deliveryViolations.Count)]; 83 84 int source = pdp.GetPickupDeliveryLocation(selected); 85 86 //find route of source 87 Tour found = null; 88 foreach (Tour tour in individual.Tours) { 89 if (tour.Stops.Contains(source)) { 90 found = tour; 91 break; 92 } 93 } 94 95 if (found != null) { 96 double rand = random.NextDouble(); 97 if (rand < 0.33) { 98 route1.Stops.Remove(selected); 99 100 int index = individual.FindBestInsertionPlace(found, selected); 101 found.Stops.Insert(index, selected); 102 performed = true; 103 104 if (route1.Stops.Count == 0) 105 individual.Tours.Remove(route1); 106 } else if (rand < 0.66) { 107 found.Stops.Remove(source); 108 109 int index = individual.FindBestInsertionPlace(route1, source); 110 route1.Stops.Insert(index, source); 111 performed = true; 112 113 if (found.Stops.Count == 0) 114 individual.Tours.Remove(found); 115 } else { 116 found.Stops.Remove(source); 117 route1.Stops.Remove(selected); 118 119 int chosenRoute = random.Next(individual.Tours.Count); 120 Tour tour = individual.Tours[chosenRoute]; 121 122 int index = individual.FindBestInsertionPlace(tour, source); 123 tour.Stops.Insert(index, source); 124 index = individual.FindBestInsertionPlace(tour, selected); 125 tour.Stops.Insert(index, selected); 126 127 if (found.Stops.Count == 0) 128 individual.Tours.Remove(found); 129 if (route1.Stops.Count == 0) 130 individual.Tours.Remove(route1); 131 } 132 } 210 133 } 211 134 } 212 } 213 214 //finally relocate random customer 215 if (!performed) { 216 selectedIndex = SelectRandomTourBiasedByLength(random, individual); 217 route1 = 218 individual.Tours[selectedIndex]; 219 220 int selected = route1.Stops[random.Next(route1.Stops.Count)]; 221 int oldIndex = route1.Stops.IndexOf(selected); 222 route1.Stops.Remove(selected); 223 224 int route, place; 225 if (FindInsertionPlace(individual, selected, selectedIndex, allowInfeasible, out route, out place)) { 226 Tour tour = individual.Tours[route]; 227 tour.Stops.Insert(place, selected); 228 performed = true; 229 } else { 230 route1.Stops.Insert(oldIndex, selected); 135 136 //then try tw 137 if (!performed && mode == 1) { 138 ITimeWindowedProblemInstance vrptw = ProblemInstance as ITimeWindowedProblemInstance; 139 140 if (vrptw != null) { 141 selectedIndex = SelectRandomTourBiasedByLength(random, individual); 142 route1 = 143 individual.Tours[selectedIndex]; 144 145 DoubleArray dueTime = vrptw.DueTime; 146 DoubleArray readyTime = vrptw.ReadyTime; 147 DoubleArray serviceTimes = vrptw.ServiceTime; 148 149 int depot = 0; 150 int depots = 1; 151 152 int tourIndex = individual.GetTourIndex(route1); 153 int vehicle = individual.GetVehicleAssignment(tourIndex); 154 155 if (ProblemInstance is IMultiDepotProblemInstance) { 156 depots = (ProblemInstance as IMultiDepotProblemInstance).Depots.Value; 157 depot = (ProblemInstance as IMultiDepotProblemInstance).VehicleDepotAssignment[vehicle]; 158 } 159 160 List<int> timeWindowViolations = new List<int>(); 161 double time = 0; 162 163 for (int i = 0; i < route1.Stops.Count; i++) { 164 int start = 0; 165 if (i > 0) 166 start = route1.Stops[i - 1]; 167 int end = route1.Stops[i]; 168 169 //drive there 170 double currentDistace = vrptw.GetDistance(start, end, individual); 171 time += currentDistace; 172 173 int endIndex = end + depots - 1; 174 175 //check if it was serviced on time 176 if (time > dueTime[endIndex]) { 177 timeWindowViolations.Add(end); 178 } 179 180 //wait 181 double currentWaitingTime = 0.0; 182 if (time < readyTime[endIndex]) 183 currentWaitingTime = readyTime[endIndex] - time; 184 time += currentWaitingTime; 185 186 if (end > 0) { 187 //service 188 double currentServiceTime = serviceTimes[end - 1]; 189 time += currentServiceTime; 190 } 191 } 192 193 if (timeWindowViolations.Count > 0) { 194 int selected = 195 timeWindowViolations[random.Next(timeWindowViolations.Count)]; 196 197 int oldIndex = route1.Stops.IndexOf(selected); 198 route1.Stops.Remove(selected); 199 200 int route, place; 201 if (FindInsertionPlace(individual, selected, selectedIndex, allowInfeasible, out route, out place)) { 202 Tour newTour = individual.Tours[route]; 203 newTour.Stops.Insert(place, selected); 204 performed = true; 205 } else { 206 route1.Stops.Insert(oldIndex, selected); 207 } 208 209 if (route1.Stops.Count == 0) 210 individual.Tours.Remove(route1); 211 } 212 } 231 213 } 232 214 233 if (route1.Stops.Count == 0) 234 individual.Tours.Remove(route1); 215 //finally relocate random customer 216 if (!performed) { 217 selectedIndex = SelectRandomTourBiasedByLength(random, individual); 218 route1 = 219 individual.Tours[selectedIndex]; 220 221 int selected = route1.Stops[random.Next(route1.Stops.Count)]; 222 int oldIndex = route1.Stops.IndexOf(selected); 223 route1.Stops.Remove(selected); 224 225 int route, place; 226 if (FindInsertionPlace(individual, selected, selectedIndex, allowInfeasible, out route, out place)) { 227 Tour tour = individual.Tours[route]; 228 tour.Stops.Insert(place, selected); 229 performed = true; 230 } else { 231 route1.Stops.Insert(oldIndex, selected); 232 } 233 234 if (route1.Stops.Count == 0) 235 individual.Tours.Remove(route1); 236 } 235 237 } 236 238 } -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Manipulators/PotvinOneLevelExchangeManipulator.cs
r6796 r6907 49 49 50 50 int selectedIndex = SelectRandomTourBiasedByLength(random, individual); 51 Tour route1 = 52 individual.Tours[selectedIndex]; 51 if (selectedIndex >= 0) { 52 Tour route1 = 53 individual.Tours[selectedIndex]; 53 54 54 int count = route1.Stops.Count;55 int i = 0;56 while(i < count) {57 int insertedRoute, insertedPlace;55 int count = route1.Stops.Count; 56 int i = 0; 57 while (i < count) { 58 int insertedRoute, insertedPlace; 58 59 59 int city = route1.Stops[i];60 route1.Stops.Remove(city);60 int city = route1.Stops[i]; 61 route1.Stops.Remove(city); 61 62 62 if (FindInsertionPlace(individual, city, selectedIndex, allowInfeasible, out insertedRoute, out insertedPlace)) { 63 individual.Tours[insertedRoute].Stops.Insert(insertedPlace, city); 64 } else { 65 route1.Stops.Insert(i, city); 66 i++; 63 if (FindInsertionPlace(individual, city, selectedIndex, allowInfeasible, out insertedRoute, out insertedPlace)) { 64 individual.Tours[insertedRoute].Stops.Insert(insertedPlace, city); 65 } else { 66 route1.Stops.Insert(i, city); 67 i++; 68 } 69 70 count = route1.Stops.Count; 67 71 } 68 69 count = route1.Stops.Count;70 72 } 71 72 73 } 73 74 } -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Manipulators/PotvinTwoLevelExchangeManipulator.cs
r6838 r6907 49 49 50 50 int selectedIndex = SelectRandomTourBiasedByLength(random, individual); 51 Tour route1 = individual.Tours[selectedIndex]; 51 if (selectedIndex >= 0) { 52 Tour route1 = individual.Tours[selectedIndex]; 52 53 53 bool performed = false;54 int customer1Position = 0;55 while (customer1Position < route1.Stops.Count) {56 performed = false;54 bool performed = false; 55 int customer1Position = 0; 56 while (customer1Position < route1.Stops.Count) { 57 performed = false; 57 58 58 foreach (Tour tour in individual.Tours) {59 if (tour != route1) {60 for (int customer2Position = 0; customer2Position < tour.Stops.Count; customer2Position++) {61 int customer1 = route1.Stops[customer1Position];62 int customer2 = tour.Stops[customer2Position];59 foreach (Tour tour in individual.Tours) { 60 if (tour != route1) { 61 for (int customer2Position = 0; customer2Position < tour.Stops.Count; customer2Position++) { 62 int customer1 = route1.Stops[customer1Position]; 63 int customer2 = tour.Stops[customer2Position]; 63 64 64 //customer1 can be feasibly inserted at the location of customer265 tour.Stops[customer2Position] = customer1;66 route1.Stops.RemoveAt(customer1Position);65 //customer1 can be feasibly inserted at the location of customer2 66 tour.Stops[customer2Position] = customer1; 67 route1.Stops.RemoveAt(customer1Position); 67 68 68 if (ProblemInstance.TourFeasible(tour, individual)) {69 int routeIdx, place;70 if (FindInsertionPlace(individual,71 customer2, selectedIndex, allowInfeasible, out routeIdx, out place)) {72 individual.Tours[routeIdx].Stops.Insert(place, customer2);69 if (ProblemInstance.TourFeasible(tour, individual)) { 70 int routeIdx, place; 71 if (FindInsertionPlace(individual, 72 customer2, selectedIndex, allowInfeasible, out routeIdx, out place)) { 73 individual.Tours[routeIdx].Stops.Insert(place, customer2); 73 74 74 //two-level exchange has been performed 75 performed = true; 76 break; 75 //two-level exchange has been performed 76 performed = true; 77 break; 78 } else { 79 tour.Stops[customer2Position] = customer2; 80 route1.Stops.Insert(customer1Position, customer1); 81 } 77 82 } else { 78 83 tour.Stops[customer2Position] = customer2; 79 84 route1.Stops.Insert(customer1Position, customer1); 80 85 } 81 } else {82 tour.Stops[customer2Position] = customer2;83 route1.Stops.Insert(customer1Position, customer1);84 86 } 85 87 } 88 89 if (performed) 90 break; 86 91 } 87 92 88 if ( performed)89 break;93 if (!performed) 94 customer1Position++; 90 95 } 91 92 if (!performed)93 customer1Position++;94 96 } 95 97 } -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/PotvinEncoding.cs
r6857 r6907 61 61 this.Unrouted = new List<int>(original.Unrouted); 62 62 this.VehicleAssignment = cloner.Clone<Permutation>(original.VehicleAssignment); 63 } 64 65 public override void Repair() { 66 List<Tour> toBeRemoved = new List<Tour>(); 67 foreach (Tour tour in Tours) { 68 if (tour.Stops.Count == 0) 69 toBeRemoved.Add(tour); 70 } 71 72 foreach (Tour tour in toBeRemoved) { 73 int index = Tours.IndexOf(tour); 74 if (index < VehicleAssignment.Length) { 75 int vehicle = VehicleAssignment[index]; 76 77 Tours.Remove(tour); 78 for (int i = index; i < VehicleAssignment.Length - 1; i++) { 79 VehicleAssignment[i] = VehicleAssignment[i + 1]; 80 } 81 VehicleAssignment[VehicleAssignment.Length - 1] = vehicle; 82 } 83 } 84 85 while (Tours.Count > ProblemInstance.Vehicles.Value) { 86 Tour tour = Tours[Tours.Count - 1]; 87 Tours[Tours.Count - 2].Stops.AddRange(tour.Stops); 88 89 Tours.Remove(tour); 90 } 63 91 } 64 92 -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/VehicleRoutingProblem.cs
r6854 r6907 207 207 OnOperatorsChanged(); 208 208 } 209 210 public void SetProblemInstance(IVRPProblemInstance instance) { 211 ProblemInstanceParameter.ValueChanged -= new EventHandler(ProblemInstanceParameter_ValueChanged); 212 213 ProblemInstance = instance; 214 AttachProblemInstanceEventHandlers(); 215 216 OnSolutionCreatorChanged(); 217 OnEvaluatorChanged(); 218 219 ProblemInstanceParameter.ValueChanged += new EventHandler(ProblemInstanceParameter_ValueChanged); 220 } 221 209 222 private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) { 210 223 ParameterizeSolutionCreator();
Note: See TracChangeset
for help on using the changeset viewer.