- Timestamp:
- 09/29/11 15:51:56 (13 years ago)
- Location:
- branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings
- Files:
-
- 9 added
- 26 edited
- 2 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Alba/AlbaEncoding.cs
r6710 r6851 33 33 [Item("AlbaEncoding", "Represents an Alba encoding of VRP solutions. It is implemented as described in Alba, E. and Dorronsoro, B. (2004). Solving the Vehicle Routing Problem by Using Cellular Genetic Algorithms.")] 34 34 [StorableClass] 35 public class AlbaEncoding : PermutationEncoding , IHeterogenousCapacitatedEncoding{35 public class AlbaEncoding : PermutationEncoding { 36 36 #region IVRPEncoding Members 37 37 public override List<Tour> GetTours() { 38 Repair(); 38 39 List<Tour> result = new List<Tour>(); 39 40 … … 55 56 if (tour.Stops.Count > 0) { 56 57 result.Add(tour); 57 } 58 } 58 59 59 60 return result; 60 61 } 61 #endregion62 62 63 #region IHeterogenousCapacitatedEncoding Members 64 65 public int GetVehicleAssignment(int tour) { 63 public override int GetVehicleAssignment(int tour) { 66 64 int vehicleAssignment = -1; 67 65 Tour currentTour = GetTours()[tour]; … … 72 70 int lastStopIndex = this.IndexOf(lastStop); 73 71 74 if (lastStopIndex == this.Length - 1) 75 vehicleAssignment = this.ProblemInstance.Vehicles.Value - 1; 72 if (lastStopIndex == this.Length - 1) { 73 int i = this.Length - 1; 74 75 while (vehicleAssignment == -1) { 76 if (this.array[i] >= ProblemInstance.Cities.Value) { 77 vehicleAssignment = this.array[i] - ProblemInstance.Cities.Value; 78 } 79 80 i--; 81 } 82 } 76 83 else 77 84 vehicleAssignment = this[lastStopIndex + 1] - this.ProblemInstance.Cities.Value; … … 80 87 } 81 88 #endregion 89 90 public void Repair() { 91 int cities = ProblemInstance.Cities.Value; 92 93 if (this[this.Length - 1] < cities) { 94 int index = this.Length - 2; 95 while (this[index] < cities) { 96 index--; 97 } 98 99 int vehicle = this[index]; 100 for (int i = index; i < this.Length - 1; i++) 101 this[i] = this[i + 1]; 102 this[Length - 1] = vehicle; 103 } 104 } 82 105 83 106 public AlbaEncoding(Permutation permutation, IVRPProblemInstance instance) … … 100 123 public static AlbaEncoding ConvertFrom(List<int> routeParam, IVRPProblemInstance instance) { 101 124 List<int> route = new List<int>(routeParam); 102 route.RemoveAt(routeParam.Count - 1);103 125 104 126 int cities = instance.Cities.Value; … … 129 151 int emptyVehicles = instance.Vehicles.Value - tours.Count; 130 152 131 int[] array = new int[cities + tours.Count + emptyVehicles - 1];153 int[] array = new int[cities + tours.Count + emptyVehicles]; 132 154 int delimiter = 0; 133 155 int arrayIndex = 0; … … 140 162 141 163 if (arrayIndex != array.Length) { 142 if (encoding is IHeterogenousCapacitatedEncoding) { 143 array[arrayIndex] = cities + (encoding as IHeterogenousCapacitatedEncoding).GetVehicleAssignment(delimiter); 144 } else { 145 array[arrayIndex] = cities + delimiter; 146 } 164 array[arrayIndex] = cities + encoding.GetVehicleAssignment(delimiter); 147 165 148 166 delimiter++; … … 151 169 } 152 170 153 for (int i = 0; i < emptyVehicles - 1; i++) { 154 if (encoding is IHeterogenousCapacitatedEncoding) { 155 array[arrayIndex] = cities + (encoding as IHeterogenousCapacitatedEncoding).GetVehicleAssignment(delimiter); 156 } else { 157 array[arrayIndex] = cities + delimiter; 158 } 171 for (int i = 0; i < emptyVehicles; i++) { 172 array[arrayIndex] = cities + encoding.GetVehicleAssignment(delimiter); 159 173 160 174 delimiter++; -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Alba/Creators/AlbaCreator.cs
r4752 r6851 47 47 : base(original, cloner) { 48 48 } 49 50 public override IOperation Apply() { 51 (VRPToursParameter.ActualValue as AlbaEncoding).Repair(); 52 53 return base.Apply(); 54 } 49 55 } 50 56 } -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Alba/Crossovers/AlbaCrossover.cs
r4752 r6851 67 67 68 68 ChildParameter.ActualValue = 69 Crossover(RandomParameter.ActualValue, parents[0] as AlbaEncoding, parents[1] as AlbaEncoding); 69 Crossover(RandomParameter.ActualValue, parents[0] as AlbaEncoding, parents[1] as AlbaEncoding); 70 (ChildParameter.ActualValue as AlbaEncoding).Repair(); 70 71 71 72 return base.Apply(); -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Alba/IAlbaOperator.cs
r4369 r6851 32 32 33 33 namespace HeuristicLab.Problems.VehicleRouting.Encodings.Alba { 34 public interface IAlbaOperator: 35 ISingleDepotOperator, IHeterogenousCapacitatedOperator, I TimeWindowedOperator {34 public interface IAlbaOperator: 35 ISingleDepotOperator, IHeterogenousCapacitatedOperator, IMultiDepotOperator, ITimeWindowedOperator { 36 36 } 37 37 } -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Alba/Manipulators/AlbaManipulator.cs
r4752 r6851 71 71 72 72 Manipulate(RandomParameter.ActualValue, VRPToursParameter.ActualValue as AlbaEncoding); 73 (VRPToursParameter.ActualValue as AlbaEncoding).Repair(); 73 74 74 75 return base.Apply(); -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/ExtendedPotvin/Creators/IterativeInsertionCreator.cs
r6838 r6851 33 33 using HeuristicLab.Problems.VehicleRouting.Variants; 34 34 35 namespace HeuristicLab.Problems.VehicleRouting.Encodings. Potvin {35 namespace HeuristicLab.Problems.VehicleRouting.Encodings.ExtendedPotvin { 36 36 [Item("IterativeInsertionCreator", "Creates a randomly initialized VRP solution.")] 37 37 [StorableClass] 38 public sealed class IterativeInsertionCreator : PotvinCreator, IStochasticOperator {38 public sealed class IterativeInsertionCreator : ExtendedPotvinCreator, IStochasticOperator { 39 39 #region IStochasticOperator Members 40 40 public ILookupParameter<IRandom> RandomParameter { … … 65 65 66 66 private static double CalculateAngleToDepot(IVRPProblemInstance instance, int city) { 67 double dx = instance. Coordinates[0,0];68 double dy = instance. Coordinates[0,1];67 double dx = instance.GetCoordinates(0)[0]; 68 double dy = instance.GetCoordinates(0)[1]; 69 69 70 double cx = instance. Coordinates[city,0];71 double cy = instance. Coordinates[city,1];70 double cx = instance.GetCoordinates(city)[0]; 71 double cy = instance.GetCoordinates(city)[1]; 72 72 73 73 double alpha = Math.Atan((cx - dx) / (dy - cy)) * (180.0 / Math.PI); … … 82 82 } 83 83 84 private static PotvinEncoding CreateSolution(IVRPProblemInstance instance, IRandom random, bool adhereTimeWindows) {85 PotvinEncoding result = newPotvinEncoding(instance);84 private static ExtendedPotvinEncoding CreateSolution(IVRPProblemInstance instance, IRandom random, bool adhereTimeWindows) { 85 ExtendedPotvinEncoding result = new ExtendedPotvinEncoding(instance); 86 86 87 87 IPickupAndDeliveryProblemInstance pdp = instance as IPickupAndDeliveryProblemInstance; … … 89 89 List<int> customers = new List<int>(); 90 90 for (int i = 1; i <= instance.Cities.Value; i++) 91 if(pdp == null || pdp. Demand[i]>= 0)91 if(pdp == null || pdp.GetDemand(i) >= 0) 92 92 customers.Add(i); 93 93 … … 107 107 int index = (i + j) % customers.Count; 108 108 109 int stopIdx = result.FindBestInsertionPlace(currentTour, customers[index]); 109 int stopIdx = 0; 110 if(currentTour.Stops.Count > 0) 111 result.FindBestInsertionPlace(currentTour, customers[index]); 110 112 currentTour.Stops.Insert(stopIdx, customers[index]); 111 113 … … 122 124 currentTour.Stops.Remove(pdp.PickupDeliveryLocation[customers[index]]); 123 125 124 if (currentTour.Stops.Count >0)125 result.Tours. Add(currentTour);126 if (currentTour.Stops.Count == 0) 127 result.Tours.Remove(currentTour); 126 128 currentTour = new Tour(); 129 result.Tours.Add(currentTour); 127 130 128 131 currentTour.Stops.Add(customers[index]); -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/ExtendedPotvin/Creators/PushForwardInsertionCreator.cs
r6839 r6851 33 33 using HeuristicLab.Problems.VehicleRouting.Variants; 34 34 35 namespace HeuristicLab.Problems.VehicleRouting.Encodings. Potvin {35 namespace HeuristicLab.Problems.VehicleRouting.Encodings.ExtendedPotvin { 36 36 [Item("PushForwardInsertionCreator", "Creates a randomly initialized VRP solution.")] 37 37 [StorableClass] 38 public sealed class PushForwardInsertionCreator : PotvinCreator, IStochasticOperator {38 public sealed class PushForwardInsertionCreator : ExtendedPotvinCreator, IStochasticOperator { 39 39 #region IStochasticOperator Members 40 40 public ILookupParameter<IRandom> RandomParameter { … … 99 99 } 100 100 101 private static double Distance(IVRPProblemInstance problemInstance, int start, int end) { 102 return problemInstance.GetDistance(start, end); 103 } 104 105 private static PotvinEncoding CreateSolution(IVRPProblemInstance problemInstance, IRandom random, 101 private static double GetDistance(int start, int end, IVRPProblemInstance problemInstance) { 102 double distance = 0.0; 103 104 double startX = problemInstance.Coordinates[start, 0]; 105 double startY = problemInstance.Coordinates[start, 1]; 106 107 double endX = problemInstance.Coordinates[end, 0]; 108 double endY = problemInstance.Coordinates[end, 1]; 109 110 distance = 111 Math.Sqrt( 112 Math.Pow(startX - endX, 2) + 113 Math.Pow(startY - endY, 2)); 114 115 return distance; 116 } 117 118 private static ExtendedPotvinEncoding CreateSolution(IVRPProblemInstance problemInstance, IRandom random, 106 119 double alphaValue = 0.7, double betaValue = 0.1, double gammaValue = 0.2, 107 120 double alphaVariance = 0.5, double betaVariance = 0.07, double gammaVariance = 0.14) { 108 PotvinEncoding result = newPotvinEncoding(problemInstance);121 ExtendedPotvinEncoding result = new ExtendedPotvinEncoding(problemInstance); 109 122 110 123 double alpha, beta, gamma; … … 113 126 gamma = N(gammaValue, Math.Sqrt(gammaVariance), random); 114 127 115 double x0 = problemInstance.Coordinates[0, 0];116 double y0 = problemInstance.Coordinates[0, 1]; 128 int totalTours = 0; 129 117 130 double distance = 0; 118 131 double cost = 0; … … 121 134 122 135 IPickupAndDeliveryProblemInstance pdp = problemInstance as IPickupAndDeliveryProblemInstance; 123 124 /*----------------------------------------------------------------------------- 125 * generate cost list 126 *----------------------------------------------------------------------------- 127 */ 128 for (int i = 1; i <= problemInstance.Cities.Value; i++) { 129 //only insert sources 130 if (pdp == null || problemInstance.Demand[i] >= 0) { 131 distance = Distance(problemInstance, i, 0); 132 if (problemInstance.Coordinates[i, 0] < x0) distance = -distance; 133 134 cost = -alpha * distance + // distance 0 <-> City[i] 135 beta * (problemInstance as ITimeWindowedProblemInstance).DueTime[i] + // latest arrival time 136 gamma * (Math.Asin((problemInstance.Coordinates[i, 1] - y0) / distance) / 360 * distance); // polar angle 137 138 int index = 0; 139 while (index < costList.Count && costList[index] < cost) index++; 140 141 costList.Insert(index, cost); 142 143 unroutedList.Insert(index, i); 144 } 136 IMultiDepotProblemInstance mdp = problemInstance as IMultiDepotProblemInstance; 137 138 int depots = 1; 139 if (mdp != null) { 140 depots = mdp.Depots.Value; 141 for (int i = 0; i < result.VehicleAssignment.Length; i++) 142 result.VehicleAssignment[i] = -1; 145 143 } 146 144 147 double minimumCost = double.MaxValue; 148 int indexOfMinimumCost = -1; 149 int indexOfMinimumCost2 = -1; 150 int indexOfCustomer = -1; 151 152 Tour tour = new Tour(); 153 result.Tours.Add(tour); 154 155 tour.Stops.Add(unroutedList[0]); 156 157 if (pdp != null) { 158 tour.Stops.Add(pdp.PickupDeliveryLocation[unroutedList[0]]); 159 } 160 161 unroutedList.RemoveAt(0); 162 163 while (unroutedList.Count > 0) { 164 for (int c = 0; c < unroutedList.Count; c++) { 165 VRPEvaluation eval = 166 problemInstance.EvaluateTour(tour, result); 167 double originalCosts = eval.Quality; 168 int customer = unroutedList[c]; 169 170 for (int i = 0; i <= tour.Stops.Count; i++) { 171 tour.Stops.Insert(i, customer); 172 eval = problemInstance.EvaluateTour(tour, result); 173 double tourCost = eval.Quality - originalCosts; 174 175 if (pdp != null) { 176 for (int j = i + 1; j <= tour.Stops.Count; j++) { 177 bool feasible; 178 cost = tourCost + 179 problemInstance.GetInsertionCosts(eval, pdp.PickupDeliveryLocation[unroutedList[c]], 0, j, out feasible); 145 List<int> unrouted = new List<int>(); 146 for (int i = 1; i <= problemInstance.Cities.Value; i++) 147 if (pdp == null || problemInstance.GetDemand(i) >= 0) 148 unrouted.Add(i); 149 150 for (int depot = 0; depot < depots; depot++) { 151 int vehicleCount = problemInstance.Vehicles.Value; 152 List<int> vehicles = new List<int>(); 153 if (mdp != null) { 154 vehicleCount = 0; 155 for (int i = 0; i < mdp.VehicleDepotAssignment.Length; i++) { 156 if (mdp.VehicleDepotAssignment[i] == depot) { 157 vehicleCount++; 158 vehicles.Add(i); 159 } 160 } 161 } else { 162 for (int i = 0; i < problemInstance.Vehicles.Value; i++) 163 vehicles.Add(i); 164 } 165 166 costList.Clear(); 167 unroutedList.Clear(); 168 169 double x0 = problemInstance.Coordinates[depot, 0]; 170 double y0 = problemInstance.Coordinates[depot, 1]; 171 172 /*----------------------------------------------------------------------------- 173 * generate cost list 174 *----------------------------------------------------------------------------- 175 */ 176 for (int i = 1; i <= problemInstance.Cities.Value; i++) { 177 //only insert sources 178 if (unrouted.Contains(i) && (pdp == null || problemInstance.GetDemand(i) >= 0)) { 179 distance = GetDistance(i + depots - 1, depot, problemInstance); 180 if (problemInstance.Coordinates[i + depots - 1, 0] < x0) distance = -distance; 181 182 double dueTime = 0; 183 if (problemInstance is ITimeWindowedProblemInstance) 184 dueTime = (problemInstance as ITimeWindowedProblemInstance).DueTime[i]; 185 186 cost = -alpha * distance + // distance 0 <-> City[i] 187 beta * dueTime + // latest arrival time 188 gamma * (Math.Asin((problemInstance.Coordinates[i + depots - 1, 0] - y0) / distance) / 360 * distance); // polar angle 189 190 int index = 0; 191 while (index < costList.Count && costList[index] < cost) index++; 192 193 costList.Insert(index, cost); 194 195 unroutedList.Insert(index, i); 196 } 197 } 198 199 int tours = 0; 200 double minimumCost = double.MaxValue; 201 int indexOfMinimumCost = -1; 202 int indexOfMinimumCost2 = -1; 203 int indexOfCustomer = -1; 204 205 Tour tour = new Tour(); 206 result.Tours.Add(tour); 207 result.VehicleAssignment[totalTours] = vehicles[0]; 208 vehicles.RemoveAt(0); 209 tours++; 210 totalTours++; 211 212 tour.Stops.Add(unroutedList[0]); 213 214 if (pdp != null) { 215 tour.Stops.Add(pdp.PickupDeliveryLocation[unroutedList[0]]); 216 } 217 218 unrouted.Remove(unroutedList[0]); 219 unroutedList.RemoveAt(0); 220 221 while (unroutedList.Count > 0 && (tours < vehicleCount || depot == depots - 1)) { 222 for (int c = 0; c < unroutedList.Count; c++) { 223 VRPEvaluation eval = problemInstance.EvaluateTour(tour, result); 224 double originalCosts = eval.Quality; 225 int customer = unroutedList[c]; 226 227 for (int i = 0; i <= tour.Stops.Count; i++) { 228 tour.Stops.Insert(i, customer); 229 eval = problemInstance.EvaluateTour(tour, result); 230 double tourCost = eval.Quality - originalCosts; 231 232 if (pdp != null) { 233 for (int j = i + 1; j <= tour.Stops.Count; j++) { 234 bool feasible; 235 cost = tourCost + 236 problemInstance.GetInsertionCosts(eval, result, pdp.PickupDeliveryLocation[unroutedList[c]], 0, j, out feasible); 237 if (cost < minimumCost && feasible) { 238 minimumCost = cost; 239 indexOfMinimumCost = i; 240 indexOfMinimumCost2 = j; 241 indexOfCustomer = c; 242 } 243 } 244 } else { 245 cost = tourCost; 246 bool feasible = problemInstance.Feasible(eval); 180 247 if (cost < minimumCost && feasible) { 181 248 minimumCost = cost; 182 249 indexOfMinimumCost = i; 183 indexOfMinimumCost2 = j;184 250 indexOfCustomer = c; 185 251 } 186 252 } 187 } else { 188 cost = tourCost; 189 bool feasible = problemInstance.Feasible(eval); 190 if (cost < minimumCost && feasible) { 191 minimumCost = cost; 192 indexOfMinimumCost = i; 193 indexOfCustomer = c; 194 } 253 254 tour.Stops.RemoveAt(i); 195 255 } 196 197 tour.Stops.RemoveAt(i); 198 } 199 } 200 201 // insert customer if found 202 if (indexOfMinimumCost != -1) { 203 tour.Stops.Insert(indexOfMinimumCost, unroutedList[indexOfCustomer]); 204 if (pdp != null) { 205 tour.Stops.Insert(indexOfMinimumCost2, pdp.PickupDeliveryLocation[unroutedList[indexOfCustomer]]); 206 } 207 208 unroutedList.RemoveAt(indexOfCustomer); 209 } else { // no feasible customer found 210 tour = new Tour(); 211 result.Tours.Add(tour); 212 213 tour.Stops.Add(unroutedList[0]); 214 if (pdp != null) { 215 tour.Stops.Add(pdp.PickupDeliveryLocation[unroutedList[0]]); 216 } 217 218 unroutedList.RemoveAt(0); 219 } 220 // reset minimum 221 minimumCost = double.MaxValue; 222 indexOfMinimumCost = -1; 223 indexOfMinimumCost2 = -1; 224 indexOfCustomer = -1; 256 } 257 258 // insert customer if found 259 if (indexOfMinimumCost != -1) { 260 tour.Stops.Insert(indexOfMinimumCost, unroutedList[indexOfCustomer]); 261 if (pdp != null) { 262 tour.Stops.Insert(indexOfMinimumCost2, pdp.PickupDeliveryLocation[unroutedList[indexOfCustomer]]); 263 } 264 265 unrouted.Remove(unroutedList[indexOfCustomer]); 266 unroutedList.RemoveAt(indexOfCustomer); 267 } else if (tours < vehicleCount || depot == depots - 1) { // no feasible customer found 268 tour = new Tour(); 269 result.Tours.Add(tour); 270 result.VehicleAssignment[totalTours] = vehicles[0]; 271 vehicles.RemoveAt(0); 272 tours++; 273 totalTours++; 274 275 tour.Stops.Add(unroutedList[0]); 276 if (pdp != null) { 277 tour.Stops.Add(pdp.PickupDeliveryLocation[unroutedList[0]]); 278 } 279 280 unrouted.Remove(unroutedList[0]); 281 unroutedList.RemoveAt(0); 282 } 283 // reset minimum 284 minimumCost = double.MaxValue; 285 indexOfMinimumCost = -1; 286 indexOfMinimumCost2 = -1; 287 indexOfCustomer = -1; 288 } 289 } 290 291 if (mdp != null) { 292 List<int> availableVehicles = new List<int>(); 293 for (int i = 0; i < mdp.Vehicles.Value; i++) 294 availableVehicles.Add(i); 295 296 for (int i = 0; i < result.VehicleAssignment.Length; i++) { 297 if (result.VehicleAssignment[i] != -1) 298 availableVehicles.Remove(result.VehicleAssignment[i]); 299 } 300 301 for (int i = 0; i < result.VehicleAssignment.Length; i++) { 302 if (result.VehicleAssignment[i] == -1) { 303 result.VehicleAssignment[i] = availableVehicles[0]; 304 availableVehicles.RemoveAt(0); 305 } 306 } 225 307 } 226 308 -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/GVR/Crossovers/GVRCrossover.cs
r4752 r6851 82 82 for (int i = 1; i <= ProblemInstance.Cities.Value; i++) { 83 83 if (!subroute.Contains(i)) { 84 double distance = ProblemInstance.GetDistance(subroute[0], i );84 double distance = ProblemInstance.GetDistance(subroute[0], i, child); 85 85 86 86 if (customer == -1 || distance < minDistance) { -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/GVR/GVREncoding.cs
r4752 r6851 42 42 double currentDemand = 0; 43 43 44 DoubleArray demand = ProblemInstance.Demand;45 44 DoubleValue capacity = new DoubleValue(double.MaxValue); 46 45 if (ProblemInstance is IHomogenousCapacitatedProblemInstance) { … … 50 49 51 50 foreach (int city in tour.Stops) { 52 currentDemand += demand[city];51 currentDemand += ProblemInstance.GetDemand(city); 53 52 54 53 if (currentDemand > capacity.Value) { … … 58 57 newTour = new Tour(); 59 58 newTour.Stops.Add(city); 60 currentDemand = demand[city];59 currentDemand = ProblemInstance.GetDemand(city); 61 60 } else { 62 61 newTour.Stops.Add(city); -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/General/Crossovers/BiasedMultiVRPSolutionCrossover.cs
r6716 r6851 160 160 OperationCollection next = new OperationCollection(successorOp); 161 161 if (successor != null) { 162 SelectedOperatorParameter.ActualValue = new StringValue(successor. GetType().Name);162 SelectedOperatorParameter.ActualValue = new StringValue(successor.Name); 163 163 164 164 if (CreateChildOperation) -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/General/Manipulators/BiasedMultiVRPSolutionManipulator.cs
r6716 r6851 160 160 OperationCollection next = new OperationCollection(successorOp); 161 161 if (successor != null) { 162 SelectedOperatorParameter.ActualValue = new StringValue(successor. GetType().Name);162 SelectedOperatorParameter.ActualValue = new StringValue(successor.Name); 163 163 164 164 if (CreateChildOperation) -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/General/PermutationEncoding.cs
r4899 r6851 35 35 #region IVRPEncoding Members 36 36 public abstract List<Tour> GetTours(); 37 38 public int GetTourIndex(Tour tour) { 39 int index = -1; 40 41 List<Tour> tours = GetTours(); 42 for (int i = 0; i < tours.Count; i++) { 43 if (tours[i].IsEqual(tour)) { 44 index = i; 45 break; 46 } 47 } 48 49 return index; 50 } 51 52 public virtual int GetVehicleAssignment(int tour) { 53 return tour; 54 } 37 55 #endregion 38 56 -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/General/TourEncoding.cs
r6837 r6851 38 38 39 39 #region IVRPEncoding Members 40 public v oid Repair() {40 public virtual void Repair() { 41 41 List<Tour> toBeRemoved = new List<Tour>(); 42 42 foreach (Tour tour in Tours) { … … 62 62 63 63 return result; 64 } 65 66 public int GetTourIndex(Tour tour) { 67 int index = -1; 68 69 for (int i = 0; i < Tours.Count; i++) { 70 if (Tours[i].IsEqual(tour)) { 71 index = i; 72 break; 73 } 74 } 75 76 return index; 77 } 78 79 public virtual int GetVehicleAssignment(int tour) { 80 return tour; 64 81 } 65 82 -
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) { -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Tour.cs
r6607 r6851 48 48 } 49 49 50 public double GetTourLength(IVRPProblemInstance instance ) {50 public double GetTourLength(IVRPProblemInstance instance, IVRPEncoding solution) { 51 51 double length = 0; 52 52 … … 60 60 61 61 for (int i = 1; i < cities.Count; i++) { 62 length += instance.GetDistance(cities[i - 1], cities[i] );62 length += instance.GetDistance(cities[i - 1], cities[i], solution); 63 63 } 64 64 } … … 66 66 return length; 67 67 } 68 69 public bool IsEqual(Tour tour) { 70 bool equal = (tour != null) && (tour.Stops.Count == Stops.Count); 71 int index = 0; 72 73 while (equal && index < Stops.Count) { 74 equal = equal && tour.Stops[index] == Stops[index]; 75 index++; 76 } 77 78 return equal; 79 } 68 80 } 69 81 } -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Zhu/Crossovers/ZhuHeuristicCrossover1.cs
r6771 r6851 77 77 78 78 if (ProblemInstance.GetDistance( 79 child[i] + 1, parent1[(i + 1) % child.Length] + 1 )79 child[i] + 1, parent1[(i + 1) % child.Length] + 1, child) 80 80 < 81 81 ProblemInstance.GetDistance( 82 child[i] + 1, parent2[(i + 1) % child.Length] + 1 )) {82 child[i] + 1, parent2[(i + 1) % child.Length] + 1, child)) { 83 83 child[(i + 1) % child.Length] = parent1[(i + 1) % child.Length]; 84 84 Swap(parent2, parent2[(i + 1) % child.Length], parent1[(i + 1) % child.Length]); -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Zhu/Crossovers/ZhuHeuristicCrossover2.cs
r6771 r6851 79 79 80 80 if (ProblemInstance.GetDistance( 81 child[i] + 1, p1[parent1Index] + 1 )81 child[i] + 1, p1[parent1Index] + 1, child) 82 82 < 83 83 ProblemInstance.GetDistance( 84 child[i] + 1, p2[parent2Index] + 1 )) {84 child[i] + 1, p2[parent2Index] + 1, child)) { 85 85 child[(i + 1) % child.Length] = p1[parent1Index]; 86 86 } else {
Note: See TracChangeset
for help on using the changeset viewer.