Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Potvin/Manipulators/PotvinLocalSearchManipulator.cs @ 4745

Last change on this file since 4745 was 4722, checked in by swagner, 14 years ago

Merged cloning refactoring branch back into trunk (#922)

File size: 5.4 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 System.Collections.Generic;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
30  [Item("PotvinLocalSearchManipulator", "The LSM operator which manipulates a VRP representation.  It is implemented as described in Potvin, J.-Y. and Bengio, S. (1996). The Vehicle Routing Problem with Time Windows - Part II: Genetic Search. INFORMS Journal of Computing, 8:165–172.")]
31  [StorableClass]
32  public sealed class PotvinLocalSearchManipulator : PotvinManipulator {
33    public IValueParameter<IntValue> Iterations {
34      get { return (IValueParameter<IntValue>)Parameters["Iterations"]; }
35    }
36
37    [StorableConstructor]
38    private PotvinLocalSearchManipulator(bool deserializing) : base(deserializing) { }
39    private PotvinLocalSearchManipulator(PotvinLocalSearchManipulator original, Cloner cloner)
40      : base(original, cloner) {
41    }
42    public override IDeepCloneable Clone(Cloner cloner) {
43      return new PotvinLocalSearchManipulator(this, cloner);
44    }
45    public PotvinLocalSearchManipulator()
46      : base() {
47      Parameters.Add(new ValueParameter<IntValue>("Iterations", "The number of max iterations.", new IntValue(100)));
48    }
49
50    private bool FindBetterInsertionPlace(
51      PotvinEncoding individual, int tour, int city, int length,
52      out int insertionTour, out int insertionPlace) {
53      bool insertionFound = false;
54      insertionTour = -1;
55      insertionPlace = 1;
56
57      List<int> toBeDeleted = individual.Tours[tour].Cities.GetRange(city, length);
58      double distance = GetLength(individual.Tours[tour]);
59      individual.Tours[tour].Cities.RemoveRange(city, length);
60      double removalBenefit = distance - GetLength(individual.Tours[tour]);
61
62      int currentTour = 0;
63      while (currentTour < individual.Tours.Count && !insertionFound) {
64        int currentCity = 0;
65        while (currentCity <= individual.Tours[currentTour].Cities.Count && !insertionFound) {
66          distance = GetLength(individual.Tours[currentTour]);
67          individual.Tours[currentTour].Cities.InsertRange(currentCity, toBeDeleted);
68          if (Feasible(individual.Tours[currentTour])) {
69            double lengthIncrease =
70              GetLength(individual.Tours[currentTour]) - distance;
71            if (removalBenefit > lengthIncrease) {
72              insertionTour = currentTour;
73              insertionPlace = currentCity;
74
75              insertionFound = true;
76            }
77          }
78          individual.Tours[currentTour].Cities.RemoveRange(currentCity, length);
79
80          currentCity++;
81        }
82        currentTour++;
83      }
84
85      individual.Tours[tour].Cities.InsertRange(city, toBeDeleted);
86
87      return insertionFound;
88    }
89
90    protected override void Manipulate(IRandom random, PotvinEncoding individual) {
91      //only apply to feasible individuals
92      if (Feasible(individual)) {
93        bool insertionFound;
94        int iterations = 0;
95
96        do {
97          insertionFound = false;
98          int length = 3;
99          while (length > 0 && !insertionFound) {
100            int tour = 0;
101            while (tour < individual.Tours.Count && !insertionFound) {
102              int city = 0;
103              while (city <= individual.Tours[tour].Cities.Count - length && !insertionFound) {
104                int insertionTour, insertionPlace;
105                if (FindBetterInsertionPlace(individual, tour, city, length,
106                 out insertionTour, out insertionPlace)) {
107                  insertionFound = true;
108
109                  List<int> toBeInserted = individual.Tours[tour].Cities.GetRange(city, length);
110
111                  individual.Tours[tour].Cities.RemoveRange(city, length);
112                  individual.Tours[insertionTour].Cities.InsertRange(
113                    insertionPlace,
114                    toBeInserted);
115                }
116                city++;
117              }
118              tour++;
119            }
120            length--;
121          }
122          iterations++;
123        } while (insertionFound &&
124          iterations < Iterations.Value.Value);
125
126        IList<Tour> toBeRemoved = new List<Tour>();
127        foreach (Tour tour in individual.Tours) {
128          if (tour.Cities.Count == 0)
129            toBeRemoved.Add(tour);
130        }
131
132        foreach (Tour tour in toBeRemoved) {
133          individual.Tours.Remove(tour);
134        }
135      }
136    }
137  }
138}
Note: See TracBrowser for help on using the repository browser.