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 |
|
---|
22 | using HeuristicLab.Common;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
25 | using System.Collections.Generic;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using System;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 |
|
---|
30 | namespace 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].Cities.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.Cities.Count; i++) {
|
---|
81 | xSum += coordinates[t1.Cities[i], 0];
|
---|
82 | ySum += coordinates[t1.Cities[i], 1];
|
---|
83 | }
|
---|
84 | c1X = xSum / t1.Cities.Count;
|
---|
85 | c1Y = ySum / t1.Cities.Count;
|
---|
86 |
|
---|
87 | for (int i = 0; i < t2.Cities.Count; i++) {
|
---|
88 | xSum += coordinates[t2.Cities[i], 0];
|
---|
89 | ySum += coordinates[t2.Cities[i], 1];
|
---|
90 | }
|
---|
91 | c2X = xSum / t1.Cities.Count;
|
---|
92 | c2Y = ySum / t1.Cities.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, DistanceMatrix distMatrix) {
|
---|
110 | int cityIndex = -1;
|
---|
111 |
|
---|
112 | double sum = 0.0;
|
---|
113 | double[] probabilities = new double[tour.Cities.Count];
|
---|
114 | for (int i = 0; i < tour.Cities.Count; i++) {
|
---|
115 | int next;
|
---|
116 | if (i + 1 >= tour.Cities.Count)
|
---|
117 | next = 0;
|
---|
118 | else
|
---|
119 | next = tour.Cities[i + 1];
|
---|
120 | double distance = VRPUtilities.GetDistance(
|
---|
121 | tour.Cities[i], next, distMatrix);
|
---|
122 |
|
---|
123 | int prev;
|
---|
124 | if (i - 1 < 0)
|
---|
125 | prev = 0;
|
---|
126 | else
|
---|
127 | prev = tour.Cities[i - 1];
|
---|
128 | distance += VRPUtilities.GetDistance(
|
---|
129 | tour.Cities[i], prev, distMatrix);
|
---|
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 | DoubleArray dueTimeArray,
|
---|
152 | DoubleArray serviceTimeArray, DoubleArray readyTimeArray, DoubleArray demandArray, DoubleValue capacity,
|
---|
153 | DistanceMatrix distMatrix, int city, bool allowInfeasible, out int place) {
|
---|
154 | place = -1;
|
---|
155 | bool bestFeasible = false;
|
---|
156 | double minDetour = 0;
|
---|
157 |
|
---|
158 | for (int i = 0; i <= tour.Cities.Count; i++) {
|
---|
159 | double length = tour.GetLength(distMatrix);
|
---|
160 |
|
---|
161 | tour.Cities.Insert(i, city);
|
---|
162 |
|
---|
163 | bool feasible = tour.Feasible(dueTimeArray, serviceTimeArray, readyTimeArray, demandArray,
|
---|
164 | capacity, distMatrix);
|
---|
165 |
|
---|
166 | if (feasible || allowInfeasible && !bestFeasible) {
|
---|
167 | double newLength = tour.GetLength(distMatrix);
|
---|
168 | double detour = newLength - length;
|
---|
169 |
|
---|
170 | if (place < 0 || detour < minDetour || feasible && !bestFeasible) {
|
---|
171 | place = i;
|
---|
172 | minDetour = detour;
|
---|
173 |
|
---|
174 | if (feasible)
|
---|
175 | bestFeasible = true;
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | tour.Cities.RemoveAt(i);
|
---|
180 | }
|
---|
181 |
|
---|
182 | return place >= 0;
|
---|
183 | }
|
---|
184 |
|
---|
185 | private ICollection<int> GetUnrouted(PotvinEncoding solution, int cities) {
|
---|
186 | HashSet<int> undiscovered = new HashSet<int>();
|
---|
187 | for (int i = 1; i <= cities; i++) {
|
---|
188 | undiscovered.Add(i);
|
---|
189 | }
|
---|
190 |
|
---|
191 | foreach (Tour tour in solution.Tours) {
|
---|
192 | foreach (int city in tour.Cities)
|
---|
193 | undiscovered.Remove(city);
|
---|
194 | }
|
---|
195 |
|
---|
196 | return undiscovered;
|
---|
197 | }
|
---|
198 |
|
---|
199 | protected override PotvinEncoding Crossover(IRandom random, PotvinEncoding parent1, PotvinEncoding parent2) {
|
---|
200 | PotvinEncoding child = new PotvinEncoding();
|
---|
201 |
|
---|
202 | BoolValue useDistanceMatrix = UseDistanceMatrixParameter.ActualValue;
|
---|
203 | DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
|
---|
204 | DistanceMatrix distMatrix = VRPUtilities.GetDistanceMatrix(coordinates, DistanceMatrixParameter, useDistanceMatrix);
|
---|
205 | DoubleArray dueTime = DueTimeParameter.ActualValue;
|
---|
206 | DoubleArray readyTime = ReadyTimeParameter.ActualValue;
|
---|
207 | DoubleArray serviceTime = ServiceTimeParameter.ActualValue;
|
---|
208 | DoubleArray demand = DemandParameter.ActualValue;
|
---|
209 | DoubleValue capacity = CapacityParameter.ActualValue;
|
---|
210 |
|
---|
211 | bool allowInfeasible = AllowInfeasibleSolutions.Value.Value;
|
---|
212 |
|
---|
213 | List<Tour> R1 = new List<Tour>();
|
---|
214 | PotvinEncoding p1Clone = parent1.Clone() as PotvinEncoding;
|
---|
215 |
|
---|
216 | int length = Math.Min(Length.Value.Value, parent1.Tours.Count) + 1;
|
---|
217 | int k = random.Next(1, length);
|
---|
218 | for (int i = 0; i < k; i++) {
|
---|
219 | int index = SelectRandomTourBiasedByLength(random, p1Clone);
|
---|
220 | R1.Add(p1Clone.Tours[index]);
|
---|
221 | p1Clone.Tours.RemoveAt(index);
|
---|
222 | }
|
---|
223 |
|
---|
224 | foreach (Tour r1 in R1) {
|
---|
225 | List<int> R2 = new List<int>();
|
---|
226 |
|
---|
227 | double r = CalculateMeanCentroidDistance(r1, parent2.Tours, coordinates);
|
---|
228 | foreach (Tour tour in parent2.Tours) {
|
---|
229 | if (CalculateCentroidDistance(r1, tour, coordinates) <= r) {
|
---|
230 | R2.AddRange(tour.Cities);
|
---|
231 | }
|
---|
232 | }
|
---|
233 |
|
---|
234 | Tour childTour = new Tour();
|
---|
235 | childTour.Cities.AddRange(r1.Cities);
|
---|
236 |
|
---|
237 | //DESTROY - remove cities from r1
|
---|
238 | int removed = random.Next(1, r1.Cities.Count + 1);
|
---|
239 | for (int i = 0; i < removed; i++) {
|
---|
240 | childTour.Cities.RemoveAt(SelectCityBiasedByNeighborDistance(random, childTour, distMatrix));
|
---|
241 | }
|
---|
242 |
|
---|
243 | //REPAIR - add cities from R2
|
---|
244 | int maxCount = random.Next(1, Math.Min(5, R2.Count));
|
---|
245 | int count = 0;
|
---|
246 |
|
---|
247 | while (count < maxCount && R2.Count != 0) {
|
---|
248 | PotvinEncoding newChild = child.Clone() as PotvinEncoding;
|
---|
249 | newChild.Tours.Add(childTour);
|
---|
250 |
|
---|
251 | int index = random.Next(R2.Count);
|
---|
252 | int city = R2[index];
|
---|
253 | R2.RemoveAt(index);
|
---|
254 |
|
---|
255 | int place = -1;
|
---|
256 | if (FindRouteInsertionPlace(childTour, dueTime, serviceTime, readyTime,
|
---|
257 | demand, capacity, distMatrix, city, allowInfeasible, out place)) {
|
---|
258 | childTour.Cities.Insert(place, city);
|
---|
259 |
|
---|
260 | if (!Repair(random, child, childTour, distMatrix, dueTime, readyTime, serviceTime, demand, capacity, allowInfeasible)) {
|
---|
261 | childTour.Cities.RemoveAt(place);
|
---|
262 | } else {
|
---|
263 | count++;
|
---|
264 | }
|
---|
265 | }
|
---|
266 | }
|
---|
267 |
|
---|
268 | child.Tours.Add(childTour);
|
---|
269 | Repair(random, child, childTour, distMatrix, dueTime, readyTime, serviceTime, demand, capacity, allowInfeasible);
|
---|
270 | }
|
---|
271 |
|
---|
272 | for (int i = 0; i < p1Clone.Tours.Count; i++) {
|
---|
273 | Tour childTour = p1Clone.Tours[i].Clone() as Tour;
|
---|
274 | child.Tours.Add(childTour);
|
---|
275 | Repair(random, child, childTour, distMatrix, dueTime, readyTime, serviceTime, demand, capacity, allowInfeasible);
|
---|
276 | }
|
---|
277 |
|
---|
278 | //route unrouted customers
|
---|
279 | child.Unrouted.AddRange(GetUnrouted(child, Cities));
|
---|
280 | bool success = RouteUnrouted(child, distMatrix, dueTime, readyTime, serviceTime, demand, capacity, allowInfeasible);
|
---|
281 |
|
---|
282 | if (success || allowInfeasible)
|
---|
283 | return child;
|
---|
284 | else {
|
---|
285 | if (random.NextDouble() < 0.5)
|
---|
286 | return parent1.Clone() as PotvinEncoding;
|
---|
287 | else
|
---|
288 | return parent2.Clone() as PotvinEncoding;
|
---|
289 | }
|
---|
290 | }
|
---|
291 | }
|
---|
292 | }
|
---|