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 |
|
---|
29 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
|
---|
30 | [Item("PotvinInsertionBasedCrossover", "The IBX crossover for VRP representations.")]
|
---|
31 | [StorableClass]
|
---|
32 | public sealed class PotvinInsertionBasedCrossover : PotvinCrossover {
|
---|
33 | [StorableConstructor]
|
---|
34 | private PotvinInsertionBasedCrossover(bool deserializing) : base(deserializing) { }
|
---|
35 | private PotvinInsertionBasedCrossover(PotvinInsertionBasedCrossover original, Cloner cloner)
|
---|
36 | : base(original, cloner) {
|
---|
37 | }
|
---|
38 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
39 | return new PotvinInsertionBasedCrossover(this, cloner);
|
---|
40 | }
|
---|
41 | public PotvinInsertionBasedCrossover()
|
---|
42 | : base() { }
|
---|
43 |
|
---|
44 | protected static int SelectRandomTourBiasedByLength(IRandom random, PotvinEncoding individual) {
|
---|
45 | int tourIndex = -1;
|
---|
46 |
|
---|
47 | double sum = 0.0;
|
---|
48 | double[] probabilities = new double[individual.Tours.Count];
|
---|
49 | for (int i = 0; i < individual.Tours.Count; i++) {
|
---|
50 | probabilities[i] = 1.0 / ((double)individual.Tours[i].Cities.Count / (double)individual.Cities);
|
---|
51 | sum += probabilities[i];
|
---|
52 | }
|
---|
53 |
|
---|
54 | for (int i = 0; i < probabilities.Length; i++)
|
---|
55 | probabilities[i] = probabilities[i] / sum;
|
---|
56 |
|
---|
57 | double rand = random.NextDouble();
|
---|
58 | double cumulatedProbabilities = 0.0;
|
---|
59 | int index = 0;
|
---|
60 | while (tourIndex == -1 && index < probabilities.Length) {
|
---|
61 | if (cumulatedProbabilities <= rand && rand <= cumulatedProbabilities + probabilities[index])
|
---|
62 | tourIndex = index;
|
---|
63 |
|
---|
64 | cumulatedProbabilities += probabilities[index];
|
---|
65 | index++;
|
---|
66 | }
|
---|
67 |
|
---|
68 | return tourIndex;
|
---|
69 | }
|
---|
70 |
|
---|
71 | private double CalculateCentroidDistance(Tour t1, Tour t2, DoubleMatrix coordinates) {
|
---|
72 | double xSum = 0;
|
---|
73 | double ySum = 0;
|
---|
74 | double c1X, c1Y, c2X, c2Y;
|
---|
75 |
|
---|
76 | for (int i = 0; i < t1.Cities.Count; i++) {
|
---|
77 | xSum += coordinates[t1.Cities[i], 0];
|
---|
78 | ySum += coordinates[t1.Cities[i], 0];
|
---|
79 | }
|
---|
80 | c1X = xSum / t1.Cities.Count;
|
---|
81 | c1Y = ySum / t1.Cities.Count;
|
---|
82 |
|
---|
83 | for (int i = 0; i < t2.Cities.Count; i++) {
|
---|
84 | xSum += coordinates[t2.Cities[i], 0];
|
---|
85 | ySum += coordinates[t2.Cities[i], 0];
|
---|
86 | }
|
---|
87 | c2X = xSum / t1.Cities.Count;
|
---|
88 | c2Y = ySum / t1.Cities.Count;
|
---|
89 |
|
---|
90 | return Math.Sqrt(
|
---|
91 | Math.Pow(c1X - c2X, 2) +
|
---|
92 | Math.Pow(c1Y - c2Y, 2));
|
---|
93 | }
|
---|
94 |
|
---|
95 | private double CalculateMeanCentroidDistance(Tour t1, IList<Tour> tours, DoubleMatrix coordinates) {
|
---|
96 | double sum = 0;
|
---|
97 |
|
---|
98 | for (int i = 0; i < tours.Count; i++) {
|
---|
99 | sum += CalculateCentroidDistance(t1, tours[i], coordinates);
|
---|
100 | }
|
---|
101 |
|
---|
102 | return sum / tours.Count;
|
---|
103 | }
|
---|
104 |
|
---|
105 | private int SelectCityBiasedByNeighborDistance(IRandom random, Tour tour, DistanceMatrix distMatrix) {
|
---|
106 | int cityIndex = -1;
|
---|
107 |
|
---|
108 | double sum = 0.0;
|
---|
109 | double[] probabilities = new double[tour.Cities.Count];
|
---|
110 | for (int i = 0; i < tour.Cities.Count; i++) {
|
---|
111 | int next = i + 1;
|
---|
112 | if (next >= tour.Cities.Count)
|
---|
113 | next = 0;
|
---|
114 | else
|
---|
115 | next = tour.Cities[next];
|
---|
116 | double distance = VRPUtilities.GetDistance(
|
---|
117 | tour.Cities[i], next, distMatrix);
|
---|
118 |
|
---|
119 | int prev = i - 1;
|
---|
120 | if (prev < 0)
|
---|
121 | prev = 0;
|
---|
122 | else
|
---|
123 | prev = tour.Cities[prev];
|
---|
124 | distance += VRPUtilities.GetDistance(
|
---|
125 | tour.Cities[i], prev, distMatrix);
|
---|
126 |
|
---|
127 | probabilities[i] = distance;
|
---|
128 | sum += probabilities[i];
|
---|
129 | }
|
---|
130 |
|
---|
131 | for (int i = 0; i < probabilities.Length; i++)
|
---|
132 | probabilities[i] = probabilities[i] / sum;
|
---|
133 |
|
---|
134 | double rand = random.NextDouble();
|
---|
135 | double cumulatedProbabilities = 0.0;
|
---|
136 | int index = 0;
|
---|
137 | while (cityIndex == -1 && index < probabilities.Length) {
|
---|
138 | if (cumulatedProbabilities <= rand && rand <= cumulatedProbabilities + probabilities[index])
|
---|
139 | cityIndex = index;
|
---|
140 |
|
---|
141 | cumulatedProbabilities += probabilities[index];
|
---|
142 | index++;
|
---|
143 | }
|
---|
144 |
|
---|
145 | return cityIndex;
|
---|
146 | }
|
---|
147 |
|
---|
148 | private bool FindRouteInsertionPlace(
|
---|
149 | Tour tour,
|
---|
150 | DoubleArray dueTimeArray,
|
---|
151 | DoubleArray serviceTimeArray, DoubleArray readyTimeArray, DoubleArray demandArray, DoubleValue capacity,
|
---|
152 | DistanceMatrix distMatrix,
|
---|
153 | int city, 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 (!bestFeasible || feasible) {
|
---|
167 | double newLength = tour.GetLength(distMatrix);
|
---|
168 | double detour = newLength - length;
|
---|
169 |
|
---|
170 | if (place <= 0 || (!(bestFeasible && !feasible)) && 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 | protected override PotvinEncoding Crossover(IRandom random, PotvinEncoding parent1, PotvinEncoding parent2) {
|
---|
186 | PotvinEncoding child = new PotvinEncoding();
|
---|
187 | bool success = true;
|
---|
188 |
|
---|
189 | BoolValue useDistanceMatrix = UseDistanceMatrixParameter.ActualValue;
|
---|
190 | DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
|
---|
191 | DistanceMatrix distMatrix = VRPUtilities.GetDistanceMatrix(coordinates, DistanceMatrixParameter, useDistanceMatrix);
|
---|
192 | DoubleArray dueTime = DueTimeParameter.ActualValue;
|
---|
193 | DoubleArray readyTime = ReadyTimeParameter.ActualValue;
|
---|
194 | DoubleArray serviceTime = ServiceTimeParameter.ActualValue;
|
---|
195 | DoubleArray demand = DemandParameter.ActualValue;
|
---|
196 | DoubleValue capacity = CapacityParameter.ActualValue;
|
---|
197 |
|
---|
198 | List<Tour> R1 = new List<Tour>();
|
---|
199 | PotvinEncoding p1Clone = parent1.Clone() as PotvinEncoding;
|
---|
200 |
|
---|
201 | int k = 1;//random.Next(1, Math.Min(10, parent1.Tours.Count + 1));
|
---|
202 | for (int i = 0; i < k; i++) {
|
---|
203 | int index = SelectRandomTourBiasedByLength(random, p1Clone);
|
---|
204 | R1.Add(p1Clone.Tours[index]);
|
---|
205 | p1Clone.Tours.RemoveAt(index);
|
---|
206 | }
|
---|
207 |
|
---|
208 | foreach (Tour r1 in R1) {
|
---|
209 | List<int> R2 = new List<int>();
|
---|
210 |
|
---|
211 | double r = CalculateMeanCentroidDistance(r1, parent2.Tours, coordinates);
|
---|
212 | foreach (Tour tour in parent2.Tours) {
|
---|
213 | if (CalculateCentroidDistance(r1, tour, coordinates) <= r) {
|
---|
214 | R2.AddRange(tour.Cities);
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | Tour childTour = new Tour();
|
---|
219 | childTour.Cities.AddRange(r1.Cities);
|
---|
220 |
|
---|
221 | //DESTROY - remove cities from r1
|
---|
222 | int removed = random.Next(1, r1.Cities.Count + 1);
|
---|
223 | for (int i = 0; i < removed; i++) {
|
---|
224 | childTour.Cities.RemoveAt(SelectCityBiasedByNeighborDistance(random, childTour, distMatrix));
|
---|
225 | }
|
---|
226 |
|
---|
227 | //REPAIR - add cities from R2
|
---|
228 | bool insertSuccess = true;
|
---|
229 | int maxCount = random.Next(1, Math.Min(5, R2.Count));
|
---|
230 | int count = 0;
|
---|
231 |
|
---|
232 | while (count < maxCount && R2.Count != 0) {
|
---|
233 | PotvinEncoding newChild = child.Clone() as PotvinEncoding;
|
---|
234 | newChild.Tours.Add(childTour);
|
---|
235 |
|
---|
236 | int index = random.Next(R2.Count);
|
---|
237 | int city = R2[index];
|
---|
238 | R2.RemoveAt(index);
|
---|
239 |
|
---|
240 | int place = -1;
|
---|
241 | if(FindRouteInsertionPlace(childTour, dueTime, serviceTime, readyTime,
|
---|
242 | demand, capacity, distMatrix, city, out place)) {
|
---|
243 | childTour.Cities.Insert(place, city);
|
---|
244 |
|
---|
245 | if (!Repair(random, child, childTour, distMatrix, dueTime, readyTime, serviceTime, demand, capacity)) {
|
---|
246 | childTour.Cities.RemoveAt(place);
|
---|
247 | insertSuccess = false;
|
---|
248 | } else {
|
---|
249 | count++;
|
---|
250 | }
|
---|
251 | }
|
---|
252 | }
|
---|
253 |
|
---|
254 | child.Tours.Add(childTour);
|
---|
255 | if (!Repair(random, child, childTour, distMatrix, dueTime, readyTime, serviceTime, demand, capacity)) {
|
---|
256 | //success = false;
|
---|
257 | //break;
|
---|
258 | }
|
---|
259 | }
|
---|
260 |
|
---|
261 | if (success) {
|
---|
262 | for (int i = 0; i < p1Clone.Tours.Count; i++) {
|
---|
263 | Tour childTour = p1Clone.Tours[i].Clone() as Tour;
|
---|
264 | child.Tours.Add(childTour);
|
---|
265 | if (!Repair(random, child, childTour, distMatrix, dueTime, readyTime, serviceTime, demand, capacity)) {
|
---|
266 | //success = false;
|
---|
267 | //break;
|
---|
268 | }
|
---|
269 | }
|
---|
270 | }
|
---|
271 |
|
---|
272 | if (success) {
|
---|
273 | //route unrouted customers
|
---|
274 | for (int i = 1; i <= parent1.Cities; i++) {
|
---|
275 | if (FindRoute(child, i) == null)
|
---|
276 | child.Unrouted.Add(i);
|
---|
277 | }
|
---|
278 |
|
---|
279 | if (!RouteUnrouted(child, distMatrix, dueTime, readyTime, serviceTime, demand, capacity)) {
|
---|
280 | success = false;
|
---|
281 | }
|
---|
282 | }
|
---|
283 |
|
---|
284 | if (success)
|
---|
285 | return child;
|
---|
286 | else {
|
---|
287 | /* if (random.NextDouble() < 0.5)
|
---|
288 | return parent1.Clone() as PotvinEncoding;
|
---|
289 | else
|
---|
290 | return parent2.Clone() as PotvinEncoding; */
|
---|
291 | return child;
|
---|
292 | }
|
---|
293 | }
|
---|
294 | }
|
---|
295 | }
|
---|