Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DynamicVehicleRouting/HeuristicLab.PDPSimulation/3.3/Operators/Moves/Exchange/PotvinPDExchangeMoveMaker.cs @ 8670

Last change on this file since 8670 was 8670, checked in by svonolfe, 12 years ago

Added first version of the dynamic vehicle routing addon (#1955)

File size: 5.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Core;
23using HeuristicLab.Data;
24using HeuristicLab.Operators;
25using HeuristicLab.Optimization;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Common;
29using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
30using HeuristicLab.Problems.VehicleRouting.Interfaces;
31using HeuristicLab.Problems.VehicleRouting.Variants;
32using HeuristicLab.Problems.VehicleRouting.Encodings.Potvin;
33using HeuristicLab.Problems.VehicleRouting;
34
35namespace HeuristicLab.PDPSimulation.Operators {
36  [Item("DynPotvinPDExchangeMoveMaker", "Peforms the exchange move on a given PDP encoding and updates the quality.")]
37  [StorableClass]
38  public class DynPotvinPDExchangeMoveMaker : PotvinMoveMaker, IDynPotvinPDExchangeMoveOperator, IMoveMaker {
39    public ILookupParameter<DynPotvinPDExchangeMove> PDExchangeMoveParameter {
40      get { return (ILookupParameter<DynPotvinPDExchangeMove>)Parameters["DynPotvinPDExchangeMove"]; }
41    }
42
43    public override ILookupParameter VRPMoveParameter {
44      get { return PDExchangeMoveParameter; }
45    }
46
47    [StorableConstructor]
48    private DynPotvinPDExchangeMoveMaker(bool deserializing) : base(deserializing) { }
49
50    public DynPotvinPDExchangeMoveMaker()
51      : base() {
52        Parameters.Add(new LookupParameter<DynPotvinPDExchangeMove>("DynPotvinPDExchangeMove", "The moves that should be made."));
53    }
54
55    public override IDeepCloneable Clone(Cloner cloner) {
56      return new DynPotvinPDExchangeMoveMaker(this, cloner);
57    }
58
59    protected DynPotvinPDExchangeMoveMaker(DynPotvinPDExchangeMoveMaker original, Cloner cloner)
60      : base(original, cloner) {
61    }
62
63    public static void Apply(PotvinEncoding solution, DynPotvinPDExchangeMove move, IVRPProblemInstance problemInstance) {
64      if (move.Tour >= solution.Tours.Count)
65        solution.Tours.Add(new Tour());
66      Tour tour = solution.Tours[move.Tour];
67
68      Tour oldTour = solution.Tours.Find(t => t.Stops.Contains(move.City));
69      oldTour.Stops.Remove(move.City);
70
71      if (problemInstance is IPickupAndDeliveryProblemInstance) {
72        IPickupAndDeliveryProblemInstance pdp = problemInstance as IPickupAndDeliveryProblemInstance;
73
74        Tour oldTour2;
75        int location = pdp.GetPickupDeliveryLocation(move.City);
76        if (location > 0 && location != move.City) {
77          oldTour2 = solution.Tours.Find(t => t.Stops.Contains(location));
78          oldTour2.Stops.Remove(location);
79        }
80
81        location = pdp.GetPickupDeliveryLocation(move.Replaced);
82        if (location > 0 && location != move.Replaced) {
83          oldTour2 = solution.Tours.Find(t => t.Stops.Contains(location));
84          oldTour2.Stops.Remove(location);
85        }
86
87        tour.Stops.Remove(move.Replaced);
88
89        if (move.City != pdp.GetPickupDeliveryLocation(move.City))
90          DynPotvinPDRearrangeMoveMaker.InsertPair(solution, tour, move.City, pdp.GetPickupDeliveryLocation(move.City), problemInstance);
91        else {
92          int place = solution.FindBestInsertionPlace(tour, move.City);
93          tour.Stops.Insert(place, move.City);
94        }
95
96        if (move.Replaced != pdp.GetPickupDeliveryLocation(move.Replaced))
97          DynPotvinPDRearrangeMoveMaker.InsertPair(solution, oldTour, move.Replaced, pdp.GetPickupDeliveryLocation(move.Replaced), problemInstance);
98        else {
99          int place = solution.FindBestInsertionPlace(oldTour, move.Replaced);
100          oldTour.Stops.Insert(place, move.Replaced);
101        }
102      } else {
103        tour.Stops.Remove(move.Replaced);
104
105        int place = solution.FindBestInsertionPlace(tour, move.City);
106        tour.Stops.Insert(place, move.City);
107
108        place = solution.FindBestInsertionPlace(oldTour, move.Replaced);
109        oldTour.Stops.Insert(place, move.Replaced);
110      }
111    }
112
113    protected override void PerformMove() {
114      DynPotvinPDExchangeMove move = PDExchangeMoveParameter.ActualValue;
115
116      PotvinEncoding newSolution = move.Individual.Clone() as PotvinEncoding;
117      Apply(newSolution, move, ProblemInstance);
118      newSolution.Repair();
119      VRPToursParameter.ActualValue = newSolution;
120    }
121  }
122}
Note: See TracBrowser for help on using the repository browser.