Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Alba/Manipulators/AlbaLambdaInterchangeManipulator.cs @ 4378

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

Worked on VRP operators (WIP) (#1177)

File size: 4.3 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;
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
41    public AlbaLambdaInterchangeManipulator()
42      : base() {
43        Parameters.Add(new ValueParameter<IntValue>("Lambda", "The lambda value.", new IntValue(1)));
44     }
45
46    public static void Apply(AlbaEncoding individual, int tour1Index, int position1, int length1,
47      int tour2Index, int position2, int length2) {
48      List<Tour> tours = individual.GetTours();
49
50      Tour tour1 = tours[tour1Index];
51      int tour1Start = -1;
52      for (int i = 0; i < individual.Length; i++) {
53        if (individual[i] == tour1.Stops[0] - 1) {
54          tour1Start = i;
55          break;
56        }
57      }
58
59      Tour tour2 = tours[tour2Index];
60      int tour2Start = -1;
61      for (int i = 0; i < individual.Length; i++) {
62        if (individual[i] == tour2.Stops[0] - 1) {
63          tour2Start = i;
64          break;
65        }
66      }
67
68      AlbaEncoding original = individual.Clone() as AlbaEncoding;
69      int index = 0;
70
71      int start1 = tour1Start + position1;
72      int end1 = start1 + length1;
73
74      int start2 = tour2Start + position2;
75      int end2 = start2 + length2;
76
77      for (int i = 0; i < original.Length; i++) {
78        if (index == start1) {
79          if (end2 - start2 == 0)
80            index = end1;
81          else
82            index = start2;
83        } else if (index == start2) {
84          if (end1 - start1 == 0)
85            index = end2;
86          else
87            index = start1;
88        } else if (index == end1) {
89          index = end2;
90        } else if (index == end2) {
91          index = end1;
92        }
93
94        individual[i] = original[index];
95
96        index++;
97      }
98    }
99
100    protected override void Manipulate(IRandom rand, AlbaEncoding individual) {
101      List<Tour> tours = individual.GetTours();
102      int lambda = LambdaParameter.Value.Value;
103
104      int route1Index = rand.Next(tours.Count);
105      Tour route1 = tours[route1Index];
106
107      int route2Index = rand.Next(tours.Count - 1);
108      if (route2Index >= route1Index)
109        route2Index += 1;
110      Tour route2 = tours[route2Index];
111
112      int length1 = rand.Next(Math.Min(lambda + 1, route1.Stops.Count + 1));
113      int index1 = rand.Next(route1.Stops.Count - length1 + 1);
114
115      int l2Min = 0;
116      if (length1 == 0)
117        l2Min = 1;
118      int length2 = rand.Next(l2Min, Math.Min(lambda + 1, route2.Stops.Count + 1));
119      int index2 = rand.Next(route2.Stops.Count - length2 + 1);
120
121      Apply(individual, route1Index, index1, length1,
122        route2Index, index2, length2);
123    }
124  }
125}
Note: See TracBrowser for help on using the repository browser.