Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15377 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 4.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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 System.Collections.Generic;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Optimization;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Problems.VehicleRouting.Interfaces;
29using HeuristicLab.Problems.VehicleRouting.Variants;
30
31namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
32  [Item("PotvinPDShiftMoveMaker", "Peforms the shift move on a given PDP encoding and updates the quality.")]
33  [StorableClass]
34  public class PotvinPDShiftMoveMaker : PotvinMoveMaker, IPotvinPDShiftMoveOperator, IMoveMaker {
35    public ILookupParameter<PotvinPDShiftMove> PDShiftMoveParameter {
36      get { return (ILookupParameter<PotvinPDShiftMove>)Parameters["PotvinPDShiftMove"]; }
37    }
38
39    public override ILookupParameter VRPMoveParameter {
40      get { return PDShiftMoveParameter; }
41    }
42
43    [StorableConstructor]
44    protected PotvinPDShiftMoveMaker(bool deserializing) : base(deserializing) { }
45
46    public PotvinPDShiftMoveMaker()
47      : base() {
48      Parameters.Add(new LookupParameter<PotvinPDShiftMove>("PotvinPDShiftMove", "The moves that should be made."));
49    }
50
51    public override IDeepCloneable Clone(Cloner cloner) {
52      return new PotvinPDShiftMoveMaker(this, cloner);
53    }
54
55    protected PotvinPDShiftMoveMaker(PotvinPDShiftMoveMaker original, Cloner cloner)
56      : base(original, cloner) {
57    }
58
59    public static void Apply(PotvinEncoding solution, PotvinPDShiftMove move, IVRPProblemInstance problemInstance) {
60      bool newTour = false;
61
62      if (move.Tour >= solution.Tours.Count) {
63        solution.Tours.Add(new Tour());
64        newTour = true;
65      }
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        int location = pdp.GetPickupDeliveryLocation(move.City);
75        Tour oldTour2 = solution.Tours.Find(t => t.Stops.Contains(location));
76        oldTour2.Stops.Remove(location);
77
78        solution.InsertPair(tour, move.City, location, problemInstance);
79      } else {
80        int place = solution.FindBestInsertionPlace(tour, move.City);
81        tour.Stops.Insert(place, move.City);
82      }
83
84      if (newTour) {
85        List<int> vehicles = new List<int>();
86        for (int i = move.Tour; i < problemInstance.Vehicles.Value; i++) {
87          vehicles.Add(solution.GetVehicleAssignment(i));
88        }
89
90        double bestQuality = double.MaxValue;
91        int bestVehicle = -1;
92
93        int originalVehicle = solution.GetVehicleAssignment(move.Tour);
94        foreach (int vehicle in vehicles) {
95          solution.VehicleAssignment[move.Tour] = vehicle;
96
97          double quality = problemInstance.EvaluateTour(tour, solution).Quality;
98          if (quality < bestQuality) {
99            bestQuality = quality;
100            bestVehicle = vehicle;
101          }
102        }
103
104        solution.VehicleAssignment[move.Tour] = originalVehicle;
105
106        int index = -1;
107        for (int i = move.Tour; i < solution.VehicleAssignment.Length; i++) {
108          if (solution.VehicleAssignment[i] == bestVehicle) {
109            index = i;
110            break;
111          }
112        }
113        solution.VehicleAssignment[index] = originalVehicle;
114        solution.VehicleAssignment[move.Tour] = bestVehicle;
115      }
116
117      solution.Repair();
118    }
119
120    protected override void PerformMove() {
121      PotvinPDShiftMove move = PDShiftMoveParameter.ActualValue;
122
123      PotvinEncoding newSolution = move.Individual.Clone() as PotvinEncoding;
124      Apply(newSolution, move, ProblemInstance);
125      VRPToursParameter.ActualValue = newSolution;
126    }
127  }
128}
Note: See TracBrowser for help on using the repository browser.