- Timestamp:
- 09/29/11 15:51:56 (13 years ago)
- Location:
- branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin
- Files:
-
- 12 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) -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Manipulators/PotvinCustomerRelocationManipulator.cs
r6753 r6851 67 67 for (int i = 0; i < route1.Stops.Count; i++) { 68 68 int stop = route1.Stops[i]; 69 if (ProblemInstance. Demand[stop]< 0) {69 if (ProblemInstance.GetDemand(stop) < 0) { 70 70 int source = pdp.PickupDeliveryLocation[stop]; 71 71 … … 158 158 159 159 //drive there 160 double currentDistace = vrptw.GetDistance(start, end );160 double currentDistace = vrptw.GetDistance(start, end, individual); 161 161 time += currentDistace; 162 162 -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Moves/PickupDelivery/PDExchange/PotvinPDExchangeExhaustiveMoveGenerator.cs
r6773 r6851 62 62 if (k != i) { 63 63 int city1 = individual.Tours[i].Stops[j]; 64 if (pdp == null || pdp. Demand[city1]>= 0) {64 if (pdp == null || pdp.GetDemand(city1) >= 0) { 65 65 for (int l = 0; l < individual.Tours[k].Stops.Count; l++) { 66 66 int city2 = individual.Tours[k].Stops[l]; 67 if (pdp == null || pdp. Demand[city2]>= 0) {67 if (pdp == null || pdp.GetDemand(city2) >= 0) { 68 68 bool valid = pdp == null || 69 69 (pdp.PickupDeliveryLocation[city2] != pdp.PickupDeliveryLocation[city1] && -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Moves/PickupDelivery/PDExchange/PotvinPDExchangeSingleMoveGenerator.cs
r6773 r6851 62 62 IPickupAndDeliveryProblemInstance pdp = problemInstance as IPickupAndDeliveryProblemInstance; 63 63 for (int i = 1; i <= individual.Cities; i++) { 64 if (pdp == null || pdp. Demand[i]>= 0)64 if (pdp == null || pdp.GetDemand(i) >= 0) 65 65 cities.Add(i); 66 66 } … … 84 84 foreach (int stop in newTour.Stops) { 85 85 if (pdp == null || 86 (pdp. Demand[stop]>= 0 &&86 (pdp.GetDemand(stop) >= 0 && 87 87 pdp.PickupDeliveryLocation[stop] != pdp.PickupDeliveryLocation[city] && 88 88 pdp.PickupDeliveryLocation[stop] != city && -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Moves/PickupDelivery/PDRearrange/PotvinPDRearrangeExhaustiveMoveGenerator.cs
r6773 r6851 54 54 55 55 for (int i = 1; i <= problemInstance.Cities.Value; i++) { 56 if (pdp == null || pdp. Demand[i]>= 0) {56 if (pdp == null || pdp.GetDemand(i) >= 0) { 57 57 int tour = individual.Tours.FindIndex(t => t.Stops.Contains(i)); 58 58 -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Moves/PickupDelivery/PDRearrange/PotvinPDRearrangeMoveMaker.cs
r6838 r6851 74 74 if (positionToAvoid != i || positionToAvoid2 != j || stops == 0) { 75 75 bool feasible; 76 double targetCosts = problemInstance.GetInsertionCosts(tourEval, target, 0, j, out feasible);76 double targetCosts = problemInstance.GetInsertionCosts(tourEval, solution, target, 0, j, out feasible); 77 77 78 78 double costs = sourceCosts + targetCosts; -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Moves/PickupDelivery/PDRearrange/PotvinPDRearrangeSingleMoveGenerator.cs
r6773 r6851 62 62 IPickupAndDeliveryProblemInstance pdp = problemInstance as IPickupAndDeliveryProblemInstance; 63 63 for (int i = 1; i <= individual.Cities; i++) { 64 if(pdp == null || pdp. Demand[i]>= 0)64 if(pdp == null || pdp.GetDemand(i) >= 0) 65 65 cities.Add(i); 66 66 } -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Moves/PickupDelivery/PDShift/PotvinPDShiftExhaustiveMoveGenerator.cs
r6773 r6851 62 62 if (k != i) { 63 63 int city = individual.Tours[i].Stops[j]; 64 if (pdp == null || pdp. Demand[city]>= 0) {64 if (pdp == null || pdp.GetDemand(city) >= 0) { 65 65 PotvinPDShiftMove move = new PotvinPDShiftMove( 66 66 city, i, k, individual); -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Moves/PickupDelivery/PDShift/PotvinPDShiftSingleMoveGenerator.cs
r6773 r6851 62 62 IPickupAndDeliveryProblemInstance pdp = problemInstance as IPickupAndDeliveryProblemInstance; 63 63 for (int i = 1; i <= individual.Cities; i++) { 64 if(pdp == null || pdp. Demand[i]>= 0)64 if(pdp == null || pdp.GetDemand(i) >= 0) 65 65 cities.Add(i); 66 66 } -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/PotvinEncoding.cs
r6838 r6851 45 45 46 46 [StorableConstructor] 47 pr ivatePotvinEncoding(bool serializing)47 protected PotvinEncoding(bool serializing) 48 48 : base(serializing) { 49 49 } … … 75 75 76 76 public double GetTourLength(Tour tour) { 77 return tour.GetTourLength(ProblemInstance );77 return tour.GetTourLength(ProblemInstance, this); 78 78 } 79 79 … … 87 87 if (positionToAvoid != i) { 88 88 bool feasible; 89 double quality = ProblemInstance.GetInsertionCosts(eval, city, 0, i, out feasible);89 double quality = ProblemInstance.GetInsertionCosts(eval, this, city, 0, i, out feasible); 90 90 if (place < 0 || quality < minQuality) { 91 91 place = i; … … 115 115 for (int i = 0; i <= Tours[tour].Stops.Count; i++) { 116 116 bool feasible; 117 double detour = ProblemInstance.GetInsertionCosts(eval, city, tour, i, out feasible);118 117 double detour = ProblemInstance.GetInsertionCosts(eval, this, city, tour, i, out feasible); 118 119 119 if (feasible || allowInfeasible) { 120 120 if (route < 0 || detour < minDetour) {
Note: See TracChangeset
for help on using the changeset viewer.