Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceOverhaul/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/GVR/Crossovers/GVRCrossover.cs @ 14712

Last change on this file since 14712 was 14711, checked in by gkronber, 8 years ago

#2520

  • renamed StorableClass -> StorableType
  • changed persistence to use GUIDs instead of type names
File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Encodings.General;
29using HeuristicLab.Problems.VehicleRouting.Interfaces;
30
31namespace 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  [StorableType("40711F17-A1F6-4902-A97F-1DD47F1C41F7")]
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
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
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);
61
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)) {
82          double distance = ProblemInstance.GetDistance(subroute[0], i, child);
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    }
104
105    public override IOperation InstrumentedApply() {
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
119      return base.InstrumentedApply();
120    }
121  }
122}
Note: See TracBrowser for help on using the repository browser.