Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Crossovers/PotvinInsertionBasedCrossover.cs @ 6608

Last change on this file since 6608 was 6608, checked in by svonolfe, 13 years ago

Added additional operators (#1177)

File size: 9.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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 HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
25using System.Collections.Generic;
26using HeuristicLab.Data;
27using System;
28using HeuristicLab.Parameters;
29
30namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
31  [Item("PotvinInsertionBasedCrossover", "The IBX crossover for VRP representations. It is implemented as described in Berger, J and Solois, M and Begin, R (1998). A hybrid genetic algorithm for the vehicle routing problem with time windows. LNCS 1418. Springer, London 114-127.")]
32  [StorableClass]
33  public sealed class PotvinInsertionBasedCrossover : PotvinCrossover {
34    public IValueParameter<IntValue> Length {
35      get { return (IValueParameter<IntValue>)Parameters["Length"]; }
36    }
37
38    [StorableConstructor]
39    private PotvinInsertionBasedCrossover(bool deserializing) : base(deserializing) { }
40    private PotvinInsertionBasedCrossover(PotvinInsertionBasedCrossover original, Cloner cloner)
41      : base(original, cloner) {
42    }
43    public override IDeepCloneable Clone(Cloner cloner) {
44      return new PotvinInsertionBasedCrossover(this, cloner);
45    }
46    public PotvinInsertionBasedCrossover()
47      : base() {
48      Parameters.Add(new ValueParameter<IntValue>("Length", "The maximum length of the replaced route.", new IntValue(1)));
49    }
50
51    private static int SelectRandomTourBiasedByLength(IRandom random, PotvinEncoding individual) {
52      int tourIndex = -1;
53
54      double sum = 0.0;
55      double[] probabilities = new double[individual.Tours.Count];
56      for (int i = 0; i < individual.Tours.Count; i++) {
57        probabilities[i] = 1.0 / ((double)individual.Tours[i].Stops.Count / (double)individual.Cities);
58        sum += probabilities[i];
59      }
60
61      double rand = random.NextDouble() * sum;
62      double cumulatedProbabilities = 0.0;
63      int index = 0;
64      while (tourIndex == -1 && index < probabilities.Length) {
65        if (cumulatedProbabilities <= rand && rand <= cumulatedProbabilities + probabilities[index])
66          tourIndex = index;
67
68        cumulatedProbabilities += probabilities[index];
69        index++;
70      }
71
72      return tourIndex;
73    }
74
75    private double CalculateCentroidDistance(Tour t1, Tour t2, DoubleMatrix coordinates) {
76      double xSum = 0;
77      double ySum = 0;
78      double c1X, c1Y, c2X, c2Y;
79
80      for (int i = 0; i < t1.Stops.Count; i++) {
81        xSum += coordinates[t1.Stops[i], 0];
82        ySum += coordinates[t1.Stops[i], 1];
83      }
84      c1X = xSum / t1.Stops.Count;
85      c1Y = ySum / t1.Stops.Count;
86
87      for (int i = 0; i < t2.Stops.Count; i++) {
88        xSum += coordinates[t2.Stops[i], 0];
89        ySum += coordinates[t2.Stops[i], 1];
90      }
91      c2X = xSum / t1.Stops.Count;
92      c2Y = ySum / t1.Stops.Count;
93
94      return Math.Sqrt(
95           (c1X - c2X) * (c1X - c2X) +
96           (c1Y - c2Y) * (c1Y - c2Y));
97    }
98
99    private double CalculateMeanCentroidDistance(Tour t1, IList<Tour> tours, DoubleMatrix coordinates) {
100      double sum = 0;
101
102      for (int i = 0; i < tours.Count; i++) {
103        sum += CalculateCentroidDistance(t1, tours[i], coordinates);
104      }
105
106      return sum / tours.Count;
107    }
108
109    private int SelectCityBiasedByNeighborDistance(IRandom random, Tour tour) {
110      int cityIndex = -1;
111
112      double sum = 0.0;
113      double[] probabilities = new double[tour.Stops.Count];
114      for (int i = 0; i < tour.Stops.Count; i++) {
115        int next;
116        if (i + 1 >= tour.Stops.Count)
117          next = 0;
118        else
119          next = tour.Stops[i + 1];
120        double distance = ProblemInstance.GetDistance(
121          tour.Stops[i], next);
122
123        int prev;
124        if (i - 1 < 0)
125          prev = 0;
126        else
127          prev = tour.Stops[i - 1];
128        distance += ProblemInstance.GetDistance(
129          tour.Stops[i], prev);
130
131        probabilities[i] = distance;
132        sum += probabilities[i];
133      }
134
135      double rand = random.NextDouble() * sum;
136      double cumulatedProbabilities = 0.0;
137      int index = 0;
138      while (cityIndex == -1 && index < probabilities.Length) {
139        if (cumulatedProbabilities <= rand && rand <= cumulatedProbabilities + probabilities[index])
140          cityIndex = index;
141
142        cumulatedProbabilities += probabilities[index];
143        index++;
144      }
145
146      return cityIndex;
147    }
148
149    private bool FindRouteInsertionPlace(
150      Tour tour,
151      int city, bool allowInfeasible, out int place) {
152      place = -1;
153      bool bestFeasible = false;
154      double minDetour = 0;
155
156      for (int i = 0; i <= tour.Stops.Count; i++) {
157        double length = tour.GetTourLength(ProblemInstance);
158
159        tour.Stops.Insert(i, city);
160
161        bool feasible = ProblemInstance.Feasible(tour);
162
163        if (feasible || allowInfeasible && !bestFeasible) {
164          double newLength = tour.GetTourLength(ProblemInstance);
165          double detour = newLength - length;
166
167          if (place < 0 || detour < minDetour || feasible && !bestFeasible) {
168            place = i;
169            minDetour = detour;
170
171            if (feasible)
172              bestFeasible = true;
173          }
174        }
175
176        tour.Stops.RemoveAt(i);
177      }
178
179      return place >= 0;
180    }
181
182    private ICollection<int> GetUnrouted(PotvinEncoding solution, int cities) {
183      HashSet<int> undiscovered = new HashSet<int>();
184      for (int i = 1; i <= cities; i++) {
185        undiscovered.Add(i);
186      }
187
188      foreach (Tour tour in solution.Tours) {
189        foreach (int city in tour.Stops)
190          undiscovered.Remove(city);
191      }
192
193      return undiscovered;
194    }
195
196    protected override PotvinEncoding Crossover(IRandom random, PotvinEncoding parent1, PotvinEncoding parent2) {
197      PotvinEncoding child = new PotvinEncoding(ProblemInstance);
198
199      bool allowInfeasible = AllowInfeasibleSolutions.Value.Value;
200
201      List<Tour> R1 = new List<Tour>();
202      PotvinEncoding p1Clone = parent1.Clone() as PotvinEncoding;
203
204      int length = Math.Min(Length.Value.Value, parent1.Tours.Count) + 1;
205      int k = random.Next(1, length);
206      for (int i = 0; i < k; i++) {
207        int index = SelectRandomTourBiasedByLength(random, p1Clone);
208        R1.Add(p1Clone.Tours[index]);
209        p1Clone.Tours.RemoveAt(index);
210      }
211
212      foreach (Tour r1 in R1) {
213        List<int> R2 = new List<int>();
214
215        double r = CalculateMeanCentroidDistance(r1, parent2.Tours, ProblemInstance.Coordinates);
216        foreach (Tour tour in parent2.Tours) {
217          if (CalculateCentroidDistance(r1, tour, ProblemInstance.Coordinates) <= r) {
218            R2.AddRange(tour.Stops);
219          }
220        }
221
222        Tour childTour = new Tour();
223        childTour.Stops.AddRange(r1.Stops);
224
225        //DESTROY - remove cities from r1
226        int removed = random.Next(1, r1.Stops.Count + 1);
227        for (int i = 0; i < removed; i++) {
228          childTour.Stops.RemoveAt(SelectCityBiasedByNeighborDistance(random, childTour));
229        }
230
231        //REPAIR - add cities from R2
232        int maxCount = random.Next(1, Math.Min(5, R2.Count));
233        int count = 0;
234
235        while (count < maxCount && R2.Count != 0) {
236          PotvinEncoding newChild = child.Clone() as PotvinEncoding;
237          newChild.Tours.Add(childTour);
238
239          int index = random.Next(R2.Count);
240          int city = R2[index];
241          R2.RemoveAt(index);
242
243          int place = -1;
244          if (FindRouteInsertionPlace(childTour, city, allowInfeasible, out place)) {
245            childTour.Stops.Insert(place, city);
246
247            if (!Repair(random, child, childTour, ProblemInstance, allowInfeasible)) {
248              childTour.Stops.RemoveAt(place);
249            } else {
250              count++;
251            }
252          }
253        }
254
255        child.Tours.Add(childTour);
256        Repair(random, child, childTour, ProblemInstance, allowInfeasible);
257      }
258
259      for (int i = 0; i < p1Clone.Tours.Count; i++) {
260        Tour childTour = p1Clone.Tours[i].Clone() as Tour;
261        child.Tours.Add(childTour);
262        Repair(random, child, childTour, ProblemInstance, allowInfeasible);
263      }
264
265      //route unrouted customers
266      child.Unrouted.AddRange(GetUnrouted(child, ProblemInstance.Cities.Value));
267      bool success = RouteUnrouted(child, allowInfeasible);
268
269      if (success || allowInfeasible)
270        return child;
271      else {
272        if (random.NextDouble() < 0.5)
273          return parent1.Clone() as PotvinEncoding;
274        else
275          return parent2.Clone() as PotvinEncoding;
276      }
277    }
278  }
279}
Note: See TracBrowser for help on using the repository browser.