[6960] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14185] | 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.Encodings.PermutationEncoding;
|
---|
[14927] | 25 | using HeuristicLab.Persistence;
|
---|
[8053] | 26 | using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
|
---|
[6960] | 27 | using HeuristicLab.Problems.VehicleRouting.Variants;
|
---|
[10460] | 28 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
[6960] | 29 |
|
---|
| 30 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
|
---|
[8231] | 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.")]
|
---|
[14927] | 32 | [StorableType("362c5c70-0f8b-46b0-afce-e55e16e32f38")]
|
---|
[6960] | 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 |
|
---|
[10460] | 47 | private static PotvinEncoding ReplacePair(PotvinEncoding individual, IVRPProblemInstance instance, int replaced, int replacing, bool allowInfeasible) {
|
---|
[6960] | 48 | individual = individual.Clone() as PotvinEncoding;
|
---|
[10460] | 49 | IPickupAndDeliveryProblemInstance pdp = instance as IPickupAndDeliveryProblemInstance;
|
---|
[6960] | 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;
|
---|
[10460] | 79 | if (!allowInfeasible && !instance.TourFeasible(replacedSourceTour, individual))
|
---|
[6967] | 80 | return null;
|
---|
| 81 |
|
---|
[6960] | 82 | replacedTargetTour.Stops[replacedTargetTour.Stops.IndexOf(replacedTarget)] = replacingTarget;
|
---|
[10460] | 83 | if (!allowInfeasible && !instance.TourFeasible(replacedTargetTour, individual))
|
---|
[6967] | 84 | return null;
|
---|
[6960] | 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];
|
---|
[10460] | 96 | VRPEvaluation eval = instance.EvaluateTour(tour, individual);
|
---|
| 97 | individual.InsertPair(tour, replacedSource, replacedTarget, instance);
|
---|
| 98 | VRPEvaluation evalNew = instance.EvaluateTour(tour, individual);
|
---|
[6960] | 99 |
|
---|
| 100 | double delta = evalNew.Quality - eval.Quality;
|
---|
| 101 |
|
---|
| 102 | if (delta < bestQuality &&
|
---|
[10460] | 103 | (instance.Feasible(evalNew) || allowInfeasible)) {
|
---|
[6960] | 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 |
|
---|
[10460] | 130 | public static PotvinEncoding ApplyManipulation(IRandom random, PotvinEncoding individual, IPickupAndDeliveryProblemInstance pdp, bool allowInfeasible) {
|
---|
| 131 | PotvinEncoding result = null;
|
---|
[14927] | 132 |
|
---|
[10460] | 133 | int selectedIndex = SelectRandomTourBiasedByLength(random, individual, pdp);
|
---|
| 134 | if (selectedIndex >= 0) {
|
---|
| 135 | bool performed = false;
|
---|
| 136 | Tour route1 = individual.Tours[selectedIndex];
|
---|
[6960] | 137 |
|
---|
[10460] | 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;
|
---|
[6960] | 142 |
|
---|
[10460] | 143 | while (customer1Position < route1.Stops.Count) {
|
---|
| 144 | performed = false;
|
---|
[6960] | 145 |
|
---|
[10460] | 146 | int customer1 = route1.Stops[perm[customer1Position]];
|
---|
| 147 | int customer2 = -1;
|
---|
[6960] | 148 |
|
---|
[10460] | 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];
|
---|
[6960] | 154 |
|
---|
[10460] | 155 | if (pdp.GetPickupDeliveryLocation(customer1) != customer2) {
|
---|
| 156 | result = ReplacePair(individual, pdp, customer2, customer1, allowInfeasible);
|
---|
| 157 | if (result != null) {
|
---|
| 158 | individual = result;
|
---|
[6960] | 159 |
|
---|
[10460] | 160 | route1 = individual.Tours[selectedIndex];
|
---|
| 161 | performed = true;
|
---|
| 162 | break;
|
---|
[6960] | 163 | }
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 |
|
---|
[10460] | 168 | if (performed) {
|
---|
[6960] | 169 | break;
|
---|
[10460] | 170 | }
|
---|
[6960] | 171 | }
|
---|
[10460] | 172 |
|
---|
| 173 | if (!performed)
|
---|
| 174 | customer1Position++;
|
---|
| 175 | else
|
---|
| 176 | break;
|
---|
[6960] | 177 | }
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
[10460] | 180 |
|
---|
| 181 | return result;
|
---|
[6960] | 182 | }
|
---|
[10460] | 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;
|
---|
[14927] | 192 | }
|
---|
[10460] | 193 | }
|
---|
| 194 | }
|
---|
[6960] | 195 | }
|
---|
| 196 | }
|
---|