Changeset 6851 for branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Crossovers
- Timestamp:
- 09/29/11 15:51:56 (13 years ago)
- Location:
- branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Crossovers
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Crossovers/PotvinInsertionBasedCrossover.cs
r6838 r6851 28 28 using HeuristicLab.Parameters; 29 29 using HeuristicLab.Problems.VehicleRouting.ProblemInstances; 30 using HeuristicLab.Problems.VehicleRouting.Interfaces; 30 31 31 32 namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin { … … 74 75 } 75 76 76 private double CalculateCentroidDistance(Tour t1, Tour t2, DoubleMatrix coordinates) {77 private double CalculateCentroidDistance(Tour t1, Tour t2, IVRPProblemInstance instance) { 77 78 double xSum = 0; 78 79 double ySum = 0; … … 80 81 81 82 for (int i = 0; i < t1.Stops.Count; i++) { 82 xSum += coordinates[t1.Stops[i],0];83 ySum += coordinates[t1.Stops[i],1];83 xSum += instance.GetCoordinates(t1.Stops[i])[0]; 84 ySum += instance.GetCoordinates(t1.Stops[i])[1]; 84 85 } 85 86 c1X = xSum / t1.Stops.Count; … … 87 88 88 89 for (int i = 0; i < t2.Stops.Count; i++) { 89 xSum += coordinates[t2.Stops[i],0];90 ySum += coordinates[t2.Stops[i],1];90 xSum += instance.GetCoordinates(t2.Stops[i])[0]; 91 ySum += instance.GetCoordinates(t2.Stops[i])[1]; 91 92 } 92 93 c2X = xSum / t1.Stops.Count; … … 98 99 } 99 100 100 private double CalculateMeanCentroidDistance(Tour t1, IList<Tour> tours, DoubleMatrix coordinates) {101 private double CalculateMeanCentroidDistance(Tour t1, IList<Tour> tours, IVRPProblemInstance instance) { 101 102 double sum = 0; 102 103 103 104 for (int i = 0; i < tours.Count; i++) { 104 sum += CalculateCentroidDistance(t1, tours[i], coordinates);105 sum += CalculateCentroidDistance(t1, tours[i], instance); 105 106 } 106 107 … … 108 109 } 109 110 110 private int SelectCityBiasedByNeighborDistance(IRandom random, Tour tour ) {111 private int SelectCityBiasedByNeighborDistance(IRandom random, Tour tour, IVRPEncoding solution) { 111 112 int cityIndex = -1; 112 113 … … 120 121 next = tour.Stops[i + 1]; 121 122 double distance = ProblemInstance.GetDistance( 122 tour.Stops[i], next );123 tour.Stops[i], next, solution); 123 124 124 125 int prev; … … 128 129 prev = tour.Stops[i - 1]; 129 130 distance += ProblemInstance.GetDistance( 130 tour.Stops[i], prev );131 tour.Stops[i], prev, solution); 131 132 132 133 probabilities[i] = distance; … … 168 169 for (int i = 0; i <= tour.Stops.Count; i++) { 169 170 bool feasible; 170 double detour = ProblemInstance.GetInsertionCosts(eval, city, 0, i, out feasible);171 double detour = ProblemInstance.GetInsertionCosts(eval, individual, city, 0, i, out feasible); 171 172 if (feasible || allowInfeasible) { 172 173 if (place < 0 || detour < minDetour) { … … 195 196 196 197 protected override PotvinEncoding Crossover(IRandom random, PotvinEncoding parent1, PotvinEncoding parent2) { 197 PotvinEncoding child = new PotvinEncoding(ProblemInstance); 198 PotvinEncoding child = parent1.Clone() as PotvinEncoding; 199 child.Tours.Clear(); 198 200 199 201 bool allowInfeasible = AllowInfeasibleSolutions.Value.Value; … … 213 215 List<int> R2 = new List<int>(); 214 216 215 double r = CalculateMeanCentroidDistance(r1, parent2.Tours, ProblemInstance .Coordinates);217 double r = CalculateMeanCentroidDistance(r1, parent2.Tours, ProblemInstance); 216 218 foreach (Tour tour in parent2.Tours) { 217 if (CalculateCentroidDistance(r1, tour, ProblemInstance .Coordinates) <= r) {219 if (CalculateCentroidDistance(r1, tour, ProblemInstance) <= r) { 218 220 R2.AddRange(tour.Stops); 219 221 } … … 227 229 int removed = random.Next(1, r1.Stops.Count + 1); 228 230 for (int i = 0; i < removed; i++) { 229 childTour.Stops.RemoveAt(SelectCityBiasedByNeighborDistance(random, childTour ));231 childTour.Stops.RemoveAt(SelectCityBiasedByNeighborDistance(random, childTour, child)); 230 232 } 231 233 232 234 //REPAIR - add cities from R2 233 int maxCount = random.Next(1, Math.Min(5, R2.Count)); 235 int maxCount = 1; 236 if(R2.Count > 1) 237 maxCount = random.Next(1, Math.Min(5, R2.Count)); 234 238 int count = 0; 235 239 -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Crossovers/PotvinRouteBasedCrossover.cs
r6607 r6851 26 26 using HeuristicLab.Data; 27 27 using HeuristicLab.Common; 28 using HeuristicLab.Problems.VehicleRouting.Encodings.ExtendedPotvin; 28 29 29 30 namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin { … … 57 58 58 59 child.Tours.Remove(replaced); 59 child.Tours.Add(replacing); 60 child.Tours.Insert(tourParent2, replacing); 61 62 if (parent1 is ExtendedPotvinEncoding && child is ExtendedPotvinEncoding) { 63 Permutation vehicleAssignment = (child as ExtendedPotvinEncoding).VehicleAssignment; 64 65 int vehicle = vehicleAssignment[tourParent2]; 66 int vehicle2 = (parent1 as ExtendedPotvinEncoding).VehicleAssignment[tourParent1]; 67 vehicleAssignment[tourParent2] = vehicle2; 68 69 for (int i = 0; i < vehicleAssignment.Length; i++) { 70 if (vehicleAssignment[i] == vehicle2 && i != tourParent2) { 71 vehicleAssignment[i] = vehicle; 72 break; 73 } 74 } 75 } 60 76 61 77 foreach (int city in replaced.Stops) -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Crossovers/PotvinSequenceBasedCrossover.cs
r6607 r6851 67 67 newTour.Stops.Add(tour2.Stops[i]); 68 68 69 int tour1Index = child.Tours.IndexOf(tour1); 69 70 child.Tours.Remove(tour1); 70 child.Tours. Add(newTour);71 child.Tours.Insert(tour1Index, newTour); 71 72 72 73 foreach (int city in tour1.Stops)
Note: See TracChangeset
for help on using the changeset viewer.