Changeset 6449 for trunk/sources
- Timestamp:
- 06/20/11 13:36:49 (14 years ago)
- Location:
- trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3
- Files:
-
- 19 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Alba/Crossovers/AlbaPermutationCrossover.cs
r5445 r6449 48 48 49 49 protected override AlbaEncoding Crossover(IRandom random, AlbaEncoding parent1, AlbaEncoding parent2) { 50 //note - the inner crossover is called here and the resultis converted to an alba representation50 //note - the inner crossover is called here and the solution is converted to an alba representation 51 51 //some refactoring should be done here in the future - the crossover operation should be called directly 52 52 53 InnerCrossoverParameter.ActualValue.ParentsParameter.ActualName = ParentsParameter.ActualName; 54 IAtomicOperation op = this.ExecutionContext.CreateOperation( 55 InnerCrossoverParameter.ActualValue, this.ExecutionContext.Scope); 56 op.Operator.Execute((IExecutionContext)op, CancellationToken); 53 if (parent1.Length == parent2.Length) { 54 InnerCrossoverParameter.ActualValue.ParentsParameter.ActualName = ParentsParameter.ActualName; 55 IAtomicOperation op = this.ExecutionContext.CreateOperation( 56 InnerCrossoverParameter.ActualValue, this.ExecutionContext.Scope); 57 op.Operator.Execute((IExecutionContext)op, CancellationToken); 57 58 58 string childName = InnerCrossoverParameter.ActualValue.ChildParameter.ActualName;59 if (ExecutionContext.Scope.Variables.ContainsKey(childName)) {60 Permutation permutation = ExecutionContext.Scope.Variables[childName].Value as Permutation;61 ExecutionContext.Scope.Variables.Remove(childName);59 string childName = InnerCrossoverParameter.ActualValue.ChildParameter.ActualName; 60 if (ExecutionContext.Scope.Variables.ContainsKey(childName)) { 61 Permutation permutation = ExecutionContext.Scope.Variables[childName].Value as Permutation; 62 ExecutionContext.Scope.Variables.Remove(childName); 62 63 63 return new AlbaEncoding(permutation, Cities); 64 } else 65 return null; 64 return new AlbaEncoding(permutation, Cities); 65 } else 66 return null; 67 } else { 68 return parent1.Clone() as AlbaEncoding; 69 } 66 70 } 67 71 } -
trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/General/Creators/PushForwardInsertionCreator.cs
r5445 r6449 91 91 } 92 92 93 private double CalculateDistance(int start, int end ) {93 private double CalculateDistance(int start, int end, DoubleMatrix coordinates) { 94 94 double distance = 0.0; 95 DoubleMatrix coordinates = CoordinatesParameter.ActualValue;96 95 97 96 distance = … … 103 102 } 104 103 105 private DoubleMatrix CreateDistanceMatrix() { 106 DoubleMatrix coordinates = CoordinatesParameter.ActualValue; 104 private DoubleMatrix CreateDistanceMatrix(DoubleMatrix coordinates) { 107 105 DoubleMatrix distanceMatrix = new DoubleMatrix(coordinates.Rows, coordinates.Rows); 108 106 109 107 for (int i = 0; i < distanceMatrix.Rows; i++) { 110 108 for (int j = i; j < distanceMatrix.Columns; j++) { 111 double distance = CalculateDistance(i, j );109 double distance = CalculateDistance(i, j, coordinates); 112 110 113 111 distanceMatrix[i, j] = distance; … … 119 117 } 120 118 121 private double Distance(int start, int end ) {119 private double Distance(int start, int end, DoubleMatrix coordinates, bool useDistanceMatrix) { 122 120 double distance = 0.0; 123 121 124 if (UseDistanceMatrixParameter.ActualValue.Value) { 125 if (DistanceMatrixParameter.ActualValue == null) { 126 DistanceMatrixParameter.ActualValue = CreateDistanceMatrix(); 127 } 128 129 distance = DistanceMatrixParameter.ActualValue[start, end]; 130 } else { 131 distance = CalculateDistance(start, end); 122 if (useDistanceMatrix) { 123 distance = coordinates[start, end]; 124 } else { 125 distance = CalculateDistance(start, end, coordinates); 132 126 } 133 127 … … 135 129 } 136 130 137 private double TravelDistance(List<int> route, int begin ) {131 private double TravelDistance(List<int> route, int begin, DoubleMatrix coordinates, bool useDistanceMatrix) { 138 132 double distance = 0; 139 133 for (int i = begin; i < route.Count - 1 && (i == begin || route[i] != 0); i++) { 140 distance += Distance(route[i], route[i + 1] );134 distance += Distance(route[i], route[i + 1], coordinates, useDistanceMatrix); 141 135 } 142 136 return distance; 143 137 } 144 138 145 private bool SubrouteConstraintsOK(List<int> route, int begin) { 139 private bool SubrouteConstraintsOK(List<int> route, int begin, DoubleMatrix coordinates, bool useDistanceMatrix, 140 DoubleArray dueTime, DoubleArray readyTime, DoubleArray serviceTime, DoubleArray demand, DoubleValue capacity) { 146 141 double t = 0.0, o = 0.0; 147 142 for (int i = begin + 1; i < route.Count; i++) { 148 t += Distance(route[i - 1], route[i] );143 t += Distance(route[i - 1], route[i], coordinates, useDistanceMatrix); 149 144 if (route[i] == 0) return (t < DueTimeParameter.ActualValue[0]); // violation on capacity constraint is handled below 150 145 else { 151 if (t > DueTimeParameter.ActualValue[route[i]]) return false;152 t = Math.Max( ReadyTimeParameter.ActualValue[route[i]], t);153 t += ServiceTimeParameter.ActualValue[route[i]];154 o += DemandParameter.ActualValue[route[i]];155 if (o > CapacityParameter.ActualValue.Value) return false; // premature exit on capacity constraint violation146 if (t > dueTime[route[i]]) return false; 147 t = Math.Max(readyTime[route[i]], t); 148 t += serviceTime[route[i]]; 149 o += demand[route[i]]; 150 if (o > capacity.Value) return false; // premature exit on capacity constraint violation 156 151 } 157 152 } … … 159 154 } 160 155 161 private bool SubrouteTardinessOK(List<int> route, int begin) { 156 private bool SubrouteTardinessOK(List<int> route, int begin, DoubleMatrix coordinates, bool useDistanceMatrix, 157 DoubleArray dueTime, DoubleArray readyTime, DoubleArray serviceTime) { 162 158 double t = 0.0; 163 159 for (int i = begin + 1; i < route.Count; i++) { 164 t += Distance(route[i - 1], route[i] );160 t += Distance(route[i - 1], route[i], coordinates, useDistanceMatrix); 165 161 if (route[i] == 0) { 166 if (t < DueTimeParameter.ActualValue[0]) return true;162 if (t < dueTime[0]) return true; 167 163 else return false; 168 164 } else { 169 if (t > DueTimeParameter.ActualValue[route[i]]) return false;170 t = Math.Max( ReadyTimeParameter.ActualValue[route[i]], t);171 t += ServiceTimeParameter.ActualValue[route[i]];165 if (t > dueTime[route[i]]) return false; 166 t = Math.Max(readyTime[route[i]], t); 167 t += serviceTime[route[i]]; 172 168 } 173 169 } … … 175 171 } 176 172 177 private bool SubrouteLoadOK(List<int> route, int begin ) {173 private bool SubrouteLoadOK(List<int> route, int begin, DoubleValue capacity, DoubleArray demand) { 178 174 double o = 0.0; 179 175 for (int i = begin + 1; i < route.Count; i++) { 180 if (route[i] == 0) return (o < CapacityParameter.ActualValue.Value);176 if (route[i] == 0) return (o < capacity.Value); 181 177 else { 182 o += DemandParameter.ActualValue[route[i]];183 } 184 } 185 return (o < CapacityParameter.ActualValue.Value);178 o += demand[route[i]]; 179 } 180 } 181 return (o < capacity.Value); 186 182 } 187 183 188 184 protected override List<int> CreateSolution() { 189 double alpha, beta, gamma;190 alpha = N(Alpha.Value.Value, Math.Sqrt(AlphaVariance.Value.Value), RandomParameter.ActualValue);191 beta = N(Beta.Value.Value, Math.Sqrt(BetaVariance.Value.Value), RandomParameter.ActualValue);192 gamma = N(Gamma.Value.Value, Math.Sqrt(GammaVariance.Value.Value), RandomParameter.ActualValue);185 //double alpha, beta, gamma; 186 double alpha = N(Alpha.Value.Value, Math.Sqrt(AlphaVariance.Value.Value), RandomParameter.ActualValue); 187 double beta = N(Beta.Value.Value, Math.Sqrt(BetaVariance.Value.Value), RandomParameter.ActualValue); 188 double gamma = N(Gamma.Value.Value, Math.Sqrt(GammaVariance.Value.Value), RandomParameter.ActualValue); 193 189 194 190 double x0 = CoordinatesParameter.ActualValue[0, 0]; … … 203 199 int indexOfCustomer = -1; 204 200 201 int vehicles = VehiclesParameter.ActualValue.Value; 202 DoubleMatrix coordinates = CoordinatesParameter.ActualValue; 203 DoubleArray dueTime = DueTimeParameter.ActualValue; 204 DoubleArray serviceTime = ServiceTimeParameter.ActualValue; 205 DoubleArray readyTime = ReadyTimeParameter.ActualValue; 206 DoubleArray demand = DemandParameter.ActualValue; 207 DoubleValue capacity = CapacityParameter.ActualValue; 208 209 bool useDistanceMatrix = UseDistanceMatrixParameter.ActualValue.Value; 210 if (useDistanceMatrix) { 211 if (DistanceMatrixParameter.ActualValue == null) { 212 DistanceMatrixParameter.ActualValue = CreateDistanceMatrix(coordinates); 213 } 214 215 coordinates = DistanceMatrixParameter.ActualValue; 216 } 217 205 218 /*----------------------------------------------------------------------------- 206 219 * generate cost list … … 208 221 */ 209 222 for (int i = 1; i <= Cities; i++) { 210 distance = Distance(i, 0 );211 if ( CoordinatesParameter.ActualValue[i, 0] < x0) distance = -distance;223 distance = Distance(i, 0, coordinates, useDistanceMatrix); 224 if (coordinates[i, 0] < x0) distance = -distance; 212 225 cost = -alpha * distance + // distance 0 <-> City[i] 213 226 beta * (DueTimeParameter.ActualValue[i]) + // latest arrival time 214 gamma * (Math.Asin(( CoordinatesParameter.ActualValue[i, 1] - y0) / distance) / 360 * distance); // polar angle227 gamma * (Math.Asin((coordinates[i, 1] - y0) / distance) / 360 * distance); // polar angle 215 228 216 229 index = 0; … … 229 242 int customer = -1; 230 243 int subTourCount = 1; 231 List<int> route = new List<int>(Cities + VehiclesParameter.ActualValue.Value - 1); 244 245 List<int> route = new List<int>(Cities + vehicles - 1); 232 246 minimumCost = double.MaxValue; 233 247 indexOfMinimumCost = -1; … … 244 258 route.Insert(i, (int)unroutedList[c]); 245 259 if (route[currentRoute] != 0) { throw new Exception("currentRoute not depot"); } 246 cost = TravelDistance(route, currentRoute );247 if (cost < minimumCost && SubrouteConstraintsOK(route, currentRoute )) {260 cost = TravelDistance(route, currentRoute, coordinates, useDistanceMatrix); 261 if (cost < minimumCost && SubrouteConstraintsOK(route, currentRoute, coordinates, useDistanceMatrix, dueTime, readyTime, serviceTime, demand, capacity)) { 248 262 minimumCost = cost; 249 263 indexOfMinimumCost = i; … … 275 289 customer = -1; 276 290 } while (unroutedList.Count > 0); 277 while (route.Count < Cities + VehiclesParameter.ActualValue.Value - 1)291 while (route.Count < Cities + vehicles) 278 292 route.Add(0); 279 293 -
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 } -
trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Potvin/Manipulators/PotvinLocalSearchManipulator.cs
r5445 r6449 49 49 50 50 private bool FindBetterInsertionPlace( 51 PotvinEncoding individual, int tour, int city, int length, 51 PotvinEncoding individual, 52 DoubleArray dueTime, DoubleArray readyTime, DoubleArray serviceTime, DoubleArray demand, 53 DoubleValue capacity, DistanceMatrix distMatrix, 54 int tour, int city, int length, 52 55 out int insertionTour, out int insertionPlace) { 53 56 bool insertionFound = false; … … 56 59 57 60 List<int> toBeDeleted = individual.Tours[tour].Cities.GetRange(city, length); 58 double distance = GetLength(individual.Tours[tour]);61 double distance = individual.Tours[tour].GetLength(distMatrix); 59 62 individual.Tours[tour].Cities.RemoveRange(city, length); 60 double removalBenefit = distance - GetLength(individual.Tours[tour]);63 double removalBenefit = distance - individual.Tours[tour].GetLength(distMatrix); 61 64 62 65 int currentTour = 0; … … 64 67 int currentCity = 0; 65 68 while (currentCity <= individual.Tours[currentTour].Cities.Count && !insertionFound) { 66 distance = GetLength(individual.Tours[currentTour]);69 distance = individual.Tours[currentTour].GetLength(distMatrix); 67 70 individual.Tours[currentTour].Cities.InsertRange(currentCity, toBeDeleted); 68 if ( Feasible(individual.Tours[currentTour])) {71 if (individual.Tours[currentTour].Feasible(dueTime, serviceTime, readyTime, demand, capacity, distMatrix)) { 69 72 double lengthIncrease = 70 GetLength(individual.Tours[currentTour]) - distance;73 individual.Tours[currentTour].GetLength(distMatrix) - distance; 71 74 if (removalBenefit > lengthIncrease) { 72 75 insertionTour = currentTour; … … 83 86 } 84 87 85 individual.Tours[tour].Cities.InsertRange(city, toBeDeleted); 88 individual.Tours[tour].Cities.InsertRange(city, toBeDeleted); 86 89 87 90 return insertionFound; … … 89 92 90 93 protected override void Manipulate(IRandom random, PotvinEncoding individual) { 94 BoolValue useDistanceMatrix = UseDistanceMatrixParameter.ActualValue; 95 DoubleMatrix coordinates = CoordinatesParameter.ActualValue; 96 DistanceMatrix distMatrix = VRPUtilities.GetDistanceMatrix(coordinates, DistanceMatrixParameter, useDistanceMatrix); 97 DoubleArray dueTime = DueTimeParameter.ActualValue; 98 DoubleArray readyTime = ReadyTimeParameter.ActualValue; 99 DoubleArray serviceTime = ServiceTimeParameter.ActualValue; 100 DoubleArray demand = DemandParameter.ActualValue; 101 DoubleValue capacity = CapacityParameter.ActualValue; 102 91 103 //only apply to feasible individuals 92 if (Feasible(individual)) { 104 bool feasible = true; 105 106 foreach (Tour tour in individual.Tours) { 107 if (!tour.Feasible(dueTime, serviceTime, readyTime, demand, capacity, distMatrix)) { 108 feasible = false; 109 break; 110 } 111 } 112 113 if (feasible) { 93 114 bool insertionFound; 94 115 int iterations = 0; … … 103 124 while (city <= individual.Tours[tour].Cities.Count - length && !insertionFound) { 104 125 int insertionTour, insertionPlace; 105 if (FindBetterInsertionPlace(individual, tour, city, length, 126 if (FindBetterInsertionPlace(individual, dueTime, readyTime, serviceTime, demand, capacity, distMatrix, 127 tour, city, length, 106 128 out insertionTour, out insertionPlace)) { 107 129 insertionFound = true; -
trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Potvin/Manipulators/PotvinManipulator.cs
r5445 r6449 25 25 using HeuristicLab.Parameters; 26 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 using HeuristicLab.Data; 27 28 28 29 namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin { … … 45 46 protected abstract void Manipulate(IRandom random, PotvinEncoding individual); 46 47 47 protected int SelectRandomTourBiasedByLength(IRandom random, PotvinEncoding individual) {48 protected static int SelectRandomTourBiasedByLength(IRandom random, PotvinEncoding individual) { 48 49 int tourIndex = -1; 49 50 … … 51 52 double[] probabilities = new double[individual.Tours.Count]; 52 53 for (int i = 0; i < individual.Tours.Count; i++) { 53 probabilities[i] = 1.0 / ((double)individual.Tours[i].Cities.Count / (double) Cities);54 probabilities[i] = 1.0 / ((double)individual.Tours[i].Cities.Count / (double)individual.Cities); 54 55 sum += probabilities[i]; 55 56 } … … 72 73 } 73 74 74 protected bool FindInsertionPlace(PotvinEncoding individual, int city, int routeToAvoid, out int route, out int place) { 75 protected static bool FindInsertionPlace(PotvinEncoding individual, int city, int routeToAvoid, 76 DoubleArray dueTime, DoubleArray serviceTime, DoubleArray readyTime, DoubleArray demand, 77 DoubleValue capacity, DistanceMatrix distMatrix, 78 out int route, out int place) { 75 79 return individual.FindInsertionPlace( 76 DueTimeParameter.ActualValue, ServiceTimeParameter.ActualValue, ReadyTimeParameter.ActualValue, 77 DemandParameter.ActualValue, CapacityParameter.ActualValue, CoordinatesParameter.ActualValue, 78 DistanceMatrixParameter, UseDistanceMatrixParameter.ActualValue, 80 dueTime, serviceTime, readyTime, 81 demand, capacity, distMatrix, 79 82 city, routeToAvoid, out route, out place); 80 83 } 84 81 85 82 86 public override IOperation Apply() { -
trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Potvin/Manipulators/PotvinOneLevelExchangeManipulator.cs
r5445 r6449 24 24 using HeuristicLab.Core; 25 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 26 using HeuristicLab.Data; 26 27 27 28 namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin { … … 39 40 public PotvinOneLevelExchangeMainpulator() : base() { } 40 41 41 protected override void Manipulate(IRandom random, PotvinEncoding individual) { 42 public static void Apply(IRandom random, PotvinEncoding individual, 43 DoubleArray dueTime, DoubleArray readyTime, DoubleArray serviceTime, DoubleArray demand, 44 DoubleValue capacity, DistanceMatrix distMatrix) { 42 45 int selectedIndex = SelectRandomTourBiasedByLength(random, individual); 43 46 Tour route1 = … … 48 51 int insertedRoute, insertedPlace; 49 52 50 if (FindInsertionPlace(individual, route1.Cities[i], selectedIndex, out insertedRoute, out insertedPlace)) { 53 if (FindInsertionPlace(individual, route1.Cities[i], selectedIndex, 54 dueTime, serviceTime, readyTime, demand, capacity, 55 distMatrix, 56 out insertedRoute, out insertedPlace)) { 51 57 individual.Tours[insertedRoute].Cities.Insert(insertedPlace, route1.Cities[i]); 52 58 replaced.Add(route1.Cities[i]); … … 65 71 individual.Tours.Remove(route1); 66 72 } 73 74 protected override void Manipulate(IRandom random, PotvinEncoding individual) { 75 BoolValue useDistanceMatrix = UseDistanceMatrixParameter.ActualValue; 76 DoubleMatrix coordinates = CoordinatesParameter.ActualValue; 77 DistanceMatrix distMatrix = VRPUtilities.GetDistanceMatrix(coordinates, DistanceMatrixParameter, useDistanceMatrix); 78 DoubleArray dueTime = DueTimeParameter.ActualValue; 79 DoubleArray readyTime = ReadyTimeParameter.ActualValue; 80 DoubleArray serviceTime = ServiceTimeParameter.ActualValue; 81 DoubleArray demand = DemandParameter.ActualValue; 82 DoubleValue capacity = CapacityParameter.ActualValue; 83 84 Apply(random, individual, dueTime, readyTime, serviceTime, demand, capacity, distMatrix); 85 } 67 86 } 68 87 } -
trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Potvin/Manipulators/PotvinTwoLevelExchangeManipulator.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 { … … 36 37 public PotvinTwoLevelExchangeManipulator() : base() { } 37 38 38 protected override void Manipulate(IRandom random, PotvinEncoding individual) { 39 public static void Apply(IRandom random, PotvinEncoding individual, 40 DoubleArray dueTime, DoubleArray readyTime, DoubleArray serviceTime, DoubleArray demand, 41 DoubleValue capacity, DistanceMatrix distMatrix) { 39 42 int selectedIndex = SelectRandomTourBiasedByLength(random, individual); 40 Tour route1 = individual.Tours[selectedIndex]; 43 Tour route1 = individual.Tours[selectedIndex]; 41 44 42 45 bool performed = false; … … 53 56 //customer1 can be feasibly inserted at the location of customer2 54 57 tour.Cities[customer2Position] = customer1; 55 if ( Feasible(tour)) {58 if (tour.Feasible(dueTime, serviceTime, readyTime, demand, capacity, distMatrix)) { 56 59 int routeIdx, place; 57 60 if (FindInsertionPlace(individual, 58 customer2, selectedIndex, out routeIdx, out place)) { 59 individual.Tours[routeIdx].Cities.Insert(place, customer2); 60 route1.Cities.RemoveAt(customer1Position); 61 customer2, selectedIndex, 62 dueTime, serviceTime, readyTime, demand, capacity, 63 distMatrix, 64 out routeIdx, out place)) { 65 individual.Tours[routeIdx].Cities.Insert(place, customer2); 66 route1.Cities.RemoveAt(customer1Position); 61 67 62 68 if (route1.Cities.Count == 0) 63 69 individual.Tours.Remove(route1); 64 70 … … 83 89 } 84 90 } 91 92 protected override void Manipulate(IRandom random, PotvinEncoding individual) { 93 BoolValue useDistanceMatrix = UseDistanceMatrixParameter.ActualValue; 94 DoubleMatrix coordinates = CoordinatesParameter.ActualValue; 95 DistanceMatrix distMatrix = VRPUtilities.GetDistanceMatrix(coordinates, DistanceMatrixParameter, useDistanceMatrix); 96 DoubleArray dueTime = DueTimeParameter.ActualValue; 97 DoubleArray readyTime = ReadyTimeParameter.ActualValue; 98 DoubleArray serviceTime = ServiceTimeParameter.ActualValue; 99 DoubleArray demand = DemandParameter.ActualValue; 100 DoubleValue capacity = CapacityParameter.ActualValue; 101 102 Apply(random, individual, dueTime, readyTime, serviceTime, demand, capacity, distMatrix); 103 } 85 104 } 86 105 } -
trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Potvin/PotvinEncoding.cs
r5445 r6449 69 69 DoubleArray dueTimeArray, 70 70 DoubleArray serviceTimeArray, DoubleArray readyTimeArray, DoubleArray demandArray, DoubleValue capacity, 71 D oubleMatrix coordinates, ILookupParameter<DoubleMatrix> distanceMatrix, BoolValue useDistanceMatrix,71 DistanceMatrix distMatrix, 72 72 int city, int routeToAvoid, out int route, out int place) { 73 73 route = -1; 74 74 place = -1; 75 bool bestFeasible = false; 75 76 double minDetour = 0; 76 77 … … 78 79 if (tour != routeToAvoid) { 79 80 for (int i = 0; i <= Tours[tour].Cities.Count; i++) { 80 double length = Tours[tour].GetLength( coordinates, distanceMatrix, useDistanceMatrix);81 double length = Tours[tour].GetLength(distMatrix); 81 82 82 83 Tours[tour].Cities.Insert(i, city); 83 84 84 if (Tours[tour].Feasible(dueTimeArray, serviceTimeArray, readyTimeArray, demandArray, 85 capacity, coordinates, distanceMatrix, useDistanceMatrix)) { 86 double newLength = Tours[tour].GetLength(coordinates, distanceMatrix, useDistanceMatrix); 85 bool feasible = Tours[tour].Feasible(dueTimeArray, serviceTimeArray, readyTimeArray, demandArray, 86 capacity, distMatrix); 87 87 88 if (!bestFeasible || feasible) { 89 double newLength = Tours[tour].GetLength(distMatrix); 88 90 double detour = newLength - length; 89 91 90 if (route <= 0 || detour < minDetour ) {92 if (route <= 0 || detour < minDetour || (!(bestFeasible && !feasible)) && detour < minDetour || (feasible && !bestFeasible)) { 91 93 route = tour; 92 94 place = i; 93 95 minDetour = detour; 96 97 if (feasible) 98 bestFeasible = true; 94 99 } 95 100 } -
trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Prins/Crossovers/PrinsPermutationCrossover.cs
r5445 r6449 46 46 47 47 protected override PrinsEncoding Crossover(IRandom random, PrinsEncoding parent1, PrinsEncoding parent2) { 48 //note - the inner crossover is called here and the resultis converted to a prins representation48 //note - the inner crossover is called here and the solution is converted to a prins representation 49 49 //some refactoring should be done here in the future - the crossover operation should be called directly 50 50 51 InnerCrossoverParameter.ActualValue.ParentsParameter.ActualName = ParentsParameter.ActualName; 52 IAtomicOperation op = this.ExecutionContext.CreateOperation( 53 InnerCrossoverParameter.ActualValue, this.ExecutionContext.Scope); 54 op.Operator.Execute((IExecutionContext)op, CancellationToken); 51 if (parent1.Length == parent2.Length) { 52 InnerCrossoverParameter.ActualValue.ParentsParameter.ActualName = ParentsParameter.ActualName; 53 IAtomicOperation op = this.ExecutionContext.CreateOperation( 54 InnerCrossoverParameter.ActualValue, this.ExecutionContext.Scope); 55 op.Operator.Execute((IExecutionContext)op, CancellationToken); 55 56 56 string childName = InnerCrossoverParameter.ActualValue.ChildParameter.ActualName;57 if (ExecutionContext.Scope.Variables.ContainsKey(childName)) {58 Permutation permutation = ExecutionContext.Scope.Variables[childName].Value as Permutation;59 ExecutionContext.Scope.Variables.Remove(childName);57 string childName = InnerCrossoverParameter.ActualValue.ChildParameter.ActualName; 58 if (ExecutionContext.Scope.Variables.ContainsKey(childName)) { 59 Permutation permutation = ExecutionContext.Scope.Variables[childName].Value as Permutation; 60 ExecutionContext.Scope.Variables.Remove(childName); 60 61 61 return new PrinsEncoding(permutation, Cities, 62 DueTimeParameter.ActualValue, 63 ServiceTimeParameter.ActualValue, 64 ReadyTimeParameter.ActualValue, 65 DemandParameter.ActualValue, 66 CapacityParameter.ActualValue, 67 FleetUsageFactor.ActualValue, 68 TimeFactor.ActualValue, 69 DistanceFactor.ActualValue, 70 OverloadPenalty.ActualValue, 71 TardinessPenalty.ActualValue, 72 CoordinatesParameter.ActualValue, 73 UseDistanceMatrixParameter.ActualValue); 74 } else 75 return null; 62 return new PrinsEncoding(permutation, Cities, 63 DueTimeParameter.ActualValue, 64 ServiceTimeParameter.ActualValue, 65 ReadyTimeParameter.ActualValue, 66 DemandParameter.ActualValue, 67 CapacityParameter.ActualValue, 68 FleetUsageFactor.ActualValue, 69 TimeFactor.ActualValue, 70 DistanceFactor.ActualValue, 71 OverloadPenalty.ActualValue, 72 TardinessPenalty.ActualValue, 73 CoordinatesParameter.ActualValue, 74 UseDistanceMatrixParameter.ActualValue); 75 } else 76 return null; 77 } else { 78 return parent1.Clone() as PrinsEncoding; 79 } 76 80 } 77 81 } -
trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Prins/PrinsEncoding.cs
r5445 r6449 75 75 public override List<Tour> GetTours(ILookupParameter<DoubleMatrix> distanceMatrix, int maxVehicles = int.MaxValue) { 76 76 List<Tour> result = new List<Tour>(); 77 78 DistanceMatrix distMatrix = VRPUtilities.GetDistanceMatrix(coordinates, distanceMatrix, useDistanceMatrix); 77 79 78 80 //Split permutation into vector P … … 106 108 overloadPenalty, 107 109 tardinessPenalty, 108 coordinates, 109 distanceMatrix, 110 useDistanceMatrix); 110 distMatrix); 111 111 112 112 double cost = eval.Quality; -
trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Tour.cs
r5445 r6449 48 48 public bool Feasible(DoubleArray dueTimeArray, 49 49 DoubleArray serviceTimeArray, DoubleArray readyTimeArray, DoubleArray demandArray, DoubleValue capacity, 50 D oubleMatrix coordinates, ILookupParameter<DoubleMatrix> distanceMatrix, BoolValue useDistanceMatrix) {50 DistanceMatrix distMatrix) { 51 51 TourEvaluation eval = VRPEvaluator.EvaluateTour(this, 52 52 dueTimeArray, … … 60 60 new DoubleValue(1), 61 61 new DoubleValue(1), 62 coordinates, 63 distanceMatrix, 64 useDistanceMatrix); 62 distMatrix); 65 63 66 64 return eval.Overload < double.Epsilon && eval.Tardiness < double.Epsilon; 67 65 } 68 66 69 public double GetLength(DoubleMatrix coordinates, 70 ILookupParameter<DoubleMatrix> distanceMatrix, 71 BoolValue useDistanceMatrix) { 67 public double GetLength(DistanceMatrix distMatrix) { 72 68 double length = 0; 73 69 … … 82 78 for (int i = 1; i < cities.Count; i++) { 83 79 length += VRPUtilities.GetDistance( 84 cities[i - 1], cities[i], coordinates, distanceMatrix, useDistanceMatrix);80 cities[i - 1], cities[i], distMatrix); 85 81 } 86 82 } -
trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Zhu/Crossovers/ZhuPermutationCrossover.cs
r5445 r6449 48 48 49 49 protected override ZhuEncoding Crossover(IRandom random, ZhuEncoding parent1, ZhuEncoding parent2) { 50 //note - the inner crossover is called here and the resultis converted to a prins representation50 //note - the inner crossover is called here and the solution is converted to a prins representation 51 51 //some refactoring should be done here in the future - the crossover operation should be called directly 52 52 53 InnerCrossoverParameter.ActualValue.ParentsParameter.ActualName = ParentsParameter.ActualName; 54 IAtomicOperation op = this.ExecutionContext.CreateOperation( 55 InnerCrossoverParameter.ActualValue, this.ExecutionContext.Scope); 56 op.Operator.Execute((IExecutionContext)op, CancellationToken); 53 if (parent1.Length == parent2.Length) { 54 InnerCrossoverParameter.ActualValue.ParentsParameter.ActualName = ParentsParameter.ActualName; 55 IAtomicOperation op = this.ExecutionContext.CreateOperation( 56 InnerCrossoverParameter.ActualValue, this.ExecutionContext.Scope); 57 op.Operator.Execute((IExecutionContext)op, CancellationToken); 57 58 58 string childName = InnerCrossoverParameter.ActualValue.ChildParameter.ActualName;59 if (ExecutionContext.Scope.Variables.ContainsKey(childName)) {60 Permutation permutation = ExecutionContext.Scope.Variables[childName].Value as Permutation;61 ExecutionContext.Scope.Variables.Remove(childName);59 string childName = InnerCrossoverParameter.ActualValue.ChildParameter.ActualName; 60 if (ExecutionContext.Scope.Variables.ContainsKey(childName)) { 61 Permutation permutation = ExecutionContext.Scope.Variables[childName].Value as Permutation; 62 ExecutionContext.Scope.Variables.Remove(childName); 62 63 63 return new ZhuEncoding(permutation, Cities, 64 DueTimeParameter.ActualValue, 65 ServiceTimeParameter.ActualValue, 66 ReadyTimeParameter.ActualValue, 67 DemandParameter.ActualValue, 68 CapacityParameter.ActualValue, 69 CoordinatesParameter.ActualValue, 70 UseDistanceMatrixParameter.ActualValue); 71 } else 72 return null; 64 return new ZhuEncoding(permutation, Cities, 65 DueTimeParameter.ActualValue, 66 ServiceTimeParameter.ActualValue, 67 ReadyTimeParameter.ActualValue, 68 DemandParameter.ActualValue, 69 CapacityParameter.ActualValue, 70 CoordinatesParameter.ActualValue, 71 UseDistanceMatrixParameter.ActualValue); 72 } else 73 return null; 74 } else { 75 return parent1.Clone() as ZhuEncoding; 76 } 73 77 } 74 78 } -
trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Zhu/ZhuEncoding.cs
r5445 r6449 63 63 Tour newTour = new Tour(); 64 64 65 DistanceMatrix distMatrix = VRPUtilities.GetDistanceMatrix(coordinates, distanceMatrix, useDistanceMatrix); 66 65 67 for (int i = 0; i < this.Length; i++) { 66 68 int city = this[i] + 1; … … 72 74 demandArray, 73 75 capacity, 74 coordinates, 75 distanceMatrix, 76 useDistanceMatrix)) { 76 distMatrix)) { 77 77 newTour.Cities.Remove(city); 78 78 if (newTour.Cities.Count > 0) -
trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Evaluators/VRPEvaluator.cs
r5445 r6449 116 116 DoubleArray serviceTimeArray, DoubleArray readyTimeArray, DoubleArray demandArray, DoubleValue capacity, 117 117 DoubleValue fleetUsageFactor, DoubleValue timeFactor, DoubleValue distanceFactor, DoubleValue overloadPenalty, DoubleValue tardinessPenalty, 118 D oubleMatrix coordinates, IParameter distanceMatrix, BoolValue useDistanceMatrix) {118 DistanceMatrix distMatrix) { 119 119 TourEvaluation eval = new TourEvaluation(); 120 120 … … 140 140 141 141 //drive there 142 double currentDistace = VRPUtilities.GetDistance(start, end, coordinates, distanceMatrix, useDistanceMatrix);142 double currentDistace = VRPUtilities.GetDistance(start, end, distMatrix); 143 143 distance += currentDistace; 144 144 time += currentDistace; … … 199 199 sumEval.Tardiness = 0; 200 200 201 DistanceMatrix distMatrix = VRPUtilities.GetDistanceMatrix(coordinates, distanceMatrix, useDistanceMatrix); 202 201 203 foreach (Tour tour in solution.GetTours(distanceMatrix as ILookupParameter<DoubleMatrix>)) { 202 204 TourEvaluation eval = EvaluateTour(tour, dueTimeArray, serviceTimeArray, readyTimeArray, demandArray, capacity, 203 205 fleetUsageFactor, timeFactor, distanceFactor, overloadPenalty, tardinessPenalty, 204 coordinates, distanceMatrix, useDistanceMatrix);206 distMatrix); 205 207 sumEval.Quality += eval.Quality; 206 208 sumEval.Distance += eval.Distance; -
trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/HeuristicLab.Problems.VehicleRouting-3.3.csproj
r6042 r6449 112 112 <Compile Include="Analyzers\BestVRPToursMemorizer.cs" /> 113 113 <Compile Include="Analyzers\BestVRPSolutionAnalyzer.cs" /> 114 <Compile Include="Encodings\Alba\LocalImprovement\AlbaLambdaInterchangeLocalImprovementOperator.cs" /> 115 <Compile Include="Encodings\General\Creators\IterativeInsertionCreator.cs" /> 116 <Compile Include="Encodings\Potvin\Crossovers\PotvinInsertionBasedCrossover.cs" /> 117 <Compile Include="Encodings\Potvin\Manipulators\PotvinLocalSearchManipulator.cs" /> 114 118 <Compile Include="Interfaces\IVRPMultiNeighborhoodShakingOperator.cs" /> 115 119 <Compile Include="ShakingOperators\VehicleRoutingShakingOperator.cs" /> … … 162 166 <Compile Include="Encodings\Potvin\Crossovers\PotvinSequenceBasedCrossover.cs" /> 163 167 <Compile Include="Encodings\Potvin\Crossovers\PotvinCrossover.cs" /> 164 <Compile Include="Encodings\Potvin\Manipulators\PotvinLocalSearchManipulator.cs" />165 168 <Compile Include="Encodings\Potvin\Manipulators\PotvinTwoLevelExchangeManipulator.cs" /> 166 169 <Compile Include="Encodings\Potvin\Manipulators\PotvinOneLevelExchangeManipulator.cs" /> -
trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/VRPOperator.cs
r5445 r6449 123 123 Parameters.Add(new LookupParameter<DoubleArray>("ServiceTime", "The service time of each customer.")); 124 124 } 125 126 protected bool Feasible(Tour tour) {127 return tour.Feasible(128 DueTimeParameter.ActualValue,129 ServiceTimeParameter.ActualValue,130 ReadyTimeParameter.ActualValue,131 DemandParameter.ActualValue,132 CapacityParameter.ActualValue,133 CoordinatesParameter.ActualValue,134 DistanceMatrixParameter,135 UseDistanceMatrixParameter.ActualValue);136 }137 138 protected bool Feasible(IVRPEncoding solution) {139 bool feasible = true;140 141 foreach (Tour tour in solution.GetTours(DistanceMatrixParameter)) {142 if (!Feasible(tour)) {143 feasible = false;144 break;145 }146 }147 148 return feasible;149 }150 151 protected double GetLength(Tour tour) {152 return tour.GetLength(153 CoordinatesParameter.ActualValue,154 DistanceMatrixParameter,155 UseDistanceMatrixParameter.ActualValue);156 }157 125 } 158 126 } -
trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/VRPUtilities.cs
r5445 r6449 25 25 26 26 namespace HeuristicLab.Problems.VehicleRouting { 27 public struct DistanceMatrix { 28 public DoubleMatrix Matrix { get; set; } 29 public BoolValue UseDistanceMatrix { get; set; } 30 } 31 27 32 public sealed class VRPUtilities { 28 33 public static double CalculateDistance(int start, int end, DoubleMatrix coordinates) { … … 50 55 51 56 return distanceMatrix; 57 } 58 59 public static DistanceMatrix GetDistanceMatrix(DoubleMatrix coordinates, IParameter distanceMatrix, BoolValue useDistanceMatrix) { 60 DistanceMatrix result = new DistanceMatrix(); 61 62 if (useDistanceMatrix.Value) { 63 result.UseDistanceMatrix = new BoolValue(true); 64 if (distanceMatrix is IValueParameter<DoubleMatrix>) { 65 if ((distanceMatrix as IValueParameter<DoubleMatrix>).Value == null) { 66 (distanceMatrix as IValueParameter<DoubleMatrix>).Value = CreateDistanceMatrix(coordinates); 67 } 68 69 result.Matrix = (distanceMatrix as IValueParameter<DoubleMatrix>).Value; 70 } else { 71 if (distanceMatrix.ActualValue == null) { 72 distanceMatrix.ActualValue = CreateDistanceMatrix(coordinates); 73 } 74 75 result.Matrix = (distanceMatrix.ActualValue as DoubleMatrix); 76 } 77 } else { 78 result.UseDistanceMatrix = new BoolValue(false); 79 result.Matrix = coordinates; 80 } 81 82 return result; 83 } 84 85 public static double GetDistance(int start, int end, DistanceMatrix distMatrix) { 86 double distance = 0.0; 87 88 if (distMatrix.UseDistanceMatrix.Value) 89 distance = distMatrix.Matrix[start, end]; 90 else 91 distance = CalculateDistance(start, end, distMatrix.Matrix); 92 93 return distance; 52 94 } 53 95
Note: See TracChangeset
for help on using the changeset viewer.