Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Prins/Manipulators/PrinsLSManipulator.cs @ 4379

Last change on this file since 4379 was 4379, checked in by svonolfe, 14 years ago

Added remaining encodings (#1177)

File size: 12.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Core;
23using HeuristicLab.Encodings.PermutationEncoding;
24using HeuristicLab.Parameters;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26using HeuristicLab.Data;
27using System.Collections.Generic;
28
29namespace HeuristicLab.Problems.VehicleRouting.Encodings.Prins {
30  [Item("PrinsLSManipulator", "An operator which manipulates a VRP representation by using the Prins local search.  It is implemented as described in Prins, C. (2004). A simple and effective evolutionary algorithm for the vehicle routing problem. Computers & Operations Research, 12:1985-2002.")]
31  [StorableClass]
32  public abstract class PrinsLSManipulator : PrinsManipulator {
33    public IValueParameter<IntValue> Iterations {
34      get { return (IValueParameter<IntValue>)Parameters["Iterations"]; }
35    }
36       
37    [StorableConstructor]
38    protected PrinsLSManipulator(bool deserializing) : base(deserializing) { }
39
40    public PrinsLSManipulator()
41      : base() {
42      Parameters.Add(new ValueParameter<IntValue>("Iterations", "The number of max iterations.", new IntValue(5)));
43    }
44
45    protected double GetQuality(PrinsEncoding individual) {
46      return ProblemInstance.Evaluate(individual);
47    }
48
49    private int FindCity(PrinsEncoding individual, int city) {
50      int index = -1;
51
52      int i = 0;
53      while (i < individual.Length && index == -1) {
54        if (individual[i] == city)
55          index = i;
56       
57        i++;
58      }
59
60      return index;
61    }
62
63    protected const int depot = -1;
64
65    private Tour FindTour(PrinsEncoding individual, int city) {
66      Tour found = null;
67
68      List<Tour> tours = individual.GetTours();
69      int i = 0;
70
71      while (found == null && i < tours.Count) {
72        if (tours[i].Stops.Contains(city))
73          found = tours[i];
74       
75        i++;
76      }
77
78      return found;
79    }
80
81    //inserts u after v in the child
82    private void InsertAfter(int u, int v, PrinsEncoding parent, PrinsEncoding child) {
83      int pi = 0;
84      int ci = 0;
85
86      while (ci != child.Length) {
87        if (parent[pi] != u) {
88          child[ci] = parent[pi];
89          ci++;
90        }
91        if (parent[pi] == v) {
92          child[ci] = u;
93          ci++;
94        }
95
96        pi++;
97      }
98    }
99
100    //inserts (u, x) after v in the child
101    private void InsertAfter(int u, int x, int v, PrinsEncoding parent, PrinsEncoding child) {
102      int pi = 0;
103      int ci = 0;
104
105      while (ci != child.Length) {
106        if (parent[pi] != u && parent[pi] != x) {
107          child[ci] = parent[pi];
108          ci++;
109        }
110        if (parent[pi] == v) {
111          child[ci] = u;
112          ci++;
113
114          child[ci] = x;
115          ci++;
116        }
117
118        pi++;
119      }
120    }
121
122    //inserts u before v in the child
123    private void InsertBefore(int u, int v, PrinsEncoding parent, PrinsEncoding child) {
124      int pi = 0;
125      int ci = 0;
126
127      while (ci != child.Length) {
128        if (parent[pi] == v) {
129          child[ci] = u;
130          ci++;
131        }
132        if (parent[pi] != u) {
133          child[ci] = parent[pi];
134          ci++;
135        }
136
137        pi++;
138      }
139    }
140
141    //inserts (u, x) before v in the child
142    private void InsertBefore(int u, int x, int v, PrinsEncoding parent, PrinsEncoding child) {
143      int pi = 0;
144      int ci = 0;
145
146      while (ci != child.Length) {
147        if (parent[pi] == v) {
148          child[ci] = u;
149          ci++;
150
151          child[ci] = x;
152          ci++;
153        }
154        if (parent[pi] != u && parent[pi] != x) {
155          child[ci] = parent[pi];
156          ci++;
157        }
158
159        pi++;
160      }
161    }
162
163    //swaps u and v
164    private void Swap(int u, int v, PrinsEncoding parent, PrinsEncoding child) {
165      for (int i = 0; i < child.Length; i++) {
166        if (parent[i] == u)
167          child[i] = v;
168        else if (parent[i] == v)
169          child[i] = u;
170        else
171          child[i] = parent[i];
172      }
173    }
174
175    //swaps (u, x) and v
176    private void Swap(int u, int x, int v, PrinsEncoding parent, PrinsEncoding child) {
177      int childIndex = 0;
178      int parentIndex = 0;
179
180      while(childIndex < child.Length) {
181        if (parent[parentIndex] == u) {
182          child[childIndex] = v;
183          parentIndex++;
184        } else if (parent[parentIndex] == v) {
185          child[childIndex] = u;
186          childIndex++;
187          child[childIndex] = x;
188        } else {
189          child[childIndex] = parent[parentIndex];
190        }
191
192        childIndex++;
193        parentIndex++;
194      }
195    }
196
197    //swaps (u, x) and (v, y)
198    private void Swap(int u, int x, int v, int y, PrinsEncoding parent, PrinsEncoding child) {
199      int i = 0;
200
201      while (i < child.Length) {
202        if (child[i] == u) {
203          child[i] = v;
204          i++;
205          child[i] = y;
206        } else if (child[i] == v) {
207          child[i] = u;
208          i++;
209          child[i] = x;
210        } else {
211          child[i] = parent[i];
212        }
213
214        i++;
215      }
216    }
217
218    //swaps (u, x) and (v, y) by (u, y) and (x, v)
219    private void Swap2(int u, int x, int v, int y, PrinsEncoding parent, PrinsEncoding child) {
220      int i = 0;
221
222      while (i < child.Length) {
223        if(parent[i] == x) {
224          child[i] = y;
225        } else if(parent[i] == v) {
226          child[i] = x;
227        } else if(parent[i] == y) {
228          child[i] = v;
229        } else {
230          child[i] = parent[i];
231        }
232
233        i++;
234      }
235    }
236
237    private void M1(PrinsEncoding parent, PrinsEncoding child, int u, int v) {
238      if (u != depot) {
239        if (v == depot) {
240          Tour tour = FindTour(child, u + 1);
241          v = tour.Stops[0] - 1;
242          InsertBefore(u, v, parent, child);
243        } else {
244          InsertAfter(u, v, parent, child);
245        }
246      }
247    }
248
249    private void M2(PrinsEncoding parent, PrinsEncoding child, int u, int v) {
250      if (u != depot) {
251        Tour tour = FindTour(child, u + 1);
252        int iu = tour.Stops.IndexOf(u + 1);
253        if (iu < tour.Stops.Count - 1) {
254          int x = tour.Stops[iu + 1] - 1;
255
256          if (v == depot) {
257            tour = FindTour(child, u + 1);
258            v = tour.Stops[0] - 1;
259            InsertBefore(u, x, v, parent, child);
260          } else {
261            InsertAfter(u, x, v, parent, child);
262          }
263        }
264      }
265    }
266
267    private void M3(PrinsEncoding parent, PrinsEncoding child, int u, int v) {
268      if (u != depot) {
269        Tour tour = FindTour(child, u + 1);
270        int iu = tour.Stops.IndexOf(u + 1);
271        if (iu < tour.Stops.Count - 1) {
272          int x = tour.Stops[iu + 1] - 1;
273
274          if (v == depot) {
275            tour = FindTour(child, u + 1);
276            v = tour.Stops[0] - 1;
277            InsertBefore(x, u, v, parent, child);
278          } else {
279            InsertAfter(x, u, v, parent, child);
280          }
281        }
282      }
283    }
284
285    private void M4(PrinsEncoding parent, PrinsEncoding child, int u, int v) {
286      if (u != depot && v != depot) {
287        Swap(u, v, parent, child);
288      }
289    }
290
291    private void M5(PrinsEncoding parent, PrinsEncoding child, int u, int v) {
292      if (u != depot && v != depot) {
293        Tour tour = FindTour(child, u + 1);
294        int iu = tour.Stops.IndexOf(u + 1);
295        if (iu < tour.Stops.Count - 1) {
296          int x = tour.Stops[iu + 1] - 1;
297
298          if(x != v)
299            Swap(u, x, v, parent, child);
300        }
301      }
302    }
303
304    private void M6(PrinsEncoding parent, PrinsEncoding child, int u, int v) {
305      if (u != depot && v != depot) {
306        Tour tour = FindTour(child, u + 1);
307        int iu = tour.Stops.IndexOf(u + 1);
308        if (iu < tour.Stops.Count - 1) {
309          int x = tour.Stops[iu + 1] - 1;
310
311          tour = FindTour(child, v + 1);
312          int iv = tour.Stops.IndexOf(v + 1);
313          if (iv < tour.Stops.Count - 1) {
314            int y = tour.Stops[iv + 1] - 1;
315
316            if (x != v && y != u)
317              Swap(u, x, v, y, parent, child);
318          }
319        }
320      }
321    }
322
323    private void M7(PrinsEncoding parent, PrinsEncoding child, int u, int v) {
324      if (u != depot && v != depot) {
325        Tour tu = FindTour(child, u + 1);
326        Tour tv = FindTour(child, v + 1);
327
328        if (tu.Stops[0] == tv.Stops[0]) {
329          int iu = tu.Stops.IndexOf(u + 1);
330          if (iu < tu.Stops.Count - 1) {
331            int x = tu.Stops[iu + 1] - 1;
332
333            int iv = tv.Stops.IndexOf(v + 1);
334            if (iv < tv.Stops.Count - 1) {
335              int y = tv.Stops[iv + 1] - 1;
336
337              if (x != v && y != u)
338                Swap(x, v, parent, child);
339             }
340           }
341        }
342      }
343    }
344
345    private void M8(PrinsEncoding parent, PrinsEncoding child, int u, int v) {
346      if (u != depot && v != depot) {
347        Tour tu = FindTour(child, u + 1);
348        Tour tv = FindTour(child, v + 1);
349
350        if (tu.Stops[0] != tv.Stops[0]) {
351          int iu = tu.Stops.IndexOf(u + 1);
352          if (iu < tu.Stops.Count - 1) {
353            int x = tu.Stops[iu + 1] - 1;
354
355            int iv = tv.Stops.IndexOf(v + 1);
356            if (iv < tv.Stops.Count - 1) {
357              int y = tv.Stops[iv + 1] - 1;
358
359              if (x != v && y != u)
360                Swap(x, v, parent, child);
361            }
362          }
363        }
364      }
365    }
366
367    private void M9(PrinsEncoding parent, PrinsEncoding child, int u, int v) {
368      if (u != depot && v != depot) {
369        Tour tu = FindTour(child, u + 1);
370        Tour tv = FindTour(child, v + 1);
371
372        if (tu.Stops[0] != tv.Stops[0]) {
373          int iu = tu.Stops.IndexOf(u + 1);
374          if (iu < tu.Stops.Count - 1) {
375            int x = tu.Stops[iu + 1] - 1;
376
377            int iv = tv.Stops.IndexOf(v + 1);
378            if (iv < tv.Stops.Count - 1) {
379              int y = tv.Stops[iv + 1] - 1;
380
381              if (x != v && y != u)
382                Swap2(u, x, v, y, parent, child);
383            }
384          }
385        }
386      }
387    }
388
389    protected PrinsEncoding Manipulate(PrinsEncoding individual,
390      double originalQuality, int u, int v) {
391      PrinsEncoding child = null;
392      bool improvement = false;
393
394      if (u != v) {
395        child = individual.Clone() as PrinsEncoding;
396        M1(individual, child, u, v);
397        improvement = GetQuality(child) < originalQuality;
398
399        if (!improvement) {
400          child = individual.Clone() as PrinsEncoding;
401          M2(individual, child, u, v);
402          improvement = GetQuality(child) < originalQuality;
403        }
404
405        if (!improvement) {
406          child = individual.Clone() as PrinsEncoding;
407          M3(individual, child, u, v);
408          improvement = GetQuality(child) < originalQuality;
409        }
410
411        if (!improvement) {
412          child = individual.Clone() as PrinsEncoding;
413          M4(individual, child, u, v);
414          improvement = GetQuality(child) < originalQuality;
415        }
416
417        if (!improvement) {
418          child = individual.Clone() as PrinsEncoding;
419          M5(individual, child, u, v);
420          improvement = GetQuality(child) < originalQuality;
421        }
422
423        if (!improvement) {
424          child = individual.Clone() as PrinsEncoding;
425          M6(individual, child, u, v);
426          improvement = GetQuality(child) < originalQuality;
427        }
428
429        if (!improvement) {
430          child = individual.Clone() as PrinsEncoding;
431          M7(individual, child, u, v);
432          improvement = GetQuality(child) < originalQuality;
433        }
434
435        if (!improvement) {
436          child = individual.Clone() as PrinsEncoding;
437          M8(individual, child, u, v);
438          improvement = GetQuality(child) < originalQuality;
439        }
440
441        if (!improvement) {
442          child = individual.Clone() as PrinsEncoding;
443          M9(individual, child, u, v);
444          improvement = GetQuality(child) < originalQuality;
445        }
446      }
447
448      if (improvement)
449        return child;
450      else
451        return null;
452    }
453  }
454}
Note: See TracBrowser for help on using the repository browser.