Changeset 9836 for branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape.VRP/Manipulators/TwoOptManipulator.cs
- Timestamp:
- 08/01/13 13:55:01 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape.VRP/Manipulators/TwoOptManipulator.cs
r9750 r9836 30 30 using HeuristicLab.Problems.VehicleRouting; 31 31 using System; 32 using HeuristicLab.Problems.VehicleRouting.Interfaces; 32 33 33 34 namespace HeuristicLab.Analysis.FitnessLandscape.VRP { … … 47 48 } 48 49 49 public static void Apply(IRandom random, PotvinEncoding individual) { 50 List<Tour> tours = individual.Tours.FindAll(t => t.Stops.Count >= 4); 50 public static void Apply(IRandom random, PotvinEncoding individual, IVRPProblemInstance instance, bool allowInfeasible) { 51 List<Tour> tours = individual.Tours.FindAll(t => t.Stops.Count >= 2); 52 if (tours.Count == 0) return; 51 53 52 if (tours.Count > 0) { 54 bool feasible; 55 int count = 0; 56 57 do { 58 feasible = true; 53 59 int tourIdx = random.Next(tours.Count); 54 60 Tour tour = tours[tourIdx]; 55 61 56 int a; 57 if (tour.Stops.Count == 4) { 58 a = 0; 59 } else if (tour.Stops.Count == 5) { 60 int idx = random.Next(4); 61 if (idx >= 2) 62 idx++; 63 a = idx; 62 int first; 63 int length; 64 if (tour.Stops.Count == 2) { 65 first = 0; 66 length = 2; 64 67 } else { 65 a = random.Next(tour.Stops.Count); 68 first = random.Next(tour.Stops.Count - 1); 69 if (first == tour.Stops.Count - 2) { 70 length = tour.Stops.Count - first; 71 } else { 72 length = random.Next(tour.Stops.Count - first - 1) + 2; 73 } 66 74 } 67 75 68 int b; 69 List<int> indices = new List<int>(); 70 for (int i = 0; i < tour.Stops.Count; i++) { 71 if (Math.Abs(i - a) > 2) { 72 indices.Add(i); 76 var originalFeasible = instance.TourFeasible(tour, individual); 77 78 var segment = tour.Stops.GetRange(first, length); 79 tour.Stops.RemoveRange(first, length); 80 segment.Reverse(); 81 tour.Stops.InsertRange(first, segment); 82 83 if (!allowInfeasible && originalFeasible) { 84 feasible = instance.TourFeasible(tour, individual); 85 if (!feasible) { 86 tour.Stops.RemoveRange(first, length); 87 segment.Reverse(); 88 tour.Stops.InsertRange(first, segment); 73 89 } 74 90 } 75 b = indices[random.Next(indices.Count)];76 91 77 if (b < a) { 78 int tmp = a; 79 a = b; 80 b = tmp; 81 } 82 83 int index = a + 1; 84 int count = b - a - 1; 85 List<int> segment = tour.Stops.GetRange(index, count); 86 tour.Stops.RemoveRange(index, count); 87 segment.Reverse(); 88 tour.Stops.InsertRange(index, segment); 89 } 92 } while(!feasible && count++ < 100); 90 93 } 91 94 92 95 protected override void Manipulate(IRandom random, PotvinEncoding individual) { 93 Apply(random, individual); 96 bool allowInfeasible = AllowInfeasibleSolutions.Value.Value; 97 Apply(random, individual, ProblemInstance, allowInfeasible); 94 98 } 95 99 }
Note: See TracChangeset
for help on using the changeset viewer.