Changeset 6907 for branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Crossovers
- Timestamp:
- 10/12/11 13:41:14 (13 years ago)
- Location:
- branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Crossovers
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Crossovers/PotvinRouteBasedCrossover.cs
r6857 r6907 50 50 PotvinEncoding child = parent2.Clone() as PotvinEncoding; 51 51 52 int tourParent1 = random.Next(parent1.Tours.Count); 53 Tour replacing = parent1.Tours[tourParent1].Clone() as Tour; 52 if (parent1.Tours.Count > 0 && child.Tours.Count > 0) { 53 int tourParent1 = random.Next(parent1.Tours.Count); 54 Tour replacing = parent1.Tours[tourParent1].Clone() as Tour; 54 55 55 int tourParent2 = random.Next(child.Tours.Count);56 Tour replaced = child.Tours[tourParent2];56 int tourParent2 = random.Next(child.Tours.Count); 57 Tour replaced = child.Tours[tourParent2]; 57 58 58 child.Tours.Remove(replaced);59 child.Tours.Insert(tourParent2, replacing);59 child.Tours.Remove(replaced); 60 child.Tours.Insert(tourParent2, replacing); 60 61 61 62 Permutation vehicleAssignment = child.VehicleAssignment; … … 70 71 break; 71 72 } 72 } 73 } 73 74 74 foreach (int city in replaced.Stops)75 if (FindRoute(child, city) == null && !child.Unrouted.Contains(city))76 child.Unrouted.Add(city);75 foreach (int city in replaced.Stops) 76 if (FindRoute(child, city) == null && !child.Unrouted.Contains(city)) 77 child.Unrouted.Add(city); 77 78 78 if (Repair(random, child, replacing, ProblemInstance, allowInfeasible) || allowInfeasible) 79 if (Repair(random, child, replacing, ProblemInstance, allowInfeasible) || allowInfeasible) 80 return child; 81 else { 82 if (random.NextDouble() < 0.5) 83 return parent1.Clone() as PotvinEncoding; 84 else 85 return parent2.Clone() as PotvinEncoding; 86 } 87 } else { 79 88 return child; 80 else {81 if(random.NextDouble() < 0.5)82 return parent1.Clone() as PotvinEncoding;83 else84 return parent2.Clone() as PotvinEncoding;85 89 } 86 90 } -
branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Crossovers/PotvinSequenceBasedCrossover.cs
r6851 r6907 53 53 int cities = ProblemInstance.Cities.Value; 54 54 55 int breakPoint1 = random.Next(1, cities + 1); 56 Tour tour1 = FindRoute(child, breakPoint1); 57 breakPoint1 = tour1.Stops.IndexOf(breakPoint1); 55 if (cities > 0) { 56 int breakPoint1 = random.Next(1, cities + 1); 57 Tour tour1 = FindRoute(child, breakPoint1); 58 breakPoint1 = tour1.Stops.IndexOf(breakPoint1); 58 59 59 for (int i = 0; i < breakPoint1; i++)60 newTour.Stops.Add(tour1.Stops[i]);60 for (int i = 0; i < breakPoint1; i++) 61 newTour.Stops.Add(tour1.Stops[i]); 61 62 62 int breakPoint2 = random.Next(1, cities + 1);63 Tour tour2 = FindRoute(parent2, breakPoint2);64 breakPoint2 = tour2.Stops.IndexOf(breakPoint2);63 int breakPoint2 = random.Next(1, cities + 1); 64 Tour tour2 = FindRoute(parent2, breakPoint2); 65 breakPoint2 = tour2.Stops.IndexOf(breakPoint2); 65 66 66 for (int i = breakPoint2; i < tour2.Stops.Count; i++)67 newTour.Stops.Add(tour2.Stops[i]);67 for (int i = breakPoint2; i < tour2.Stops.Count; i++) 68 newTour.Stops.Add(tour2.Stops[i]); 68 69 69 int tour1Index = child.Tours.IndexOf(tour1);70 child.Tours.Remove(tour1);71 child.Tours.Insert(tour1Index, newTour);70 int tour1Index = child.Tours.IndexOf(tour1); 71 child.Tours.Remove(tour1); 72 child.Tours.Insert(tour1Index, newTour); 72 73 73 foreach (int city in tour1.Stops)74 if (FindRoute(child, city) == null && !child.Unrouted.Contains(city))75 child.Unrouted.Add(city);74 foreach (int city in tour1.Stops) 75 if (FindRoute(child, city) == null && !child.Unrouted.Contains(city)) 76 child.Unrouted.Add(city); 76 77 77 foreach (int city in tour2.Stops)78 if (FindRoute(child, city) == null && !child.Unrouted.Contains(city))79 child.Unrouted.Add(city);78 foreach (int city in tour2.Stops) 79 if (FindRoute(child, city) == null && !child.Unrouted.Contains(city)) 80 child.Unrouted.Add(city); 80 81 81 if (Repair(random, child, newTour, ProblemInstance, allowInfeasible) || allowInfeasible) { 82 if (Repair(random, child, newTour, ProblemInstance, allowInfeasible) || allowInfeasible) { 83 return child; 84 } else { 85 if (random.NextDouble() < 0.5) 86 return parent1.Clone() as PotvinEncoding; 87 else 88 return parent2.Clone() as PotvinEncoding; 89 } 90 } else { 82 91 return child; 83 } else { 84 if (random.NextDouble() < 0.5) 85 return parent1.Clone() as PotvinEncoding; 86 else 87 return parent2.Clone() as PotvinEncoding; 88 } 92 } 89 93 } 90 94 }
Note: See TracChangeset
for help on using the changeset viewer.