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 |
|
---|
22 | using HeuristicLab.Common;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Optimization;
|
---|
25 | using HeuristicLab.Parameters;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
28 | using HeuristicLab.Problems.VehicleRouting.Variants;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
|
---|
31 | [Item("PotvinPDExchangeMoveMaker", "Peforms the exchange move on a given PDP encoding and updates the quality.")]
|
---|
32 | [StorableClass]
|
---|
33 | public class PotvinPDExchangeMoveMaker : PotvinMoveMaker, IPotvinPDExchangeMoveOperator, IMoveMaker {
|
---|
34 | public ILookupParameter<PotvinPDExchangeMove> PDExchangeMoveParameter {
|
---|
35 | get { return (ILookupParameter<PotvinPDExchangeMove>)Parameters["PotvinPDExchangeMove"]; }
|
---|
36 | }
|
---|
37 |
|
---|
38 | public override ILookupParameter VRPMoveParameter {
|
---|
39 | get { return PDExchangeMoveParameter; }
|
---|
40 | }
|
---|
41 |
|
---|
42 | [StorableConstructor]
|
---|
43 | protected PotvinPDExchangeMoveMaker(bool deserializing) : base(deserializing) { }
|
---|
44 |
|
---|
45 | public PotvinPDExchangeMoveMaker()
|
---|
46 | : base() {
|
---|
47 | Parameters.Add(new LookupParameter<PotvinPDExchangeMove>("PotvinPDExchangeMove", "The moves that should be made."));
|
---|
48 | }
|
---|
49 |
|
---|
50 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
51 | return new PotvinPDExchangeMoveMaker(this, cloner);
|
---|
52 | }
|
---|
53 |
|
---|
54 | protected PotvinPDExchangeMoveMaker(PotvinPDExchangeMoveMaker original, Cloner cloner)
|
---|
55 | : base(original, cloner) {
|
---|
56 | }
|
---|
57 |
|
---|
58 | public static void Apply(PotvinEncoding solution, PotvinPDExchangeMove move, IVRPProblemInstance problemInstance) {
|
---|
59 | if (move.Tour >= solution.Tours.Count)
|
---|
60 | solution.Tours.Add(new Tour());
|
---|
61 | Tour tour = solution.Tours[move.Tour];
|
---|
62 |
|
---|
63 | Tour oldTour = solution.Tours.Find(t => t.Stops.Contains(move.City));
|
---|
64 | oldTour.Stops.Remove(move.City);
|
---|
65 |
|
---|
66 | if (problemInstance is IPickupAndDeliveryProblemInstance) {
|
---|
67 | IPickupAndDeliveryProblemInstance pdp = problemInstance as IPickupAndDeliveryProblemInstance;
|
---|
68 |
|
---|
69 | int location = pdp.GetPickupDeliveryLocation(move.City);
|
---|
70 | Tour oldTour2 = solution.Tours.Find(t => t.Stops.Contains(location));
|
---|
71 | oldTour2.Stops.Remove(location);
|
---|
72 |
|
---|
73 | location = pdp.GetPickupDeliveryLocation(move.Replaced);
|
---|
74 | oldTour2 = solution.Tours.Find(t => t.Stops.Contains(location));
|
---|
75 |
|
---|
76 | oldTour2.Stops.Remove(location);
|
---|
77 | tour.Stops.Remove(move.Replaced);
|
---|
78 |
|
---|
79 | solution.InsertPair(tour, move.City, pdp.GetPickupDeliveryLocation(move.City), problemInstance);
|
---|
80 | solution.InsertPair(oldTour, move.Replaced, pdp.GetPickupDeliveryLocation(move.Replaced), problemInstance);
|
---|
81 | } else {
|
---|
82 | tour.Stops.Remove(move.Replaced);
|
---|
83 |
|
---|
84 | int place = solution.FindBestInsertionPlace(tour, move.City);
|
---|
85 | tour.Stops.Insert(place, move.City);
|
---|
86 |
|
---|
87 | place = solution.FindBestInsertionPlace(oldTour, move.Replaced);
|
---|
88 | oldTour.Stops.Insert(place, move.Replaced);
|
---|
89 | }
|
---|
90 |
|
---|
91 | solution.Repair();
|
---|
92 | }
|
---|
93 |
|
---|
94 | protected override void PerformMove() {
|
---|
95 | PotvinPDExchangeMove move = PDExchangeMoveParameter.ActualValue;
|
---|
96 |
|
---|
97 | PotvinEncoding newSolution = move.Individual.Clone() as PotvinEncoding;
|
---|
98 | Apply(newSolution, move, ProblemInstance);
|
---|
99 | VRPToursParameter.ActualValue = newSolution;
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|