Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.7/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Potvin/Manipulators/PotvinLocalSearchManipulator.cs

Last change on this file was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

File size: 6.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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, 
52      DoubleArray dueTime, DoubleArray readyTime, DoubleArray serviceTime, DoubleArray demand,
53      DoubleValue capacity, DistanceMatrix distMatrix,
54      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].Cities.GetRange(city, length);
61      double distance = individual.Tours[tour].GetLength(distMatrix);
62      individual.Tours[tour].Cities.RemoveRange(city, length);
63      double removalBenefit = distance - individual.Tours[tour].GetLength(distMatrix);
64
65      int currentTour = 0;
66      while (currentTour < individual.Tours.Count && !insertionFound) {
67        int currentCity = 0;
68        while (currentCity <= individual.Tours[currentTour].Cities.Count && !insertionFound) {
69          distance = individual.Tours[currentTour].GetLength(distMatrix);
70          individual.Tours[currentTour].Cities.InsertRange(currentCity, toBeDeleted);
71          if (individual.Tours[currentTour].Feasible(dueTime, serviceTime, readyTime, demand, capacity, distMatrix)) {
72            double lengthIncrease =
73              individual.Tours[currentTour].GetLength(distMatrix) - distance;
74            if (removalBenefit > lengthIncrease) {
75              insertionTour = currentTour;
76              insertionPlace = currentCity;
77
78              insertionFound = true;
79            }
80          }
81          individual.Tours[currentTour].Cities.RemoveRange(currentCity, length);
82
83          currentCity++;
84        }
85        currentTour++;
86      }
87
88      individual.Tours[tour].Cities.InsertRange(city, toBeDeleted); 
89
90      return insertionFound;
91    }
92
93    protected override void Manipulate(IRandom random, PotvinEncoding individual) {
94      BoolValue useDistanceMatrix = UseDistanceMatrixParameter.ActualValue;
95      DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
96      DistanceMatrix distMatrix = VRPUtilities.GetDistanceMatrix(coordinates, DistanceMatrixParameter, useDistanceMatrix);
97      DoubleArray dueTime = DueTimeParameter.ActualValue;
98      DoubleArray readyTime = ReadyTimeParameter.ActualValue;
99      DoubleArray serviceTime = ServiceTimeParameter.ActualValue;
100      DoubleArray demand = DemandParameter.ActualValue;
101      DoubleValue capacity = CapacityParameter.ActualValue;
102     
103      //only apply to feasible individuals
104      bool feasible = true;
105
106      foreach (Tour tour in individual.Tours) {
107        if (!tour.Feasible(dueTime, serviceTime, readyTime, demand, capacity, distMatrix)) {
108          feasible = false;
109          break;
110        }
111      }
112
113      if (feasible) {
114        bool insertionFound;
115        int iterations = 0;
116
117        do {
118          insertionFound = false;
119          int length = 3;
120          while (length > 0 && !insertionFound) {
121            int tour = 0;
122            while (tour < individual.Tours.Count && !insertionFound) {
123              int city = 0;
124              while (city <= individual.Tours[tour].Cities.Count - length && !insertionFound) {
125                int insertionTour, insertionPlace;
126                if (FindBetterInsertionPlace(individual, dueTime, readyTime, serviceTime, demand, capacity, distMatrix,
127                  tour, city, length,
128                 out insertionTour, out insertionPlace)) {
129                  insertionFound = true;
130
131                  List<int> toBeInserted = individual.Tours[tour].Cities.GetRange(city, length);
132
133                  individual.Tours[tour].Cities.RemoveRange(city, length);
134                  individual.Tours[insertionTour].Cities.InsertRange(
135                    insertionPlace,
136                    toBeInserted);
137                }
138                city++;
139              }
140              tour++;
141            }
142            length--;
143          }
144          iterations++;
145        } while (insertionFound &&
146          iterations < Iterations.Value.Value);
147
148        IList<Tour> toBeRemoved = new List<Tour>();
149        foreach (Tour tour in individual.Tours) {
150          if (tour.Cities.Count == 0)
151            toBeRemoved.Add(tour);
152        }
153
154        foreach (Tour tour in toBeRemoved) {
155          individual.Tours.Remove(tour);
156        }
157      }
158    }
159  }
160}
Note: See TracBrowser for help on using the repository browser.