Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Alba/Manipulators/AlbaIntraRouteInversionManipulator.cs @ 11624

Last change on this file since 11624 was 4722, checked in by swagner, 14 years ago

Merged cloning refactoring branch back into trunk (#922)

File size: 3.5 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 System.Collections.Generic;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Problems.VehicleRouting.Encodings.Alba {
28  [Item("AlbaIntraRouteInversionManipulator", "An operator which applies the SLS operation to a VRP representation. It is implemented as described in Alba, E. and Dorronsoro, B. (2004). Solving the Vehicle Routing Problem by Using Cellular Genetic Algorithms.")]
29  [StorableClass]
30  public sealed class AlbaIntraRouteInversionManipulator : AlbaManipulator {
31    [StorableConstructor]
32    private AlbaIntraRouteInversionManipulator(bool deserializing) : base(deserializing) { }
33    private AlbaIntraRouteInversionManipulator(AlbaIntraRouteInversionManipulator original, Cloner cloner) : base(original, cloner) { }
34    public AlbaIntraRouteInversionManipulator()
35      : base() {
36    }
37
38    public override IDeepCloneable Clone(Cloner cloner) {
39      return new AlbaIntraRouteInversionManipulator(this, cloner);
40    }
41
42    public static void Apply(AlbaEncoding individual, int index1, int index2) {
43      if (index1 != -1 && index2 != -1) {
44        int breakPoint1 = index1 + 1;
45        int breakPoint2 = index2;
46        for (int i = 0; i <= (breakPoint2 - breakPoint1) / 2; i++) {  // invert permutation between breakpoints
47          int temp = individual[breakPoint1 + i];
48          individual[breakPoint1 + i] = individual[breakPoint2 - i];
49          individual[breakPoint2 - i] = temp;
50        }
51      }
52    }
53
54    protected override void Manipulate(IRandom rand, AlbaEncoding individual) {
55      int index1 = -1;
56      int index2 = -1;
57
58      List<Tour> validTours = new List<Tour>();
59      foreach (Tour tour in individual.GetTours()) {
60        if (tour.Cities.Count >= 4)
61          validTours.Add(tour);
62      }
63
64      if (validTours.Count > 0) {
65        Tour chosenTour = validTours[rand.Next(validTours.Count)];
66        int currentTourStart = -1;
67        for (int i = 0; i < individual.Length; i++) {
68          if (individual[i] + 1 == chosenTour.Cities[0]) {
69            currentTourStart = i;
70            break;
71          }
72        }
73
74        int currentTourEnd = currentTourStart;
75        while (currentTourEnd < individual.Length &&
76          individual[currentTourEnd] < individual.Cities) {
77          currentTourEnd++;
78        }
79
80        int tourLength = currentTourEnd - currentTourStart;
81        int a = rand.Next(tourLength - 3);
82        index1 = currentTourStart + a;
83        index2 = currentTourStart + rand.Next(a + 2, tourLength - 1);
84      }
85
86      Apply(individual, index1, index2);
87    }
88  }
89}
Note: See TracBrowser for help on using the repository browser.