Changeset 9750 for branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape.VRP/Manipulators
- Timestamp:
- 07/25/13 09:21:45 (11 years ago)
- Location:
- branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape.VRP/Manipulators
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape.VRP/Manipulators/ExchangeManipulator.cs
r7159 r9750 29 29 using HeuristicLab.Problems.VehicleRouting.Encodings; 30 30 using HeuristicLab.Problems.VehicleRouting; 31 using HeuristicLab.Problems.VehicleRouting.Interfaces; 31 32 32 33 namespace HeuristicLab.Analysis.FitnessLandscape.VRP { … … 46 47 } 47 48 48 private static bool RouteFeasible(Tour tour, DoubleArray demand, DoubleValue capacity) { 49 double routeLoad = 0; 50 for (int i = 0; i < tour.Cities.Count; i++) { 51 routeLoad += demand[tour.Cities[i]]; 52 } 53 54 return routeLoad <= capacity.Value; 55 } 56 57 public static void Apply(IRandom random, PotvinEncoding individual, DoubleArray demand, DoubleValue capacity, bool allowInfeasible) { 49 public static void Apply(IRandom random, PotvinEncoding individual, IVRPProblemInstance problemInstance, bool allowInfeasible) { 58 50 bool feasible; 59 51 … … 70 62 Tour tour2 = individual.Tours[tour2Idx]; 71 63 72 int index1 = random.Next(tour1. Cities.Count);73 int city1 = tour1. Cities[index1];64 int index1 = random.Next(tour1.Stops.Count); 65 int city1 = tour1.Stops[index1]; 74 66 75 int index2 = random.Next(tour2. Cities.Count);76 int city2 = tour2. Cities[index2];67 int index2 = random.Next(tour2.Stops.Count); 68 int city2 = tour2.Stops[index2]; 77 69 78 70 if (!allowInfeasible) { 79 bool originalFeasible = 80 RouteFeasible(tour1, demand, capacity) && 81 RouteFeasible(tour2, demand, capacity); 71 bool originalFeasible = problemInstance.TourFeasible(tour1, individual) && 72 problemInstance.TourFeasible(tour2, individual); 82 73 83 74 if (originalFeasible) { 84 double routeLoad = 0; 85 for (int i = 0; i < tour1.Cities.Count; i++) { 86 if (i != index1) 87 routeLoad += demand[tour1.Cities[i]]; 75 var tmpCity1 = tour1.Stops[index1]; 76 tour1.Stops.RemoveAt(index1); 77 tour1.Stops.Insert(index1, city2); 78 79 if (problemInstance.TourFeasible(tour1, individual)) { 80 var tmpCity2 = tour2.Stops[index2]; 81 tour2.Stops.RemoveAt(index2); 82 tour2.Stops.Insert(index2, city1); 83 84 feasible = problemInstance.TourFeasible(tour2, individual); 85 86 if (!feasible) { 87 // undo tour1 and tour2 changes 88 tour2.Stops.RemoveAt(index2); 89 tour2.Stops.Insert(index2, tmpCity2); 90 91 tour1.Stops.RemoveAt(index1); 92 tour1.Stops.Insert(index1, tmpCity1); 93 } 94 } else { 95 feasible = false; 96 // undo tour1 change 97 tour1.Stops.RemoveAt(index1); 98 tour1.Stops.Insert(index1, tmpCity1); 88 99 } 89 routeLoad += demand[city2]; 90 91 if (routeLoad > capacity.Value) { 92 feasible = false; 93 } else { 94 routeLoad = 0; 95 for (int i = 0; i < tour2.Cities.Count; i++) { 96 if (i != index2) 97 routeLoad += demand[tour2.Cities[i]]; 98 } 99 routeLoad += demand[city1]; 100 101 if (routeLoad > capacity.Value) { 102 feasible = false; 103 } 104 } 105 106 } 107 } 108 109 if (feasible) { 110 tour1.Cities.RemoveAt(index1); 111 tour1.Cities.Insert(index1, city2); 112 113 tour2.Cities.RemoveAt(index2); 114 tour2.Cities.Insert(index2, city1); 100 } else feasible = false; 115 101 } 116 102 } … … 119 105 120 106 protected override void Manipulate(IRandom random, PotvinEncoding individual) { 121 BoolValue useDistanceMatrix = UseDistanceMatrixParameter.ActualValue;122 DoubleMatrix coordinates = CoordinatesParameter.ActualValue;123 DistanceMatrix distMatrix = VRPUtilities.GetDistanceMatrix(coordinates, DistanceMatrixParameter, useDistanceMatrix);124 DoubleArray demand = DemandParameter.ActualValue;125 DoubleValue capacity = CapacityParameter.ActualValue;126 127 107 bool allowInfeasible = AllowInfeasibleSolutions.Value.Value; 128 Apply(random, individual, demand, capacity, allowInfeasible);108 Apply(random, individual, ProblemInstance, allowInfeasible); 129 109 } 130 110 } -
branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape.VRP/Manipulators/OrOptManipulator.cs
r7159 r9750 48 48 49 49 public static void Apply(IRandom random, PotvinEncoding individual) { 50 List<Tour> tours = individual.Tours.FindAll(t => t. Cities.Count >= 2);50 List<Tour> tours = individual.Tours.FindAll(t => t.Stops.Count >= 2); 51 51 52 52 if (tours.Count > 0) { … … 54 54 Tour tour = tours[tourIdx]; 55 55 56 int segmentStart = random.Next(tour. Cities.Count);56 int segmentStart = random.Next(tour.Stops.Count); 57 57 int segmentLength; 58 58 if (segmentStart == 0) { 59 segmentLength = 1 + random.Next(tour. Cities.Count - 1);59 segmentLength = 1 + random.Next(tour.Stops.Count - 1); 60 60 } else { 61 segmentLength = 1 + random.Next(tour. Cities.Count - segmentStart);61 segmentLength = 1 + random.Next(tour.Stops.Count - segmentStart); 62 62 } 63 63 64 List<int> segment = tour. Cities.GetRange(segmentStart, segmentLength);65 tour. Cities.RemoveRange(segmentStart, segmentLength);64 List<int> segment = tour.Stops.GetRange(segmentStart, segmentLength); 65 tour.Stops.RemoveRange(segmentStart, segmentLength); 66 66 int newPos; 67 if (tour.Cities.Count == 1)67 if (tour.Stops.Count == 1) 68 68 newPos = 0; 69 69 else 70 newPos = random.Next(tour. Cities.Count - 1);70 newPos = random.Next(tour.Stops.Count - 1); 71 71 72 72 if (newPos >= segmentStart) 73 73 newPos++; 74 tour. Cities.InsertRange(newPos, segment);74 tour.Stops.InsertRange(newPos, segment); 75 75 } 76 76 } -
branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape.VRP/Manipulators/RelocateManipulator.cs
r7159 r9750 29 29 using HeuristicLab.Problems.VehicleRouting.Encodings; 30 30 using HeuristicLab.Problems.VehicleRouting; 31 using HeuristicLab.Problems.VehicleRouting.Interfaces; 31 32 32 33 namespace HeuristicLab.Analysis.FitnessLandscape.VRP { … … 46 47 } 47 48 48 private static bool RouteFeasible(Tour tour, DoubleArray demand, DoubleValue capacity) { 49 double routeLoad = 0; 50 for (int i = 0; i < tour.Cities.Count; i++) { 51 routeLoad += demand[tour.Cities[i]]; 52 } 53 54 return routeLoad <= capacity.Value; 55 } 56 57 public static void Apply(IRandom random, PotvinEncoding individual, DoubleArray demand, DoubleValue capacity, bool allowInfeasible) { 49 public static void Apply(IRandom random, PotvinEncoding individual, IVRPProblemInstance problemInstance, bool allowInfeasible) { 58 50 bool feasible; 59 51 … … 63 55 int cities = individual.Cities; 64 56 int city = 1 + random.Next(cities); 65 Tour originalTour = individual.Tours.Find(t => t. Cities.Contains(city));57 Tour originalTour = individual.Tours.Find(t => t.Stops.Contains(city)); 66 58 //consider creating new route 67 59 individual.Tours.Add(new Tour()); … … 72 64 } 73 65 74 int originalPosition = originalTour. Cities.IndexOf(city);75 originalTour. Cities.RemoveAt(originalPosition);66 int originalPosition = originalTour.Stops.IndexOf(city); 67 originalTour.Stops.RemoveAt(originalPosition); 76 68 77 69 Tour insertionTour; 78 70 int insertionPosition; 79 71 if (position <= cities) { 80 insertionTour = individual.Tours.Find(t => t. Cities.Contains(position));81 insertionPosition = insertionTour. Cities.IndexOf(position) + 1;72 insertionTour = individual.Tours.Find(t => t.Stops.Contains(position)); 73 insertionPosition = insertionTour.Stops.IndexOf(position) + 1; 82 74 } else { 83 75 insertionTour = individual.Tours[position - cities - 1]; … … 86 78 87 79 if (!allowInfeasible && insertionTour != originalTour) { 88 bool originalFeasible = RouteFeasible(insertionTour, demand, capacity);80 bool originalFeasible = problemInstance.TourFeasible(insertionTour, individual); 89 81 90 82 if (originalFeasible) { 91 double routeLoad = 0; 92 for (int i = 0; i < insertionTour.Cities.Count; i++) { 93 routeLoad += demand[insertionTour.Cities[i]]; 83 // try insert into insertionTour 84 insertionTour.Stops.Insert(insertionPosition, city); 85 feasible = problemInstance.TourFeasible(insertionTour, individual); 86 87 if (!feasible) { 88 // when insertionTour is not feasible we undo the change and add to the original tour instead 89 insertionTour.Stops.RemoveAt(insertionPosition); 90 originalTour.Stops.Insert(originalPosition, city); 94 91 } 95 routeLoad += demand[city]; 96 97 feasible = routeLoad <= capacity.Value; 98 } 92 } else feasible = false; 99 93 } 100 101 if (feasible) { 102 insertionTour.Cities.Insert(insertionPosition, city); 103 } else { 104 originalTour.Cities.Insert(originalPosition, city); 105 } 106 107 individual.Tours.RemoveAll(t => t.Cities.Count == 0); 94 individual.Tours.RemoveAll(t => t.Stops.Count == 0); 108 95 } while (!feasible); 109 96 } 110 97 111 98 protected override void Manipulate(IRandom random, PotvinEncoding individual) { 112 BoolValue useDistanceMatrix = UseDistanceMatrixParameter.ActualValue;113 DoubleMatrix coordinates = CoordinatesParameter.ActualValue;114 DistanceMatrix distMatrix = VRPUtilities.GetDistanceMatrix(coordinates, DistanceMatrixParameter, useDistanceMatrix);115 DoubleArray demand = DemandParameter.ActualValue;116 DoubleValue capacity = CapacityParameter.ActualValue;117 118 99 bool allowInfeasible = AllowInfeasibleSolutions.Value.Value; 119 Apply(random, individual, demand, capacity, allowInfeasible);100 Apply(random, individual, ProblemInstance, allowInfeasible); 120 101 } 121 102 } -
branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape.VRP/Manipulators/TwoOptManipulator.cs
r7159 r9750 48 48 49 49 public static void Apply(IRandom random, PotvinEncoding individual) { 50 List<Tour> tours = individual.Tours.FindAll(t => t. Cities.Count >= 4);50 List<Tour> tours = individual.Tours.FindAll(t => t.Stops.Count >= 4); 51 51 52 52 if (tours.Count > 0) { … … 55 55 56 56 int a; 57 if (tour. Cities.Count == 4) {57 if (tour.Stops.Count == 4) { 58 58 a = 0; 59 } else if (tour. Cities.Count == 5) {59 } else if (tour.Stops.Count == 5) { 60 60 int idx = random.Next(4); 61 61 if (idx >= 2) … … 63 63 a = idx; 64 64 } else { 65 a = random.Next(tour. Cities.Count);65 a = random.Next(tour.Stops.Count); 66 66 } 67 67 68 68 int b; 69 69 List<int> indices = new List<int>(); 70 for (int i = 0; i < tour. Cities.Count; i++) {70 for (int i = 0; i < tour.Stops.Count; i++) { 71 71 if (Math.Abs(i - a) > 2) { 72 72 indices.Add(i); … … 83 83 int index = a + 1; 84 84 int count = b - a - 1; 85 List<int> segment = tour. Cities.GetRange(index, count);86 tour. Cities.RemoveRange(index, count);85 List<int> segment = tour.Stops.GetRange(index, count); 86 tour.Stops.RemoveRange(index, count); 87 87 segment.Reverse(); 88 tour. Cities.InsertRange(index, segment);88 tour.Stops.InsertRange(index, segment); 89 89 } 90 90 } -
branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape.VRP/Manipulators/TwoOptStarManipulator.cs
r7159 r9750 29 29 using HeuristicLab.Problems.VehicleRouting.Encodings; 30 30 using HeuristicLab.Problems.VehicleRouting; 31 using HeuristicLab.Problems.VehicleRouting.Interfaces; 31 32 32 33 namespace HeuristicLab.Analysis.FitnessLandscape.VRP { … … 46 47 } 47 48 48 private static bool RouteFeasible(Tour tour, DoubleArray demand, DoubleValue capacity) { 49 double routeLoad = 0; 50 for (int i = 0; i < tour.Cities.Count; i++) { 51 routeLoad += demand[tour.Cities[i]]; 52 } 53 54 return routeLoad <= capacity.Value; 55 } 56 57 public static void Apply(IRandom random, PotvinEncoding individual, DoubleArray demand, DoubleValue capacity, bool allowInfeasible) { 49 public static void Apply(IRandom random, PotvinEncoding individual, IVRPProblemInstance problemInstance, bool allowInfeasible) { 58 50 bool feasible; 59 51 … … 72 64 Tour route2 = individual.Tours[route2Idx]; 73 65 74 int x1 = random.Next(route1. Cities.Count + 1);75 int x2 = random.Next(route2. Cities.Count + 1);66 int x1 = random.Next(route1.Stops.Count + 1); 67 int x2 = random.Next(route2.Stops.Count + 1); 76 68 77 69 if (!allowInfeasible) { 78 bool originalFeasible = 79 RouteFeasible(route1, demand, capacity) && 80 RouteFeasible(route2, demand, capacity); 70 bool originalFeasible = problemInstance.TourFeasible(route1, individual) && 71 problemInstance.TourFeasible(route2, individual); 81 72 82 73 if (originalFeasible) { 83 double routeLoad = 0; 84 for (int i = 0; i < x1; i++) 85 routeLoad += demand[route1.Cities[i]]; 86 for (int i = x2; i < route2.Cities.Count; i++) 87 routeLoad += demand[route2.Cities[i]]; 74 int count = route1.Stops.Count - x1; 75 List<int> segmentX1 = new List<int>(); 76 if (count > 0) { 77 segmentX1 = route1.Stops.GetRange(x1, count); 78 route1.Stops.RemoveRange(x1, count); 79 } 88 80 89 if (routeLoad > capacity.Value) { 90 feasible = false; 81 count = route2.Stops.Count - x2; 82 List<int> segmentX2 = new List<int>(); 83 if (count > 0) { 84 segmentX2 = route2.Stops.GetRange(x2, count); 85 route2.Stops.RemoveRange(x2, count); 86 } 87 88 route1.Stops.AddRange(segmentX2); 89 feasible = problemInstance.TourFeasible(route1, individual); 90 if (feasible) { 91 route2.Stops.AddRange(segmentX1); 92 feasible = problemInstance.TourFeasible(route2, individual); 93 if (!feasible) { 94 // undo all changes 95 route1.Stops.RemoveRange(x1, segmentX2.Count); 96 route1.Stops.AddRange(segmentX1); 97 route2.Stops.RemoveRange(x2, segmentX1.Count); 98 route2.Stops.AddRange(segmentX2); 99 } 91 100 } else { 92 routeLoad = 0;93 for (int i = 0; i < x2; i++)94 routeLoad += demand[route2.Cities[i]];95 for (int i = x1; i < route1.Cities.Count; i++)96 routeLoad += demand[route1.Cities[i]];101 // undo changes 102 route1.Stops.RemoveRange(x1, segmentX2.Count); 103 route1.Stops.AddRange(segmentX1); 104 route2.Stops.AddRange(segmentX2); 105 } 97 106 98 if (routeLoad > capacity.Value) { 99 feasible = false; 100 } 101 } 107 } else { 108 feasible = false; 102 109 } 103 }104 105 if (feasible) {106 int count = route1.Cities.Count - x1;107 List<int> segmentX1 = new List<int>();108 if (count > 0) {109 segmentX1 = route1.Cities.GetRange(x1, count);110 route1.Cities.RemoveRange(x1, count);111 }112 113 count = route2.Cities.Count - x2;114 List<int> segmentX2 = new List<int>();115 if (count > 0) {116 segmentX2 = route2.Cities.GetRange(x2, count);117 route2.Cities.RemoveRange(x2, count);118 }119 120 route1.Cities.AddRange(segmentX2);121 route2.Cities.AddRange(segmentX1);122 110 } 123 111 } while (!feasible); 124 112 125 individual.Tours.RemoveAll(t => t. Cities.Count == 0);113 individual.Tours.RemoveAll(t => t.Stops.Count == 0); 126 114 } 127 115 128 116 protected override void Manipulate(IRandom random, PotvinEncoding individual) { 129 BoolValue useDistanceMatrix = UseDistanceMatrixParameter.ActualValue;130 DoubleMatrix coordinates = CoordinatesParameter.ActualValue;131 DistanceMatrix distMatrix = VRPUtilities.GetDistanceMatrix(coordinates, DistanceMatrixParameter, useDistanceMatrix);132 DoubleArray demand = DemandParameter.ActualValue;133 DoubleValue capacity = CapacityParameter.ActualValue;134 135 117 bool allowInfeasible = AllowInfeasibleSolutions.Value.Value; 136 Apply(random, individual, demand, capacity, allowInfeasible);118 Apply(random, individual, ProblemInstance, allowInfeasible); 137 119 } 138 120 }
Note: See TracChangeset
for help on using the changeset viewer.