source: branches/SpectralKernelForGaussianProcesses/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Manipulators/PotvinLocalSearchManipulator.cs @ 10479

Last change on this file since 10479 was 10479, checked in by gkronber, 6 years ago

#2124 merged all changes from trunk to prepare for trunk-reintegration

File size: 5.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Collections.Generic;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Problems.VehicleRouting.Interfaces;
29
30namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
31  [Item("PotvinLocalSearchManipulator", "The LSM operator which manipulates a VRP representation.  It is implemented as described in Potvin, J.-Y. and Bengio, S. (1996). The Vehicle Routing Problem with Time Windows - Part II: Genetic Search. INFORMS Journal of Computing, 8:165–172.")]
32  [StorableClass]
33  public sealed class PotvinLocalSearchManipulator : PotvinManipulator, IVRPLocalSearchManipulator {
34    public IValueParameter<IntValue> Iterations {
35      get { return (IValueParameter<IntValue>)Parameters["Iterations"]; }
36    }
37
38    [StorableConstructor]
39    private PotvinLocalSearchManipulator(bool deserializing) : base(deserializing) { }
40
41    public PotvinLocalSearchManipulator()
42      : base() {
43      Parameters.Add(new ValueParameter<IntValue>("Iterations", "The number of max iterations.", new IntValue(100)));
44    }
45
46    public override IDeepCloneable Clone(Cloner cloner) {
47      return new PotvinLocalSearchManipulator(this, cloner);
48    }
49
50    private PotvinLocalSearchManipulator(PotvinLocalSearchManipulator original, Cloner cloner)
51      : base(original, cloner) {
52    }
53
54    private static bool FindBetterInsertionPlace(
55      PotvinEncoding individual, IVRPProblemInstance instance, int tour, int city, int length,
56      out int insertionTour, out int insertionPlace) {
57      bool insertionFound = false;
58      insertionTour = -1;
59      insertionPlace = 1;
60
61      List<int> toBeDeleted = individual.Tours[tour].Stops.GetRange(city, length);
62      double distance = individual.GetTourLength(individual.Tours[tour]);
63      individual.Tours[tour].Stops.RemoveRange(city, length);
64      double removalBenefit = distance - individual.GetTourLength(individual.Tours[tour]);
65
66      int currentTour = 0;
67      while (currentTour < individual.Tours.Count && !insertionFound) {
68        int currentCity = 0;
69        while (currentCity <= individual.Tours[currentTour].Stops.Count && !insertionFound) {
70          distance = individual.GetTourLength(individual.Tours[currentTour]);
71          individual.Tours[currentTour].Stops.InsertRange(currentCity, toBeDeleted);
72          if (instance.TourFeasible(individual.Tours[currentTour], individual)) {
73            double lengthIncrease =
74              individual.GetTourLength(individual.Tours[currentTour]) - distance;
75            if (removalBenefit > lengthIncrease) {
76              insertionTour = currentTour;
77              insertionPlace = currentCity;
78
79              insertionFound = true;
80            }
81          }
82          individual.Tours[currentTour].Stops.RemoveRange(currentCity, length);
83
84          currentCity++;
85        }
86        currentTour++;
87      }
88
89      individual.Tours[tour].Stops.InsertRange(city, toBeDeleted);
90
91      return insertionFound;
92    }
93
94    public static void ApplyManipulation(IRandom random, PotvinEncoding individual, IVRPProblemInstance instance, int maxIterations) {
95      //only apply to feasible individuals
96      if (instance.Feasible(individual)) {
97        bool insertionFound;
98        int iterations = 0;
99
100        do {
101          insertionFound = false;
102          int length = 3;
103          while (length > 0 && !insertionFound) {
104            int tour = 0;
105            while (tour < individual.Tours.Count && !insertionFound) {
106              int city = 0;
107              while (city <= individual.Tours[tour].Stops.Count - length && !insertionFound) {
108                int insertionTour, insertionPlace;
109                if (FindBetterInsertionPlace(individual, instance, tour, city, length,
110                 out insertionTour, out insertionPlace)) {
111                  insertionFound = true;
112
113                  List<int> toBeInserted = individual.Tours[tour].Stops.GetRange(city, length);
114
115                  individual.Tours[tour].Stops.RemoveRange(city, length);
116                  individual.Tours[insertionTour].Stops.InsertRange(
117                    insertionPlace,
118                    toBeInserted);
119                }
120                city++;
121              }
122              tour++;
123            }
124            length--;
125          }
126          iterations++;
127        } while (insertionFound &&
128          iterations < maxIterations);
129
130        IList<Tour> toBeRemoved = new List<Tour>();
131        foreach (Tour tour in individual.Tours) {
132          if (tour.Stops.Count == 0)
133            toBeRemoved.Add(tour);
134        }
135
136        foreach (Tour tour in toBeRemoved) {
137          individual.Tours.Remove(tour);
138        }
139      }
140    }
141 
142
143    protected override void Manipulate(IRandom random, PotvinEncoding individual) {
144      ApplyManipulation(random, individual, ProblemInstance, Iterations.Value.Value);     
145    }
146  }
147}
Note: See TracBrowser for help on using the repository browser.