Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Alba/Manipulators/AlbaLambdaInterchangeManipulator.cs @ 6452

Last change on this file since 6452 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 4.5 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Problems.VehicleRouting.Encodings.Alba {
31  [Item("AlbaLambdaInterchangeManipulator", "An operator which applies the lambda interchange 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.")]
32  [StorableClass]
33  public sealed class AlbaLambdaInterchangeManipulator : AlbaManipulator {
34    public IValueParameter<IntValue> LambdaParameter {
35      get { return (IValueParameter<IntValue>)Parameters["Lambda"]; }
36    }
37
38    [StorableConstructor]
39    private AlbaLambdaInterchangeManipulator(bool deserializing) : base(deserializing) { }
40    private AlbaLambdaInterchangeManipulator(AlbaLambdaInterchangeManipulator original, Cloner cloner) : base(original, cloner) { }
41    public AlbaLambdaInterchangeManipulator()
42      : base() {
43      Parameters.Add(new ValueParameter<IntValue>("Lambda", "The lambda value.", new IntValue(1)));
44    }
45
46    public override IDeepCloneable Clone(Cloner cloner) {
47      return new AlbaLambdaInterchangeManipulator(this, cloner);
48    }
49
50    public static void Apply(AlbaEncoding individual, int tour1Index, int position1, int length1,
51      int tour2Index, int position2, int length2) {
52      List<Tour> tours = individual.GetTours();
53
54      Tour tour1 = tours[tour1Index];
55      int tour1Start = -1;
56      for (int i = 0; i < individual.Length; i++) {
57        if (individual[i] == tour1.Cities[0] - 1) {
58          tour1Start = i;
59          break;
60        }
61      }
62
63      Tour tour2 = tours[tour2Index];
64      int tour2Start = -1;
65      for (int i = 0; i < individual.Length; i++) {
66        if (individual[i] == tour2.Cities[0] - 1) {
67          tour2Start = i;
68          break;
69        }
70      }
71
72      AlbaEncoding original = individual.Clone() as AlbaEncoding;
73      int index = 0;
74
75      int start1 = tour1Start + position1;
76      int end1 = start1 + length1;
77
78      int start2 = tour2Start + position2;
79      int end2 = start2 + length2;
80
81      for (int i = 0; i < original.Length; i++) {
82        if (index == start1) {
83          if (end2 - start2 == 0)
84            index = end1;
85          else
86            index = start2;
87        } else if (index == start2) {
88          if (end1 - start1 == 0)
89            index = end2;
90          else
91            index = start1;
92        } else if (index == end1) {
93          index = end2;
94        } else if (index == end2) {
95          index = end1;
96        }
97
98        individual[i] = original[index];
99
100        index++;
101      }
102    }
103
104    protected override void Manipulate(IRandom rand, AlbaEncoding individual) {
105      List<Tour> tours = individual.GetTours();
106      int lambda = LambdaParameter.Value.Value;
107
108      int route1Index = rand.Next(tours.Count);
109      Tour route1 = tours[route1Index];
110
111      int route2Index = rand.Next(tours.Count - 1);
112      if (route2Index >= route1Index)
113        route2Index += 1;
114      Tour route2 = tours[route2Index];
115
116      int length1 = rand.Next(Math.Min(lambda + 1, route1.Cities.Count + 1));
117      int index1 = rand.Next(route1.Cities.Count - length1 + 1);
118
119      int l2Min = 0;
120      if (length1 == 0)
121        l2Min = 1;
122      int length2 = rand.Next(l2Min, Math.Min(lambda + 1, route2.Cities.Count + 1));
123      int index2 = rand.Next(route2.Cities.Count - length2 + 1);
124
125      Apply(individual, route1Index, index1, length1,
126        route2Index, index2, length2);
127    }
128  }
129}
Note: See TracBrowser for help on using the repository browser.