Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Alba/Manipulators/AlbaIntraRouteInversionManipulator.cs @ 7317

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

Merged relevant changes from the trunk into the branch (cloning,...) (#1177)

File size: 3.6 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;
28using System.Collections.Generic;
29using HeuristicLab.Common;
30
31namespace HeuristicLab.Problems.VehicleRouting.Encodings.Alba {
32  [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.")]
33  [StorableClass]
34  public sealed class AlbaIntraRouteInversionManipulator : AlbaManipulator {   
35    [StorableConstructor]
36    private AlbaIntraRouteInversionManipulator(bool deserializing) : base(deserializing) { }
37
38    public AlbaIntraRouteInversionManipulator()
39      : base() {
40     }
41
42    public override IDeepCloneable Clone(Cloner cloner) {
43      return new AlbaIntraRouteInversionManipulator(this, cloner);
44    }
45
46    private AlbaIntraRouteInversionManipulator(AlbaIntraRouteInversionManipulator original, Cloner cloner)
47      : base(original, cloner) {
48    }
49
50    public static void Apply(AlbaEncoding individual, int index1, int index2) {
51      if (index1 != -1 && index2 != -1) {
52        int breakPoint1 = index1 + 1;
53        int breakPoint2 = index2;
54        for (int i = 0; i <= (breakPoint2 - breakPoint1) / 2; i++) {  // invert permutation between breakpoints
55          int temp = individual[breakPoint1 + i];
56          individual[breakPoint1 + i] = individual[breakPoint2 - i];
57          individual[breakPoint2 - i] = temp;
58        }
59      }
60    }
61
62    protected override void Manipulate(IRandom rand, AlbaEncoding individual) {
63      int index1 = -1;
64      int index2 = -1;
65
66      List<Tour> validTours = new List<Tour>();
67      foreach (Tour tour in individual.GetTours()) {
68        if (tour.Stops.Count >= 4)
69          validTours.Add(tour);
70      }
71
72      if (validTours.Count > 0) {
73        Tour chosenTour = validTours[rand.Next(validTours.Count)];
74        int currentTourStart = -1;
75        for (int i = 0; i < individual.Length; i++) {
76          if (individual[i] + 1 == chosenTour.Stops[0]) {
77            currentTourStart = i;
78            break;
79          }
80        }
81
82        int currentTourEnd = currentTourStart;
83        while (currentTourEnd < individual.Length &&
84          individual[currentTourEnd] < ProblemInstance.Cities.Value) {
85          currentTourEnd++;
86        }
87
88        int tourLength = currentTourEnd - currentTourStart;
89        int a = rand.Next(tourLength - 3);
90        index1 = currentTourStart + a;
91        index2 = currentTourStart + rand.Next(a + 2, tourLength - 1);
92      }
93
94      Apply(individual, index1, index2);
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.