[6960] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14186] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6960] | 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 |
|
---|
[8053] | 22 | using HeuristicLab.Common;
|
---|
[6960] | 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[8053] | 25 | using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
|
---|
[6960] | 26 | using HeuristicLab.Problems.VehicleRouting.Variants;
|
---|
[10744] | 27 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
[6960] | 28 |
|
---|
| 29 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
|
---|
[8231] | 30 | [Item("PotvinPairwiseOneLevelExchangeMainpulator", "The 1M operator which manipulates a PDP 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.")]
|
---|
[6960] | 31 | [StorableClass]
|
---|
| 32 | public sealed class PotvinPairwiseOneLevelExchangeMainpulator : PotvinManipulator {
|
---|
| 33 | [StorableConstructor]
|
---|
| 34 | private PotvinPairwiseOneLevelExchangeMainpulator(bool deserializing) : base(deserializing) { }
|
---|
| 35 |
|
---|
| 36 | public PotvinPairwiseOneLevelExchangeMainpulator() : base() { }
|
---|
| 37 |
|
---|
| 38 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 39 | return new PotvinPairwiseOneLevelExchangeMainpulator(this, cloner);
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | private PotvinPairwiseOneLevelExchangeMainpulator(PotvinPairwiseOneLevelExchangeMainpulator original, Cloner cloner)
|
---|
| 43 | : base(original, cloner) {
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[10744] | 46 | public static bool PairwiseMove(PotvinEncoding individual, IVRPProblemInstance instance, int city, bool allowInfeasible) {
|
---|
[6960] | 47 | bool success;
|
---|
| 48 |
|
---|
[10744] | 49 | IPickupAndDeliveryProblemInstance pdp = instance as IPickupAndDeliveryProblemInstance;
|
---|
[6960] | 50 |
|
---|
| 51 | if (pdp != null) {
|
---|
| 52 | Tour route1 = individual.Tours.Find(t => t.Stops.Contains(city));
|
---|
| 53 | int i = route1.Stops.IndexOf(city);
|
---|
| 54 |
|
---|
| 55 | int dest = pdp.GetPickupDeliveryLocation(city);
|
---|
| 56 | Tour destRoute = individual.Tours.Find(t => t.Stops.Contains(dest));
|
---|
| 57 | int j = destRoute.Stops.IndexOf(dest);
|
---|
| 58 |
|
---|
| 59 | route1.Stops.Remove(city);
|
---|
| 60 | destRoute.Stops.Remove(dest);
|
---|
| 61 |
|
---|
| 62 | int routeToAvoid = -1;
|
---|
| 63 | if (route1 == destRoute)
|
---|
| 64 | routeToAvoid = individual.Tours.IndexOf(route1);
|
---|
| 65 |
|
---|
| 66 | int source, target;
|
---|
[10744] | 67 | if (instance.GetDemand(city) >= 0) {
|
---|
[6960] | 68 | source = city;
|
---|
| 69 | target = dest;
|
---|
| 70 | } else {
|
---|
| 71 | source = dest;
|
---|
| 72 | target = city;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | double bestQuality = double.MaxValue;
|
---|
| 76 | int bestTour = -1;
|
---|
| 77 | int bestPositionSource = -1;
|
---|
| 78 | int bestPositionTarget = -1;
|
---|
| 79 |
|
---|
| 80 | for (int tourIdx = 0; tourIdx < individual.Tours.Count; tourIdx++) {
|
---|
| 81 | if (tourIdx != routeToAvoid) {
|
---|
| 82 | Tour tour = individual.Tours[tourIdx];
|
---|
[10744] | 83 | VRPEvaluation eval = instance.EvaluateTour(tour, individual);
|
---|
| 84 | individual.InsertPair(tour, source, target, instance);
|
---|
| 85 | VRPEvaluation evalNew = instance.EvaluateTour(tour, individual);
|
---|
[6960] | 86 |
|
---|
| 87 | double delta = evalNew.Quality - eval.Quality;
|
---|
| 88 |
|
---|
| 89 | if (delta < bestQuality &&
|
---|
[10744] | 90 | (instance.Feasible(evalNew) || allowInfeasible)) {
|
---|
[6960] | 91 | bestQuality = delta;
|
---|
| 92 | bestTour = tourIdx;
|
---|
| 93 | bestPositionSource = tour.Stops.IndexOf(source);
|
---|
| 94 | bestPositionTarget = tour.Stops.IndexOf(target);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | tour.Stops.Remove(source);
|
---|
| 98 | tour.Stops.Remove(target);
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | if (bestTour >= 0) {
|
---|
| 103 | if (bestPositionTarget < bestPositionSource) {
|
---|
| 104 | individual.Tours[bestTour].Stops.Insert(bestPositionTarget, target);
|
---|
| 105 | individual.Tours[bestTour].Stops.Insert(bestPositionSource, source);
|
---|
| 106 | } else {
|
---|
| 107 | individual.Tours[bestTour].Stops.Insert(bestPositionSource, source);
|
---|
| 108 | individual.Tours[bestTour].Stops.Insert(bestPositionTarget, target);
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | success = true;
|
---|
| 112 | } else {
|
---|
| 113 | if (j < i) {
|
---|
| 114 | destRoute.Stops.Insert(j, dest);
|
---|
| 115 | route1.Stops.Insert(i, city);
|
---|
| 116 | } else {
|
---|
| 117 | route1.Stops.Insert(i, city);
|
---|
| 118 | destRoute.Stops.Insert(j, dest);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | success = false;
|
---|
| 122 | }
|
---|
| 123 | } else {
|
---|
| 124 | success = false;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | return success;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[10744] | 130 | public static void ApplyManipulation(IRandom random, PotvinEncoding individual, IPickupAndDeliveryProblemInstance pdp, bool allowInfeasible) {
|
---|
| 131 | int selectedIndex = SelectRandomTourBiasedByLength(random, individual, pdp);
|
---|
| 132 | if (selectedIndex >= 0) {
|
---|
| 133 | Tour route1 =
|
---|
| 134 | individual.Tours[selectedIndex];
|
---|
[6960] | 135 |
|
---|
[10744] | 136 | int count = route1.Stops.Count;
|
---|
[6960] | 137 |
|
---|
[10744] | 138 | if (count > 0) {
|
---|
| 139 | int i = random.Next(0, count);
|
---|
| 140 | int city = route1.Stops[i];
|
---|
[6960] | 141 |
|
---|
[10744] | 142 | if (!PairwiseMove(individual, pdp, city, allowInfeasible))
|
---|
| 143 | i++;
|
---|
[6960] | 144 |
|
---|
[10744] | 145 | count = route1.Stops.Count;
|
---|
[6960] | 146 | }
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
[10744] | 149 |
|
---|
| 150 |
|
---|
| 151 | protected override void Manipulate(IRandom random, PotvinEncoding individual) {
|
---|
| 152 | bool allowInfeasible = AllowInfeasibleSolutions.Value.Value;
|
---|
| 153 |
|
---|
| 154 | IPickupAndDeliveryProblemInstance pdp = ProblemInstance as IPickupAndDeliveryProblemInstance;
|
---|
| 155 |
|
---|
| 156 | if (pdp != null) {
|
---|
| 157 | ApplyManipulation(random, individual, pdp, allowInfeasible);
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
[6960] | 160 | }
|
---|
| 161 | }
|
---|