[6773] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17097] | 3 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6773] | 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 |
|
---|
[8053] | 22 | using System.Collections.Generic;
|
---|
| 23 | using HeuristicLab.Common;
|
---|
[6773] | 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Optimization;
|
---|
| 26 | using HeuristicLab.Parameters;
|
---|
[17097] | 27 | using HEAL.Attic;
|
---|
[6773] | 28 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
| 29 | using HeuristicLab.Problems.VehicleRouting.Variants;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
|
---|
| 32 | [Item("PotvinPDShiftMoveMaker", "Peforms the shift move on a given PDP encoding and updates the quality.")]
|
---|
[17097] | 33 | [StorableType("510DE8E9-7634-4DF5-8721-E9797B925A6F")]
|
---|
[6773] | 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]
|
---|
[17097] | 44 | protected PotvinPDShiftMoveMaker(StorableConstructorFlag _) : base(_) { }
|
---|
[6773] | 45 |
|
---|
| 46 | public PotvinPDShiftMoveMaker()
|
---|
| 47 | : base() {
|
---|
[8053] | 48 | Parameters.Add(new LookupParameter<PotvinPDShiftMove>("PotvinPDShiftMove", "The moves that should be made."));
|
---|
| 49 | }
|
---|
[6773] | 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) {
|
---|
[7791] | 60 | bool newTour = false;
|
---|
| 61 |
|
---|
| 62 | if (move.Tour >= solution.Tours.Count) {
|
---|
[6773] | 63 | solution.Tours.Add(new Tour());
|
---|
[7791] | 64 | newTour = true;
|
---|
| 65 | }
|
---|
[6773] | 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) {
|
---|
[6856] | 72 | IPickupAndDeliveryProblemInstance pdp = problemInstance as IPickupAndDeliveryProblemInstance;
|
---|
[8053] | 73 |
|
---|
[6856] | 74 | int location = pdp.GetPickupDeliveryLocation(move.City);
|
---|
[6773] | 75 | Tour oldTour2 = solution.Tours.Find(t => t.Stops.Contains(location));
|
---|
| 76 | oldTour2.Stops.Remove(location);
|
---|
| 77 |
|
---|
[6960] | 78 | solution.InsertPair(tour, move.City, location, problemInstance);
|
---|
[6773] | 79 | } else {
|
---|
| 80 | int place = solution.FindBestInsertionPlace(tour, move.City);
|
---|
| 81 | tour.Stops.Insert(place, move.City);
|
---|
| 82 | }
|
---|
[7080] | 83 |
|
---|
[7791] | 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 |
|
---|
[7080] | 117 | solution.Repair();
|
---|
[6773] | 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 | }
|
---|