[4379] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[4379] | 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;
|
---|
[4379] | 24 | using HeuristicLab.Core;
|
---|
[8053] | 25 | using HeuristicLab.Optimization;
|
---|
[4379] | 26 | using HeuristicLab.Parameters;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 | using HeuristicLab.Problems.VehicleRouting.Encodings.General;
|
---|
| 29 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.GVR {
|
---|
| 32 | [Item("GVRCrossover", "The GVR crossover operation. It is implemented as described in Pereira, F.B. et al (2002). GVR: a New Genetic Representation for the Vehicle Routing Problem. AICS 2002, LNAI 2464, pp. 95-102.")]
|
---|
| 33 | [StorableClass]
|
---|
| 34 | public sealed class GVRCrossover : VRPCrossover, IStochasticOperator, IGVROperator {
|
---|
| 35 | public ILookupParameter<IRandom> RandomParameter {
|
---|
| 36 | get { return (LookupParameter<IRandom>)Parameters["Random"]; }
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | [StorableConstructor]
|
---|
| 40 | private GVRCrossover(bool deserializing) : base(deserializing) { }
|
---|
| 41 |
|
---|
| 42 | public GVRCrossover() {
|
---|
| 43 | Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic manipulation operators."));
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[4752] | 46 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 47 | return new GVRCrossover(this, cloner);
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | private GVRCrossover(GVRCrossover original, Cloner cloner)
|
---|
| 51 | : base(original, cloner) {
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[4379] | 54 | private GVREncoding Crossover(IRandom random, GVREncoding parent1, GVREncoding parent2) {
|
---|
| 55 | GVREncoding child = parent1.Clone() as GVREncoding;
|
---|
| 56 |
|
---|
| 57 | Tour tour = parent2.Tours[random.Next(parent2.Tours.Count)];
|
---|
| 58 | int breakPoint1 = random.Next(tour.Stops.Count);
|
---|
| 59 | int length = random.Next(1, tour.Stops.Count - breakPoint1 + 1);
|
---|
| 60 | List<int> subroute = tour.Stops.GetRange(breakPoint1, length);
|
---|
[8053] | 61 |
|
---|
[4379] | 62 | //remove duplicates
|
---|
| 63 | List<Tour> toBeRemoved = new List<Tour>();
|
---|
| 64 |
|
---|
| 65 | foreach (Tour route in child.Tours) {
|
---|
| 66 | foreach (int city in subroute) {
|
---|
| 67 | route.Stops.Remove(city);
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | if (route.Stops.Count == 0)
|
---|
| 71 | toBeRemoved.Add(route);
|
---|
| 72 | }
|
---|
| 73 | foreach (Tour route in toBeRemoved) {
|
---|
| 74 | child.Tours.Remove(route);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | //choose nearest customer
|
---|
| 78 | double minDistance = -1;
|
---|
| 79 | int customer = -1;
|
---|
| 80 | for (int i = 1; i <= ProblemInstance.Cities.Value; i++) {
|
---|
| 81 | if (!subroute.Contains(i)) {
|
---|
[6851] | 82 | double distance = ProblemInstance.GetDistance(subroute[0], i, child);
|
---|
[4379] | 83 |
|
---|
| 84 | if (customer == -1 || distance < minDistance) {
|
---|
| 85 | customer = i;
|
---|
| 86 | minDistance = distance;
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | //insert
|
---|
| 92 | if (customer != -1) {
|
---|
| 93 | Tour newTour;
|
---|
| 94 | int newPosition;
|
---|
| 95 | child.FindCustomer(customer, out newTour, out newPosition);
|
---|
| 96 | newTour.Stops.InsertRange(newPosition + 1, subroute);
|
---|
| 97 | } else {
|
---|
| 98 | //special case -> only one tour, whole tour has been chosen as subroute
|
---|
| 99 | child = parent1.Clone() as GVREncoding;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | return child;
|
---|
| 103 | }
|
---|
[8053] | 104 |
|
---|
[10298] | 105 | public override IOperation InstrumentedApply() {
|
---|
[4379] | 106 | ItemArray<IVRPEncoding> parents = new ItemArray<IVRPEncoding>(ParentsParameter.ActualValue.Length);
|
---|
| 107 | for (int i = 0; i < ParentsParameter.ActualValue.Length; i++) {
|
---|
| 108 | IVRPEncoding solution = ParentsParameter.ActualValue[i];
|
---|
| 109 | if (!(solution is GVREncoding)) {
|
---|
| 110 | parents[i] = GVREncoding.ConvertFrom(solution, ProblemInstance);
|
---|
| 111 | } else {
|
---|
| 112 | parents[i] = solution;
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 | ParentsParameter.ActualValue = parents;
|
---|
| 116 |
|
---|
| 117 | ChildParameter.ActualValue = Crossover(RandomParameter.ActualValue, parents[0] as GVREncoding, parents[1] as GVREncoding);
|
---|
| 118 |
|
---|
[10298] | 119 | return base.InstrumentedApply();
|
---|
[4379] | 120 | }
|
---|
| 121 | }
|
---|
| 122 | }
|
---|