[4376] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17181] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[4376] | 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;
|
---|
[4376] | 24 | using HeuristicLab.Core;
|
---|
[8053] | 25 | using HeuristicLab.Data;
|
---|
[4376] | 26 | using HeuristicLab.Parameters;
|
---|
[17097] | 27 | using HEAL.Attic;
|
---|
[7855] | 28 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
[4376] | 29 |
|
---|
| 30 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
|
---|
| 31 | [Item("PotvinLocalSearchManipulator", "The LSM operator which manipulates a VRP representation. It is implemented as described in Potvin, J.-Y. and Bengio, S. (1996). The Vehicle Routing Problem with Time Windows - Part II: Genetic Search. INFORMS Journal of Computing, 8:165–172.")]
|
---|
[17097] | 32 | [StorableType("EF16AD46-5C58-4846-95CF-3C3DF78D2F68")]
|
---|
[7855] | 33 | public sealed class PotvinLocalSearchManipulator : PotvinManipulator, IVRPLocalSearchManipulator {
|
---|
[4376] | 34 | public IValueParameter<IntValue> Iterations {
|
---|
| 35 | get { return (IValueParameter<IntValue>)Parameters["Iterations"]; }
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | [StorableConstructor]
|
---|
[17097] | 39 | private PotvinLocalSearchManipulator(StorableConstructorFlag _) : base(_) { }
|
---|
[4376] | 40 |
|
---|
[8053] | 41 | public PotvinLocalSearchManipulator()
|
---|
| 42 | : base() {
|
---|
[4376] | 43 | Parameters.Add(new ValueParameter<IntValue>("Iterations", "The number of max iterations.", new IntValue(100)));
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[4752] | 46 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 47 | return new PotvinLocalSearchManipulator(this, cloner);
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | private PotvinLocalSearchManipulator(PotvinLocalSearchManipulator original, Cloner cloner)
|
---|
| 51 | : base(original, cloner) {
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[10744] | 54 | private static bool FindBetterInsertionPlace(
|
---|
| 55 | PotvinEncoding individual, IVRPProblemInstance instance, int tour, int city, int length,
|
---|
[4376] | 56 | out int insertionTour, out int insertionPlace) {
|
---|
| 57 | bool insertionFound = false;
|
---|
| 58 | insertionTour = -1;
|
---|
| 59 | insertionPlace = 1;
|
---|
| 60 |
|
---|
| 61 | List<int> toBeDeleted = individual.Tours[tour].Stops.GetRange(city, length);
|
---|
| 62 | double distance = individual.GetTourLength(individual.Tours[tour]);
|
---|
| 63 | individual.Tours[tour].Stops.RemoveRange(city, length);
|
---|
| 64 | double removalBenefit = distance - individual.GetTourLength(individual.Tours[tour]);
|
---|
| 65 |
|
---|
| 66 | int currentTour = 0;
|
---|
| 67 | while (currentTour < individual.Tours.Count && !insertionFound) {
|
---|
| 68 | int currentCity = 0;
|
---|
| 69 | while (currentCity <= individual.Tours[currentTour].Stops.Count && !insertionFound) {
|
---|
| 70 | distance = individual.GetTourLength(individual.Tours[currentTour]);
|
---|
| 71 | individual.Tours[currentTour].Stops.InsertRange(currentCity, toBeDeleted);
|
---|
[10744] | 72 | if (instance.TourFeasible(individual.Tours[currentTour], individual)) {
|
---|
[4376] | 73 | double lengthIncrease =
|
---|
| 74 | individual.GetTourLength(individual.Tours[currentTour]) - distance;
|
---|
| 75 | if (removalBenefit > lengthIncrease) {
|
---|
| 76 | insertionTour = currentTour;
|
---|
| 77 | insertionPlace = currentCity;
|
---|
| 78 |
|
---|
| 79 | insertionFound = true;
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 | individual.Tours[currentTour].Stops.RemoveRange(currentCity, length);
|
---|
[8053] | 83 |
|
---|
[4376] | 84 | currentCity++;
|
---|
| 85 | }
|
---|
| 86 | currentTour++;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | individual.Tours[tour].Stops.InsertRange(city, toBeDeleted);
|
---|
| 90 |
|
---|
| 91 | return insertionFound;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[10744] | 94 | public static void ApplyManipulation(IRandom random, PotvinEncoding individual, IVRPProblemInstance instance, int maxIterations) {
|
---|
[4376] | 95 | //only apply to feasible individuals
|
---|
[10744] | 96 | if (instance.Feasible(individual)) {
|
---|
[4376] | 97 | bool insertionFound;
|
---|
| 98 | int iterations = 0;
|
---|
| 99 |
|
---|
| 100 | do {
|
---|
| 101 | insertionFound = false;
|
---|
| 102 | int length = 3;
|
---|
| 103 | while (length > 0 && !insertionFound) {
|
---|
| 104 | int tour = 0;
|
---|
| 105 | while (tour < individual.Tours.Count && !insertionFound) {
|
---|
| 106 | int city = 0;
|
---|
| 107 | while (city <= individual.Tours[tour].Stops.Count - length && !insertionFound) {
|
---|
| 108 | int insertionTour, insertionPlace;
|
---|
[10744] | 109 | if (FindBetterInsertionPlace(individual, instance, tour, city, length,
|
---|
[4376] | 110 | out insertionTour, out insertionPlace)) {
|
---|
| 111 | insertionFound = true;
|
---|
| 112 |
|
---|
| 113 | List<int> toBeInserted = individual.Tours[tour].Stops.GetRange(city, length);
|
---|
| 114 |
|
---|
| 115 | individual.Tours[tour].Stops.RemoveRange(city, length);
|
---|
| 116 | individual.Tours[insertionTour].Stops.InsertRange(
|
---|
| 117 | insertionPlace,
|
---|
[8053] | 118 | toBeInserted);
|
---|
[4376] | 119 | }
|
---|
| 120 | city++;
|
---|
| 121 | }
|
---|
| 122 | tour++;
|
---|
| 123 | }
|
---|
| 124 | length--;
|
---|
| 125 | }
|
---|
| 126 | iterations++;
|
---|
[8053] | 127 | } while (insertionFound &&
|
---|
[10744] | 128 | iterations < maxIterations);
|
---|
[4376] | 129 |
|
---|
| 130 | IList<Tour> toBeRemoved = new List<Tour>();
|
---|
| 131 | foreach (Tour tour in individual.Tours) {
|
---|
| 132 | if (tour.Stops.Count == 0)
|
---|
| 133 | toBeRemoved.Add(tour);
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | foreach (Tour tour in toBeRemoved) {
|
---|
| 137 | individual.Tours.Remove(tour);
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
[10744] | 141 |
|
---|
| 142 |
|
---|
| 143 | protected override void Manipulate(IRandom random, PotvinEncoding individual) {
|
---|
| 144 | ApplyManipulation(random, individual, ProblemInstance, Iterations.Value.Value);
|
---|
| 145 | }
|
---|
[4376] | 146 | }
|
---|
| 147 | }
|
---|