Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Alba/Manipulators/AlbaIntraRouteInversionManipulator.cs @ 12012

Last change on this file since 12012 was 12012, checked in by ascheibe, 9 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 3.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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
34    public AlbaIntraRouteInversionManipulator()
35      : base() {
36    }
37
38    public override IDeepCloneable Clone(Cloner cloner) {
39      return new AlbaIntraRouteInversionManipulator(this, cloner);
40    }
41
42    private AlbaIntraRouteInversionManipulator(AlbaIntraRouteInversionManipulator original, Cloner cloner)
43      : base(original, cloner) {
44    }
45
46    public static void Apply(AlbaEncoding individual, int index1, int index2) {
47      if (index1 != -1 && index2 != -1) {
48        int breakPoint1 = index1 + 1;
49        int breakPoint2 = index2;
50        for (int i = 0; i <= (breakPoint2 - breakPoint1) / 2; i++) {  // invert permutation between breakpoints
51          int temp = individual[breakPoint1 + i];
52          individual[breakPoint1 + i] = individual[breakPoint2 - i];
53          individual[breakPoint2 - i] = temp;
54        }
55      }
56    }
57
58    protected override void Manipulate(IRandom rand, AlbaEncoding individual) {
59      int index1 = -1;
60      int index2 = -1;
61
62      List<Tour> validTours = new List<Tour>();
63      foreach (Tour tour in individual.GetTours()) {
64        if (tour.Stops.Count >= 4)
65          validTours.Add(tour);
66      }
67
68      if (validTours.Count > 0) {
69        Tour chosenTour = validTours[rand.Next(validTours.Count)];
70        int currentTourStart = -1;
71        for (int i = 0; i < individual.Length; i++) {
72          if (individual[i] + 1 == chosenTour.Stops[0]) {
73            currentTourStart = i;
74            break;
75          }
76        }
77
78        int currentTourEnd = currentTourStart;
79        while (currentTourEnd < individual.Length &&
80          individual[currentTourEnd] < ProblemInstance.Cities.Value) {
81          currentTourEnd++;
82        }
83
84        int tourLength = currentTourEnd - currentTourStart;
85        int a = rand.Next(tourLength - 3);
86        index1 = currentTourStart + a;
87        index2 = currentTourStart + rand.Next(a + 2, tourLength - 1);
88      }
89
90      Apply(individual, index1, index2);
91    }
92  }
93}
Note: See TracBrowser for help on using the repository browser.