[4376] | 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 |
|
---|
| 22 | using HeuristicLab.Core;
|
---|
| 23 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
| 24 | using HeuristicLab.Parameters;
|
---|
| 25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Optimization;
|
---|
| 28 | using System.Collections.Generic;
|
---|
| 29 | using HeuristicLab.Problems.VehicleRouting.Encodings.General;
|
---|
| 30 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
[4752] | 31 | using HeuristicLab.Common;
|
---|
[4376] | 32 |
|
---|
| 33 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
|
---|
| 34 | [Item("PotvinCrossover", "A VRP crossover operation.")]
|
---|
| 35 | [StorableClass]
|
---|
| 36 | public abstract class PotvinCrossover : VRPCrossover, IStochasticOperator, IPotvinOperator {
|
---|
| 37 | public ILookupParameter<IRandom> RandomParameter {
|
---|
| 38 | get { return (LookupParameter<IRandom>)Parameters["Random"]; }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[6607] | 41 | public IValueParameter<BoolValue> AllowInfeasibleSolutions {
|
---|
| 42 | get { return (IValueParameter<BoolValue>)Parameters["AllowInfeasibleSolutions"]; }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[4376] | 45 | [StorableConstructor]
|
---|
| 46 | protected PotvinCrossover(bool deserializing) : base(deserializing) { }
|
---|
| 47 |
|
---|
| 48 | public PotvinCrossover() {
|
---|
| 49 | Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic manipulation operators."));
|
---|
[6607] | 50 | Parameters.Add(new ValueParameter<BoolValue>("AllowInfeasibleSolutions", "Indicates if infeasible solutions should be allowed.", new BoolValue(false)));
|
---|
[4376] | 51 | }
|
---|
| 52 |
|
---|
[4752] | 53 | protected PotvinCrossover(PotvinCrossover original, Cloner cloner)
|
---|
| 54 | : base(original, cloner) {
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[4376] | 57 | protected abstract PotvinEncoding Crossover(IRandom random, PotvinEncoding parent1, PotvinEncoding parent2);
|
---|
| 58 |
|
---|
[6607] | 59 | protected static bool FindInsertionPlace(PotvinEncoding individual, int city, bool allowInfeasible,
|
---|
| 60 | out int route, out int place) {
|
---|
[4376] | 61 | return individual.FindInsertionPlace(
|
---|
[6607] | 62 | city, -1, allowInfeasible,
|
---|
| 63 | out route, out place);
|
---|
[4376] | 64 | }
|
---|
| 65 |
|
---|
| 66 | protected Tour FindRoute(PotvinEncoding solution, int city) {
|
---|
| 67 | Tour found = null;
|
---|
| 68 |
|
---|
| 69 | foreach (Tour tour in solution.Tours) {
|
---|
| 70 | if (tour.Stops.Contains(city)) {
|
---|
| 71 | found = tour;
|
---|
| 72 | break;
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | return found;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[6607] | 79 | protected static bool RouteUnrouted(PotvinEncoding solution, bool allowInfeasible) {
|
---|
[4376] | 80 | bool success = true;
|
---|
[6607] | 81 | int index = 0;
|
---|
| 82 | while (index < solution.Unrouted.Count && success) {
|
---|
| 83 | int unrouted = solution.Unrouted[index];
|
---|
| 84 |
|
---|
| 85 | int route, place;
|
---|
| 86 | if (FindInsertionPlace(solution, unrouted, allowInfeasible,
|
---|
| 87 | out route, out place)) {
|
---|
| 88 | solution.Tours[route].Stops.Insert(place, unrouted);
|
---|
| 89 | } else {
|
---|
| 90 | success = false;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | index++;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | for (int i = 0; i < index; i++)
|
---|
| 97 | solution.Unrouted.RemoveAt(0);
|
---|
| 98 |
|
---|
| 99 | return success;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | protected static bool Repair(IRandom random, PotvinEncoding solution, Tour newTour, IVRPProblemInstance instance, bool allowInfeasible) {
|
---|
| 103 | bool success = true;
|
---|
| 104 |
|
---|
[4376] | 105 | //remove duplicates from new tour
|
---|
| 106 | for (int i = 0; i < newTour.Stops.Count; i++) {
|
---|
| 107 | for (int j = 0; j < newTour.Stops.Count; j++) {
|
---|
| 108 | if (newTour.Stops[i] == newTour.Stops[j] && i != j) {
|
---|
| 109 | if (random.NextDouble() < 0.5)
|
---|
| 110 | newTour.Stops[i] = 0;
|
---|
| 111 | else
|
---|
| 112 | newTour.Stops[j] = 0;
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 | while (newTour.Stops.Contains(0))
|
---|
| 117 | newTour.Stops.Remove(0);
|
---|
| 118 |
|
---|
| 119 | //remove duplicates from old tours
|
---|
| 120 | for (int i = 0; i < newTour.Stops.Count; i++) {
|
---|
| 121 | foreach (Tour tour in solution.Tours) {
|
---|
| 122 | if (tour != newTour && tour.Stops.Contains(newTour.Stops[i])) {
|
---|
| 123 | tour.Stops.Remove(newTour.Stops[i]);
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | //remove empty tours
|
---|
| 129 | List<Tour> toBeDeleted = new List<Tour>();
|
---|
| 130 | foreach (Tour tour in solution.Tours) {
|
---|
| 131 | if (tour.Stops.Count == 0)
|
---|
| 132 | toBeDeleted.Add(tour);
|
---|
| 133 | }
|
---|
| 134 | foreach (Tour tour in toBeDeleted) {
|
---|
| 135 | solution.Tours.Remove(tour);
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[6607] | 138 | if (!allowInfeasible && !instance.Feasible(newTour))
|
---|
| 139 | return false;
|
---|
| 140 |
|
---|
[4376] | 141 | //route unrouted vehicles
|
---|
[6607] | 142 | success = RouteUnrouted(solution, allowInfeasible);
|
---|
[4376] | 143 |
|
---|
| 144 | return success;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | public override IOperation Apply() {
|
---|
| 148 | ItemArray<IVRPEncoding> parents = new ItemArray<IVRPEncoding>(ParentsParameter.ActualValue.Length);
|
---|
| 149 | for (int i = 0; i < ParentsParameter.ActualValue.Length; i++) {
|
---|
| 150 | IVRPEncoding solution = ParentsParameter.ActualValue[i];
|
---|
| 151 |
|
---|
| 152 | if (!(solution is PotvinEncoding)) {
|
---|
| 153 | parents[i] = PotvinEncoding.ConvertFrom(solution, ProblemInstance);
|
---|
| 154 | } else {
|
---|
| 155 | parents[i] = solution;
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
| 158 | ParentsParameter.ActualValue = parents;
|
---|
| 159 |
|
---|
| 160 | ChildParameter.ActualValue = Crossover(RandomParameter.ActualValue, parents[0] as PotvinEncoding, parents[1] as PotvinEncoding);
|
---|
| 161 |
|
---|
| 162 | return base.Apply();
|
---|
| 163 | }
|
---|
| 164 | }
|
---|
| 165 | }
|
---|