Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Moves/PickupDelivery/PDRearrange/PotvinPDRearrangeMoveMaker.cs @ 6856

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

Added support for the multi depot pickup and delivery problem with time windows (#1177)

File size: 5.1 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("PotvinPDRearrangeMoveMaker", "Peforms the rearrange move on a given PDP encoding and updates the quality.")]
35  [StorableClass]
36  public class PotvinPDRearrangeMoveMaker : PotvinMoveMaker, IPotvinPDRearrangeMoveOperator, IMoveMaker {
37    public ILookupParameter<PotvinPDRearrangeMove> PDRearrangeMoveParameter {
38      get { return (ILookupParameter<PotvinPDRearrangeMove>)Parameters["PotvinPDRearrangeMove"]; }
39    }
40
41    public override ILookupParameter VRPMoveParameter {
42      get { return PDRearrangeMoveParameter; }
43    }
44
45    [StorableConstructor]
46    private PotvinPDRearrangeMoveMaker(bool deserializing) : base(deserializing) { }
47
48    public PotvinPDRearrangeMoveMaker()
49      : base() {
50        Parameters.Add(new LookupParameter<PotvinPDRearrangeMove>("PotvinPDRearrangeMove", "The moves that should be made."));
51     }
52
53    public override IDeepCloneable Clone(Cloner cloner) {
54      return new PotvinPDRearrangeMoveMaker(this, cloner);
55    }
56
57    protected PotvinPDRearrangeMoveMaker(PotvinPDRearrangeMoveMaker original, Cloner cloner)
58      : base(original, cloner) {
59    }
60
61    public static void InsertPair(PotvinEncoding solution, Tour tour, int source, int target, IVRPProblemInstance problemInstance, int positionToAvoid = -1, int positionToAvoid2 = -1) {
62      int stops = tour.Stops.Count;
63      VRPEvaluation eval = problemInstance.EvaluateTour(tour, solution);
64      double minCosts = double.MaxValue;
65      int sourceLocation = -1;
66      int targetLocation = -1;
67
68      for (int i = 0; i <= stops; i++) {
69        tour.Stops.Insert(i, source);
70        VRPEvaluation tourEval = problemInstance.EvaluateTour(tour, solution);
71        double sourceCosts = tourEval.Quality - eval.Quality;
72
73        for (int j = i + 1; j <= stops + 1; j++) {
74          if (positionToAvoid != i || positionToAvoid2 != j || stops == 0) {
75            bool feasible;
76            double targetCosts = problemInstance.GetInsertionCosts(tourEval, solution, target, 0, j, out feasible);
77
78            double costs = sourceCosts + targetCosts;
79            if (costs < minCosts) {
80              minCosts = costs;
81              sourceLocation = i;
82              targetLocation = j;
83            }
84          }
85        }
86        tour.Stops.Remove(source);
87      }
88
89      tour.Stops.Insert(sourceLocation, source);
90      tour.Stops.Insert(targetLocation, target);
91    }
92
93    public static void Apply(PotvinEncoding solution, PotvinPDRearrangeMove move, IVRPProblemInstance problemInstance) {
94      Tour tour = solution.Tours[move.Tour];
95      int position = tour.Stops.IndexOf(move.City);
96
97      IPickupAndDeliveryProblemInstance pdp = problemInstance as IPickupAndDeliveryProblemInstance;
98      if (pdp != null) {
99        int location = pdp.GetPickupDeliveryLocation(move.City);
100        Tour tour2 = solution.Tours.Find(t => t.Stops.Contains(location));
101        int position2 = tour2.Stops.IndexOf(location);
102
103        tour.Stops.Remove(move.City);
104        tour2.Stops.Remove(location);
105
106        PotvinPDRearrangeMoveMaker.InsertPair(solution, tour, move.City, location, problemInstance, position, position2);
107      } else {
108        tour.Stops.Remove(move.City);
109        int place = solution.FindBestInsertionPlace(tour, move.City, position);
110        tour.Stops.Insert(place, move.City);
111      }
112    }
113
114    protected override void PerformMove() {
115      PotvinPDRearrangeMove move = PDRearrangeMoveParameter.ActualValue;
116
117      PotvinEncoding newSolution = move.Individual.Clone() as PotvinEncoding;
118      Apply(newSolution, move, ProblemInstance);
119      VRPToursParameter.ActualValue = newSolution;
120
121      //reset move quality
122      VRPEvaluation eval = ProblemInstance.Evaluate(newSolution);
123      MoveQualityParameter.ActualValue.Value = eval.Quality;
124    }
125  }
126}
Note: See TracBrowser for help on using the repository browser.