Changeset 6459
- Timestamp:
- 06/21/11 10:27:03 (14 years ago)
- Location:
- trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Potvin
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Potvin/Crossovers/PotvinCrossover.cs
r6455 r6459 36 36 } 37 37 38 public IValueParameter<BoolValue> AllowInfeasibleSolutions { 39 get { return (IValueParameter<BoolValue>)Parameters["AllowInfeasibleSolutions"]; } 40 } 41 42 [StorableHook(HookType.AfterDeserialization)] 43 private void AfterDeserialization() { 44 // BackwardsCompatibility3.3 45 #region Backwards compatible code (remove with 3.4) 46 if (!Parameters.ContainsKey("AllowInfeasibleSolutions")) { 47 Parameters.Add(new ValueParameter<BoolValue>("AllowInfeasibleSolutions", "Indicates if infeasible solutions should be allowed.", new BoolValue(false))); 48 } 49 #endregion 50 } 51 38 52 [StorableConstructor] 39 53 protected PotvinCrossover(bool deserializing) : base(deserializing) { } … … 44 58 public PotvinCrossover() { 45 59 Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic manipulation operators.")); 60 Parameters.Add(new ValueParameter<BoolValue>("AllowInfeasibleSolutions", "Indicates if infeasible solutions should be allowed.", new BoolValue(false))); 46 61 } 47 62 … … 50 65 protected static bool FindInsertionPlace(PotvinEncoding individual, int city, 51 66 DoubleArray dueTime, DoubleArray serviceTime, DoubleArray readyTime, DoubleArray demand, 52 DoubleValue capacity, DistanceMatrix distMatrix, 67 DoubleValue capacity, DistanceMatrix distMatrix, bool allowInfeasible, 53 68 out int route, out int place) { 54 69 return individual.FindInsertionPlace( 55 70 dueTime, serviceTime, readyTime, 56 71 demand, capacity, distMatrix, 57 city, -1, out route, out place); 72 city, -1, allowInfeasible, 73 out route, out place); 58 74 } 59 75 … … 72 88 73 89 protected static bool RouteUnrouted(PotvinEncoding solution, DistanceMatrix distMatrix, 74 DoubleArray dueTime, DoubleArray readyTime, DoubleArray serviceTime, DoubleArray demand, DoubleValue capacity ) {90 DoubleArray dueTime, DoubleArray readyTime, DoubleArray serviceTime, DoubleArray demand, DoubleValue capacity, bool allowInfeasible) { 75 91 bool success = true; 76 92 int index = 0; … … 81 97 if (FindInsertionPlace(solution, unrouted, 82 98 dueTime, serviceTime, readyTime, demand, capacity, 83 distMatrix, 99 distMatrix, allowInfeasible, 84 100 out route, out place)) { 85 101 solution.Tours[route].Cities.Insert(place, unrouted); … … 98 114 99 115 protected static bool Repair(IRandom random, PotvinEncoding solution, Tour newTour, DistanceMatrix distmatrix, 100 DoubleArray dueTime, DoubleArray readyTime, DoubleArray serviceTime, DoubleArray demand, DoubleValue capacity) { 116 DoubleArray dueTime, DoubleArray readyTime, DoubleArray serviceTime, DoubleArray demand, DoubleValue capacity, 117 bool allowInfeasible) { 101 118 bool success = true; 102 119 … … 134 151 } 135 152 136 if (! newTour.Feasible(153 if (!allowInfeasible && !newTour.Feasible( 137 154 dueTime, serviceTime, readyTime, demand, capacity, distmatrix)) 138 155 return false; 139 156 140 157 //route unrouted vehicles 141 success = RouteUnrouted(solution, distmatrix, dueTime, readyTime, serviceTime, demand, capacity );158 success = RouteUnrouted(solution, distmatrix, dueTime, readyTime, serviceTime, demand, capacity, allowInfeasible); 142 159 143 160 return success; -
trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Potvin/Crossovers/PotvinInsertionBasedCrossover.cs
r6455 r6459 157 157 DoubleArray dueTimeArray, 158 158 DoubleArray serviceTimeArray, DoubleArray readyTimeArray, DoubleArray demandArray, DoubleValue capacity, 159 DistanceMatrix distMatrix, 160 int city, out int place) { 159 DistanceMatrix distMatrix, int city, bool allowInfeasible, out int place) { 161 160 place = -1; 162 161 bool bestFeasible = false; … … 171 170 capacity, distMatrix); 172 171 173 if ( !bestFeasible || feasible) {172 if ((!allowInfeasible && feasible) || (allowInfeasible && (!bestFeasible || feasible))) { 174 173 double newLength = tour.GetLength(distMatrix); 175 174 double detour = newLength - length; 176 175 177 if (place <= 0 || (!(bestFeasible && !feasible)) && detour < minDetour || (feasible && !bestFeasible)) { 176 if (place <= 0 || detour < minDetour || 177 (allowInfeasible && ((!(bestFeasible && !feasible)) && detour < minDetour || (feasible && !bestFeasible)))) { 178 178 place = i; 179 179 minDetour = detour; … … 203 203 DoubleValue capacity = CapacityParameter.ActualValue; 204 204 205 bool allowInfeasible = AllowInfeasibleSolutions.Value.Value; 206 205 207 List<Tour> R1 = new List<Tour>(); 206 208 PotvinEncoding p1Clone = parent1.Clone() as PotvinEncoding; … … 247 249 248 250 int place = -1; 249 if(FindRouteInsertionPlace(childTour, dueTime, serviceTime, readyTime, 250 demand, capacity, distMatrix, city, out place)) {251 if(FindRouteInsertionPlace(childTour, dueTime, serviceTime, readyTime, 252 demand, capacity, distMatrix, city, allowInfeasible, out place)) { 251 253 childTour.Cities.Insert(place, city); 252 254 253 if (!Repair(random, child, childTour, distMatrix, dueTime, readyTime, serviceTime, demand, capacity )) {255 if (!Repair(random, child, childTour, distMatrix, dueTime, readyTime, serviceTime, demand, capacity, allowInfeasible)) { 254 256 childTour.Cities.RemoveAt(place); 255 257 insertSuccess = false; … … 261 263 262 264 child.Tours.Add(childTour); 263 if (!Repair(random, child, childTour, distMatrix, dueTime, readyTime, serviceTime, demand, capacity )) {265 if (!Repair(random, child, childTour, distMatrix, dueTime, readyTime, serviceTime, demand, capacity, allowInfeasible)) { 264 266 /*success = false; 265 267 break;*/ … … 271 273 Tour childTour = p1Clone.Tours[i].Clone() as Tour; 272 274 child.Tours.Add(childTour); 273 if (!Repair(random, child, childTour, distMatrix, dueTime, readyTime, serviceTime, demand, capacity )) {275 if (!Repair(random, child, childTour, distMatrix, dueTime, readyTime, serviceTime, demand, capacity, allowInfeasible)) { 274 276 /*success = false; 275 277 break;*/ … … 285 287 } 286 288 287 if (!RouteUnrouted(child, distMatrix, dueTime, readyTime, serviceTime, demand, capacity )) {289 if (!RouteUnrouted(child, distMatrix, dueTime, readyTime, serviceTime, demand, capacity, allowInfeasible)) { 288 290 success = false; 289 291 } 290 292 } 291 293 292 if (success )294 if (success || allowInfeasible) 293 295 return child; 294 296 else { -
trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Potvin/Crossovers/PotvinRouteBasedCrossover.cs
r6449 r6459 50 50 DoubleValue capacity = CapacityParameter.ActualValue; 51 51 52 bool allowInfeasible = AllowInfeasibleSolutions.Value.Value; 53 52 54 PotvinEncoding child = parent2.Clone() as PotvinEncoding; 53 55 … … 65 67 child.Unrouted.Add(city); 66 68 67 if (Repair(random, child, replacing, distMatrix, dueTime, readyTime, serviceTime, demand, capacity ))69 if (Repair(random, child, replacing, distMatrix, dueTime, readyTime, serviceTime, demand, capacity, allowInfeasible) || allowInfeasible) 68 70 return child; 69 71 else { -
trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Potvin/Crossovers/PotvinSequenceBasedCrossover.cs
r6449 r6459 51 51 DoubleValue capacity = CapacityParameter.ActualValue; 52 52 53 bool allowInfeasible = AllowInfeasibleSolutions.Value.Value; 54 53 55 PotvinEncoding child = parent1.Clone() as PotvinEncoding; 54 56 Tour newTour = new Tour(); … … 79 81 child.Unrouted.Add(city); 80 82 81 if (Repair(random, child, newTour, distMatrix, dueTime, readyTime, serviceTime, demand, capacity )) {83 if (Repair(random, child, newTour, distMatrix, dueTime, readyTime, serviceTime, demand, capacity, allowInfeasible) || allowInfeasible) { 82 84 return child; 83 85 } else { -
trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Potvin/Manipulators/PotvinManipulator.cs
r6449 r6459 35 35 } 36 36 37 public IValueParameter<BoolValue> AllowInfeasibleSolutions { 38 get { return (IValueParameter<BoolValue>)Parameters["AllowInfeasibleSolutions"]; } 39 } 40 41 [StorableHook(HookType.AfterDeserialization)] 42 private void AfterDeserialization() { 43 // BackwardsCompatibility3.3 44 #region Backwards compatible code (remove with 3.4) 45 if (!Parameters.ContainsKey("AllowInfeasibleSolutions")) { 46 Parameters.Add(new ValueParameter<BoolValue>("AllowInfeasibleSolutions", "Indicates if infeasible solutions should be allowed.", new BoolValue(false))); 47 } 48 #endregion 49 } 50 37 51 [StorableConstructor] 38 52 protected PotvinManipulator(bool deserializing) : base(deserializing) { } … … 42 56 public PotvinManipulator() { 43 57 Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic manipulation operators.")); 58 Parameters.Add(new ValueParameter<BoolValue>("AllowInfeasibleSolutions", "Indicates if infeasible solutions should be allowed.", new BoolValue(false))); 44 59 } 45 60 … … 75 90 protected static bool FindInsertionPlace(PotvinEncoding individual, int city, int routeToAvoid, 76 91 DoubleArray dueTime, DoubleArray serviceTime, DoubleArray readyTime, DoubleArray demand, 77 DoubleValue capacity, DistanceMatrix distMatrix, 92 DoubleValue capacity, DistanceMatrix distMatrix, bool allowInfeasible, 78 93 out int route, out int place) { 79 94 return individual.FindInsertionPlace( 80 95 dueTime, serviceTime, readyTime, 81 96 demand, capacity, distMatrix, 82 city, routeToAvoid, out route, out place); 97 city, routeToAvoid, allowInfeasible, 98 out route, out place); 83 99 } 84 100 -
trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Potvin/Manipulators/PotvinOneLevelExchangeManipulator.cs
r6449 r6459 42 42 public static void Apply(IRandom random, PotvinEncoding individual, 43 43 DoubleArray dueTime, DoubleArray readyTime, DoubleArray serviceTime, DoubleArray demand, 44 DoubleValue capacity, DistanceMatrix distMatrix ) {44 DoubleValue capacity, DistanceMatrix distMatrix, bool allowInfeasible) { 45 45 int selectedIndex = SelectRandomTourBiasedByLength(random, individual); 46 46 Tour route1 = … … 53 53 if (FindInsertionPlace(individual, route1.Cities[i], selectedIndex, 54 54 dueTime, serviceTime, readyTime, demand, capacity, 55 distMatrix, 55 distMatrix, allowInfeasible, 56 56 out insertedRoute, out insertedPlace)) { 57 57 individual.Tours[insertedRoute].Cities.Insert(insertedPlace, route1.Cities[i]); … … 82 82 DoubleValue capacity = CapacityParameter.ActualValue; 83 83 84 Apply(random, individual, dueTime, readyTime, serviceTime, demand, capacity, distMatrix); 84 bool allowInfeasible = AllowInfeasibleSolutions.Value.Value; 85 86 Apply(random, individual, dueTime, readyTime, serviceTime, demand, capacity, distMatrix, allowInfeasible); 85 87 } 86 88 } -
trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Potvin/Manipulators/PotvinTwoLevelExchangeManipulator.cs
r6449 r6459 39 39 public static void Apply(IRandom random, PotvinEncoding individual, 40 40 DoubleArray dueTime, DoubleArray readyTime, DoubleArray serviceTime, DoubleArray demand, 41 DoubleValue capacity, DistanceMatrix distMatrix ) {41 DoubleValue capacity, DistanceMatrix distMatrix, bool allowInfeasible) { 42 42 int selectedIndex = SelectRandomTourBiasedByLength(random, individual); 43 43 Tour route1 = individual.Tours[selectedIndex]; … … 61 61 customer2, selectedIndex, 62 62 dueTime, serviceTime, readyTime, demand, capacity, 63 distMatrix, 63 distMatrix, allowInfeasible, 64 64 out routeIdx, out place)) { 65 65 individual.Tours[routeIdx].Cities.Insert(place, customer2); … … 100 100 DoubleValue capacity = CapacityParameter.ActualValue; 101 101 102 Apply(random, individual, dueTime, readyTime, serviceTime, demand, capacity, distMatrix); 102 bool allowInfeasible = AllowInfeasibleSolutions.Value.Value; 103 104 Apply(random, individual, dueTime, readyTime, serviceTime, demand, capacity, distMatrix, allowInfeasible); 103 105 } 104 106 } -
trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Potvin/PotvinEncoding.cs
r6450 r6459 69 69 DoubleArray dueTimeArray, 70 70 DoubleArray serviceTimeArray, DoubleArray readyTimeArray, DoubleArray demandArray, DoubleValue capacity, 71 DistanceMatrix distMatrix, 72 int city, int routeToAvoid, out int route, out int place) { 71 DistanceMatrix distMatrix, 72 int city, int routeToAvoid, bool allowInfeasible, 73 out int route, out int place) { 73 74 route = -1; 74 75 place = -1; 76 bool bestFeasible = false; 75 77 double minDetour = 0; 76 78 … … 85 87 capacity, distMatrix); 86 88 87 if ( feasible) {89 if ((!allowInfeasible && feasible) || (allowInfeasible && (!bestFeasible || feasible))) { 88 90 double newLength = Tours[tour].GetLength(distMatrix); 89 91 double detour = newLength - length; 90 92 91 if (route <= 0 || detour < minDetour) { 93 if (route <= 0 || detour < minDetour || 94 (allowInfeasible && ((!(bestFeasible && !feasible)) && detour < minDetour || (feasible && !bestFeasible)))) { 92 95 route = tour; 93 96 place = i; 94 97 minDetour = detour; 98 99 if (feasible) 100 bestFeasible = true; 95 101 } 96 102 }
Note: See TracChangeset
for help on using the changeset viewer.