Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Alba/Manipulators/LambdaInterchangeManipulator.cs @ 4206

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

Updated naming and description of the VRP operators (#1039)

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