1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 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 |
|
---|
22 | using HEAL.Attic;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
26 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
27 | using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
|
---|
28 | using HeuristicLab.Problems.VehicleRouting.Variants;
|
---|
29 |
|
---|
30 | namespace 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 | [StorableType("8E4D8A17-1204-45CC-8ED7-375FCE0355E8")]
|
---|
33 | public sealed class PotvinPairwiseTwoLevelExchangeManipulator : PotvinManipulator {
|
---|
34 | [StorableConstructor]
|
---|
35 | private PotvinPairwiseTwoLevelExchangeManipulator(StorableConstructorFlag _) : base(_) { }
|
---|
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 PotvinEncodedSolution ReplacePair(PotvinEncodedSolution individual, IVRPProblemInstance instance, int replaced, int replacing, bool allowInfeasible) {
|
---|
48 | individual = individual.Clone() as PotvinEncodedSolution;
|
---|
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 | var evalSourceTour = instance.EvaluateTour(replacedSourceTour, individual);
|
---|
80 | if (!allowInfeasible && !evalSourceTour.IsFeasible)
|
---|
81 | return null;
|
---|
82 |
|
---|
83 | var evalTargetTour = instance.EvaluateTour(replacedTargetTour, individual);
|
---|
84 | replacedTargetTour.Stops[replacedTargetTour.Stops.IndexOf(replacedTarget)] = replacingTarget;
|
---|
85 | if (!allowInfeasible && !evalTargetTour.IsFeasible)
|
---|
86 | return null;
|
---|
87 |
|
---|
88 | double bestQuality = double.MaxValue;
|
---|
89 | int bestTour = -1;
|
---|
90 | int bestPositionSource = -1;
|
---|
91 | int bestPositionTarget = -1;
|
---|
92 |
|
---|
93 | int routeToAvoid = individual.Tours.IndexOf(replacingSourceTour);
|
---|
94 |
|
---|
95 | for (int tourIdx = 0; tourIdx < individual.Tours.Count; tourIdx++) {
|
---|
96 | if (tourIdx != routeToAvoid) {
|
---|
97 | Tour tour = individual.Tours[tourIdx];
|
---|
98 | VRPEvaluation eval = instance.EvaluateTour(tour, individual);
|
---|
99 | individual.InsertPair(tour, replacedSource, replacedTarget, instance);
|
---|
100 | VRPEvaluation evalNew = instance.EvaluateTour(tour, individual);
|
---|
101 |
|
---|
102 | double delta = evalNew.Quality - eval.Quality;
|
---|
103 |
|
---|
104 | if (delta < bestQuality &&
|
---|
105 | (evalNew.IsFeasible || allowInfeasible)) {
|
---|
106 | bestQuality = delta;
|
---|
107 | bestTour = tourIdx;
|
---|
108 | bestPositionSource = tour.Stops.IndexOf(replacedSource);
|
---|
109 | bestPositionTarget = tour.Stops.IndexOf(replacedTarget);
|
---|
110 | }
|
---|
111 |
|
---|
112 | tour.Stops.Remove(replacedSource);
|
---|
113 | tour.Stops.Remove(replacedTarget);
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | if (bestTour != -1) {
|
---|
118 | if (bestPositionTarget < bestPositionSource) {
|
---|
119 | individual.Tours[bestTour].Stops.Insert(bestPositionTarget, replacedTarget);
|
---|
120 | individual.Tours[bestTour].Stops.Insert(bestPositionSource, replacedSource);
|
---|
121 | } else {
|
---|
122 | individual.Tours[bestTour].Stops.Insert(bestPositionSource, replacedSource);
|
---|
123 | individual.Tours[bestTour].Stops.Insert(bestPositionTarget, replacedTarget);
|
---|
124 | }
|
---|
125 |
|
---|
126 | return individual;
|
---|
127 | } else {
|
---|
128 | return null;
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | public static PotvinEncodedSolution ApplyManipulation(IRandom random, PotvinEncodedSolution individual, IPickupAndDeliveryProblemInstance pdp, bool allowInfeasible) {
|
---|
133 | PotvinEncodedSolution result = null;
|
---|
134 |
|
---|
135 | int selectedIndex = SelectRandomTourBiasedByLength(random, individual, pdp);
|
---|
136 | if (selectedIndex >= 0) {
|
---|
137 | bool performed = false;
|
---|
138 | Tour route1 = individual.Tours[selectedIndex];
|
---|
139 |
|
---|
140 | if (route1.Stops.Count > 0) {
|
---|
141 | //randomize customer selection
|
---|
142 | Permutation perm = new Permutation(PermutationTypes.Absolute, route1.Stops.Count, random);
|
---|
143 | int customer1Position = 0;
|
---|
144 |
|
---|
145 | while (customer1Position < route1.Stops.Count) {
|
---|
146 | performed = false;
|
---|
147 |
|
---|
148 | int customer1 = route1.Stops[perm[customer1Position]];
|
---|
149 | int customer2 = -1;
|
---|
150 |
|
---|
151 | for (int i = 0; i < individual.Tours.Count; i++) {
|
---|
152 | if (i != selectedIndex) {
|
---|
153 | Tour tour = individual.Tours[i];
|
---|
154 | for (int customer2Position = 0; customer2Position < tour.Stops.Count; customer2Position++) {
|
---|
155 | customer2 = tour.Stops[customer2Position];
|
---|
156 |
|
---|
157 | if (pdp.GetPickupDeliveryLocation(customer1) != customer2) {
|
---|
158 | result = ReplacePair(individual, pdp, customer2, customer1, allowInfeasible);
|
---|
159 | if (result != null) {
|
---|
160 | individual = result;
|
---|
161 |
|
---|
162 | route1 = individual.Tours[selectedIndex];
|
---|
163 | performed = true;
|
---|
164 | break;
|
---|
165 | }
|
---|
166 | }
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | if (performed) {
|
---|
171 | break;
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | if (!performed)
|
---|
176 | customer1Position++;
|
---|
177 | else
|
---|
178 | break;
|
---|
179 | }
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | return result;
|
---|
184 | }
|
---|
185 |
|
---|
186 | protected override void Manipulate(IRandom random, PotvinEncodedSolution individual) {
|
---|
187 | bool allowInfeasible = AllowInfeasibleSolutions.Value.Value;
|
---|
188 | IPickupAndDeliveryProblemInstance pdp = ProblemInstance as IPickupAndDeliveryProblemInstance;
|
---|
189 |
|
---|
190 | if (pdp != null) {
|
---|
191 | PotvinEncodedSolution result = ApplyManipulation(random, individual, pdp, allowInfeasible);
|
---|
192 | if (result != null) {
|
---|
193 | VRPToursParameter.ActualValue = result;
|
---|
194 | }
|
---|
195 | }
|
---|
196 | }
|
---|
197 | }
|
---|
198 | }
|
---|