Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Manipulators/PotvinLocalSearchManipulator.cs @ 6947

Last change on this file since 6947 was 6838, checked in by svonolfe, 13 years ago

Updated interface of evaluator - added individual (#1177)

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