Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RemoveBackwardsCompatibility/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Manipulators/PotvinPairwiseTwoLevelExchangeManipulator.cs @ 14778

Last change on this file since 14778 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 8.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Encodings.PermutationEncoding;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
27using HeuristicLab.Problems.VehicleRouting.Variants;
28using HeuristicLab.Problems.VehicleRouting.Interfaces;
29
30namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
31  [Item("PotvinPairwiseTwoLevelExchangeManipulator", "The 2M operator which manipulates a VRP representation.   It has been adapted to pickup and delivery from 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.  It was adapted to the PDP formulation.")]
32  [StorableClass]
33  public sealed class PotvinPairwiseTwoLevelExchangeManipulator : PotvinManipulator {
34    [StorableConstructor]
35    private PotvinPairwiseTwoLevelExchangeManipulator(bool deserializing) : base(deserializing) { }
36
37    public PotvinPairwiseTwoLevelExchangeManipulator() : base() { }
38
39    public override IDeepCloneable Clone(Cloner cloner) {
40      return new PotvinPairwiseTwoLevelExchangeManipulator(this, cloner);
41    }
42
43    private PotvinPairwiseTwoLevelExchangeManipulator(PotvinPairwiseTwoLevelExchangeManipulator original, Cloner cloner)
44      : base(original, cloner) {
45    }
46
47    private static PotvinEncoding ReplacePair(PotvinEncoding individual, IVRPProblemInstance instance, int replaced, int replacing, bool allowInfeasible) {
48      individual = individual.Clone() as PotvinEncoding;
49      IPickupAndDeliveryProblemInstance pdp = instance as IPickupAndDeliveryProblemInstance;
50
51      int replacedDest = pdp.GetPickupDeliveryLocation(replaced);
52      int replacedSource, replacedTarget;
53      if (pdp.GetDemand(replaced) >= 0) {
54        replacedSource = replaced;
55        replacedTarget = replacedDest;
56      } else {
57        replacedSource = replacedDest;
58        replacedTarget = replaced;
59      }
60      Tour replacedSourceTour = individual.Tours.Find(t => t.Stops.Contains(replacedSource));
61      Tour replacedTargetTour = individual.Tours.Find(t => t.Stops.Contains(replacedTarget));
62
63      int replacingDest = pdp.GetPickupDeliveryLocation(replacing);
64      int replacingSource, replacingTarget;
65      if (pdp.GetDemand(replacing) >= 0) {
66        replacingSource = replacing;
67        replacingTarget = replacingDest;
68      } else {
69        replacingSource = replacingDest;
70        replacingTarget = replacing;
71      }
72      Tour replacingSourceTour = individual.Tours.Find(t => t.Stops.Contains(replacingSource));
73      Tour replacingTargetTour = individual.Tours.Find(t => t.Stops.Contains(replacingTarget));
74
75      replacingSourceTour.Stops.Remove(replacingSource);
76      replacingTargetTour.Stops.Remove(replacingTarget);
77
78      replacedSourceTour.Stops[replacedSourceTour.Stops.IndexOf(replacedSource)] = replacingSource;
79      if (!allowInfeasible && !instance.TourFeasible(replacedSourceTour, individual))
80        return null;
81
82      replacedTargetTour.Stops[replacedTargetTour.Stops.IndexOf(replacedTarget)] = replacingTarget;
83      if (!allowInfeasible && !instance.TourFeasible(replacedTargetTour, individual))
84        return null;
85
86      double bestQuality = double.MaxValue;
87      int bestTour = -1;
88      int bestPositionSource = -1;
89      int bestPositionTarget = -1;
90
91      int routeToAvoid = individual.Tours.IndexOf(replacingSourceTour);
92
93      for (int tourIdx = 0; tourIdx < individual.Tours.Count; tourIdx++) {
94        if (tourIdx != routeToAvoid) {
95          Tour tour = individual.Tours[tourIdx];
96          VRPEvaluation eval = instance.EvaluateTour(tour, individual);
97          individual.InsertPair(tour, replacedSource, replacedTarget, instance);
98          VRPEvaluation evalNew = instance.EvaluateTour(tour, individual);
99
100          double delta = evalNew.Quality - eval.Quality;
101
102          if (delta < bestQuality &&
103              (instance.Feasible(evalNew) || allowInfeasible)) {
104            bestQuality = delta;
105            bestTour = tourIdx;
106            bestPositionSource = tour.Stops.IndexOf(replacedSource);
107            bestPositionTarget = tour.Stops.IndexOf(replacedTarget);
108          }
109
110          tour.Stops.Remove(replacedSource);
111          tour.Stops.Remove(replacedTarget);
112        }
113      }
114
115      if (bestTour != -1) {
116        if (bestPositionTarget < bestPositionSource) {
117          individual.Tours[bestTour].Stops.Insert(bestPositionTarget, replacedTarget);
118          individual.Tours[bestTour].Stops.Insert(bestPositionSource, replacedSource);
119        } else {
120          individual.Tours[bestTour].Stops.Insert(bestPositionSource, replacedSource);
121          individual.Tours[bestTour].Stops.Insert(bestPositionTarget, replacedTarget);
122        }
123
124        return individual;
125      } else {
126        return null;
127      }
128    }
129
130    public static PotvinEncoding ApplyManipulation(IRandom random, PotvinEncoding individual, IPickupAndDeliveryProblemInstance pdp, bool allowInfeasible) {
131      PotvinEncoding result = null;
132     
133      int selectedIndex = SelectRandomTourBiasedByLength(random, individual, pdp);
134      if (selectedIndex >= 0) {
135        bool performed = false;
136        Tour route1 = individual.Tours[selectedIndex];
137
138        if (route1.Stops.Count > 0) {
139          //randomize customer selection
140          Permutation perm = new Permutation(PermutationTypes.Absolute, route1.Stops.Count, random);
141          int customer1Position = 0;
142
143          while (customer1Position < route1.Stops.Count) {
144            performed = false;
145
146            int customer1 = route1.Stops[perm[customer1Position]];
147            int customer2 = -1;
148
149            for (int i = 0; i < individual.Tours.Count; i++) {
150              if (i != selectedIndex) {
151                Tour tour = individual.Tours[i];
152                for (int customer2Position = 0; customer2Position < tour.Stops.Count; customer2Position++) {
153                  customer2 = tour.Stops[customer2Position];
154
155                  if (pdp.GetPickupDeliveryLocation(customer1) != customer2) {
156                    result = ReplacePair(individual, pdp, customer2, customer1, allowInfeasible);
157                    if (result != null) {
158                      individual = result;
159
160                      route1 = individual.Tours[selectedIndex];
161                      performed = true;
162                      break;
163                    }
164                  }
165                }
166              }
167
168              if (performed) {
169                break;
170              }
171            }
172
173            if (!performed)
174              customer1Position++;
175            else
176              break;
177          }
178        }
179      }
180
181      return result;
182    }
183
184    protected override void Manipulate(IRandom random, PotvinEncoding individual) {
185      bool allowInfeasible = AllowInfeasibleSolutions.Value.Value;
186      IPickupAndDeliveryProblemInstance pdp = ProblemInstance as IPickupAndDeliveryProblemInstance;
187
188      if (pdp != null) {
189        PotvinEncoding result = ApplyManipulation(random, individual, pdp, allowInfeasible);
190        if (result != null) {
191          VRPToursParameter.ActualValue = result;
192        }       
193      }
194    }
195  }
196}
Note: See TracBrowser for help on using the repository browser.