Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Moves/PickupDelivery/PDShift/PotvinPDShiftMoveMaker.cs @ 6797

Last change on this file since 6797 was 6773, checked in by svonolfe, 13 years ago

Added moves for pickup and delivery problems (#1177)

File size: 3.8 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;
32
33namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
34  [Item("PotvinPDShiftMoveMaker", "Peforms the shift move on a given PDP encoding and updates the quality.")]
35  [StorableClass]
36  public class PotvinPDShiftMoveMaker : PotvinMoveMaker, IPotvinPDShiftMoveOperator, IMoveMaker {
37    public ILookupParameter<PotvinPDShiftMove> PDShiftMoveParameter {
38      get { return (ILookupParameter<PotvinPDShiftMove>)Parameters["PotvinPDShiftMove"]; }
39    }
40
41    public override ILookupParameter VRPMoveParameter {
42      get { return PDShiftMoveParameter; }
43    }
44
45    [StorableConstructor]
46    private PotvinPDShiftMoveMaker(bool deserializing) : base(deserializing) { }
47
48    public PotvinPDShiftMoveMaker()
49      : base() {
50        Parameters.Add(new LookupParameter<PotvinPDShiftMove>("PotvinPDShiftMove", "The moves that should be made."));
51     }
52
53    public override IDeepCloneable Clone(Cloner cloner) {
54      return new PotvinPDShiftMoveMaker(this, cloner);
55    }
56
57    protected PotvinPDShiftMoveMaker(PotvinPDShiftMoveMaker original, Cloner cloner)
58      : base(original, cloner) {
59    }
60
61    public static void Apply(PotvinEncoding solution, PotvinPDShiftMove move, IVRPProblemInstance problemInstance) {
62      if (move.Tour >= solution.Tours.Count)
63        solution.Tours.Add(new Tour());
64      Tour tour = solution.Tours[move.Tour];
65
66      Tour oldTour = solution.Tours.Find(t => t.Stops.Contains(move.City));
67      oldTour.Stops.Remove(move.City);
68
69      if (problemInstance is IPickupAndDeliveryProblemInstance) {
70        IntArray pickupLocation =
71          (problemInstance as IPickupAndDeliveryProblemInstance).PickupDeliveryLocation;
72
73        int location = pickupLocation[move.City];
74        Tour oldTour2 = solution.Tours.Find(t => t.Stops.Contains(location));
75        oldTour2.Stops.Remove(location);
76
77        PotvinPDRearrangeMoveMaker.InsertPair(solution, tour, move.City, pickupLocation[move.City], problemInstance);
78      } else {
79        int place = solution.FindBestInsertionPlace(tour, move.City);
80        tour.Stops.Insert(place, move.City);
81      }
82    }
83
84    protected override void PerformMove() {
85      PotvinPDShiftMove move = PDShiftMoveParameter.ActualValue;
86
87      PotvinEncoding newSolution = move.Individual.Clone() as PotvinEncoding;
88      Apply(newSolution, move, ProblemInstance);
89      VRPToursParameter.ActualValue = newSolution;
90
91      //reset move quality
92      VRPEvaluation eval = ProblemInstance.Evaluate(newSolution);
93      MoveQualityParameter.ActualValue.Value = eval.Quality;
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.