Changeset 6449 for trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Potvin/Crossovers
- Timestamp:
- 06/20/11 13:36:49 (13 years ago)
- Location:
- trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Potvin/Crossovers
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Potvin/Crossovers/PotvinCrossover.cs
r5445 r6449 26 26 using HeuristicLab.Parameters; 27 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 using HeuristicLab.Data; 28 29 29 30 namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin { … … 47 48 protected abstract PotvinEncoding Crossover(IRandom random, PotvinEncoding parent1, PotvinEncoding parent2); 48 49 49 protected bool FindInsertionPlace(PotvinEncoding individual, int city, out int route, out int place) { 50 protected static bool FindInsertionPlace(PotvinEncoding individual, int city, 51 DoubleArray dueTime, DoubleArray serviceTime, DoubleArray readyTime, DoubleArray demand, 52 DoubleValue capacity, DistanceMatrix distMatrix, 53 out int route, out int place) { 50 54 return individual.FindInsertionPlace( 51 DueTimeParameter.ActualValue, ServiceTimeParameter.ActualValue, ReadyTimeParameter.ActualValue, 52 DemandParameter.ActualValue, CapacityParameter.ActualValue, CoordinatesParameter.ActualValue, 53 DistanceMatrixParameter, UseDistanceMatrixParameter.ActualValue, 55 dueTime, serviceTime, readyTime, 56 demand, capacity, distMatrix, 54 57 city, -1, out route, out place); 55 58 } … … 68 71 } 69 72 70 protected bool Repair(IRandom random, PotvinEncoding solution, Tour newTour) { 73 protected static bool RouteUnrouted(PotvinEncoding solution, DistanceMatrix distMatrix, 74 DoubleArray dueTime, DoubleArray readyTime, DoubleArray serviceTime, DoubleArray demand, DoubleValue capacity) { 75 bool success = true; 76 int index = 0; 77 while (index < solution.Unrouted.Count && success) { 78 int unrouted = solution.Unrouted[index]; 79 80 int route, place; 81 if (FindInsertionPlace(solution, unrouted, 82 dueTime, serviceTime, readyTime, demand, capacity, 83 distMatrix, 84 out route, out place)) { 85 solution.Tours[route].Cities.Insert(place, unrouted); 86 } else { 87 success = false; 88 } 89 90 index++; 91 } 92 93 for (int i = 0; i < index; i++) 94 solution.Unrouted.RemoveAt(0); 95 96 return success; 97 } 98 99 protected static bool Repair(IRandom random, PotvinEncoding solution, Tour newTour, DistanceMatrix distmatrix, 100 DoubleArray dueTime, DoubleArray readyTime, DoubleArray serviceTime, DoubleArray demand, DoubleValue capacity) { 71 101 bool success = true; 72 102 … … 84 114 while (newTour.Cities.Contains(0)) 85 115 newTour.Cities.Remove(0); 116 117 if (!newTour.Feasible( 118 dueTime, serviceTime, readyTime, demand, capacity, distmatrix)) 119 return false; 86 120 87 121 //remove duplicates from old tours … … 105 139 106 140 //route unrouted vehicles 107 int index = 0; 108 while (index < solution.Unrouted.Count && success) { 109 int unrouted = solution.Unrouted[index]; 110 111 int route, place; 112 if (FindInsertionPlace(solution, unrouted, out route, out place)) { 113 solution.Tours[route].Cities.Insert(place, unrouted); 114 } else { 115 success = false; 116 } 117 118 index++; 119 } 120 121 for (int i = 0; i < index; i++) 122 solution.Unrouted.RemoveAt(0); 141 success = RouteUnrouted(solution, distmatrix, dueTime, readyTime, serviceTime, demand, capacity); 123 142 124 143 return success; -
trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Potvin/Crossovers/PotvinRouteBasedCrossover.cs
r5445 r6449 23 23 using HeuristicLab.Core; 24 24 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 25 using HeuristicLab.Data; 25 26 26 27 namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin { … … 40 41 41 42 protected override PotvinEncoding Crossover(IRandom random, PotvinEncoding parent1, PotvinEncoding parent2) { 43 BoolValue useDistanceMatrix = UseDistanceMatrixParameter.ActualValue; 44 DoubleMatrix coordinates = CoordinatesParameter.ActualValue; 45 DistanceMatrix distMatrix = VRPUtilities.GetDistanceMatrix(coordinates, DistanceMatrixParameter, useDistanceMatrix); 46 DoubleArray dueTime = DueTimeParameter.ActualValue; 47 DoubleArray readyTime = ReadyTimeParameter.ActualValue; 48 DoubleArray serviceTime = ServiceTimeParameter.ActualValue; 49 DoubleArray demand = DemandParameter.ActualValue; 50 DoubleValue capacity = CapacityParameter.ActualValue; 51 42 52 PotvinEncoding child = parent2.Clone() as PotvinEncoding; 43 53 … … 55 65 child.Unrouted.Add(city); 56 66 57 if (Repair(random, child, replacing ))67 if (Repair(random, child, replacing, distMatrix, dueTime, readyTime, serviceTime, demand, capacity)) 58 68 return child; 59 69 else { … … 61 71 return parent1.Clone() as PotvinEncoding; 62 72 else 63 return parent2.Clone() as PotvinEncoding; 73 return parent2.Clone() as PotvinEncoding; 64 74 } 65 75 } -
trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Potvin/Crossovers/PotvinSequenceBasedCrossover.cs
r5445 r6449 23 23 using HeuristicLab.Core; 24 24 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 25 using HeuristicLab.Data; 25 26 26 27 namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin { … … 41 42 42 43 protected override PotvinEncoding Crossover(IRandom random, PotvinEncoding parent1, PotvinEncoding parent2) { 44 BoolValue useDistanceMatrix = UseDistanceMatrixParameter.ActualValue; 45 DoubleMatrix coordinates = CoordinatesParameter.ActualValue; 46 DistanceMatrix distMatrix = VRPUtilities.GetDistanceMatrix(coordinates, DistanceMatrixParameter, useDistanceMatrix); 47 DoubleArray dueTime = DueTimeParameter.ActualValue; 48 DoubleArray readyTime = ReadyTimeParameter.ActualValue; 49 DoubleArray serviceTime = ServiceTimeParameter.ActualValue; 50 DoubleArray demand = DemandParameter.ActualValue; 51 DoubleValue capacity = CapacityParameter.ActualValue; 52 43 53 PotvinEncoding child = parent1.Clone() as PotvinEncoding; 44 54 Tour newTour = new Tour(); … … 69 79 child.Unrouted.Add(city); 70 80 71 if (Feasible(newTour) && 72 Repair(random, child, newTour)) { 81 if (Repair(random, child, newTour, distMatrix, dueTime, readyTime, serviceTime, demand, capacity)) { 73 82 return child; 74 83 } else { 75 if (random.NextDouble() < 0.5)84 if (random.NextDouble() < 0.5) 76 85 return parent1.Clone() as PotvinEncoding; 77 86 else 78 return parent2.Clone() as PotvinEncoding; 87 return parent2.Clone() as PotvinEncoding; 79 88 } 80 89 }
Note: See TracChangeset
for help on using the changeset viewer.