Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CloningRefactoring/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/GVR/Crossovers/GVRCrossover.cs @ 4689

Last change on this file since 4689 was 4689, checked in by abeham, 13 years ago

#922

  • Refactored GVR encoding in HeuristicLab.Problems.VehicleRouting
File size: 4.8 KB
Line 
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
22using HeuristicLab.Core;
23using HeuristicLab.Encodings.PermutationEncoding;
24using HeuristicLab.Parameters;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26using HeuristicLab.Data;
27using HeuristicLab.Optimization;
28using System.Collections.Generic;
29using HeuristicLab.Common;
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  [StorableClass]
34  public sealed class GVRCrossover : VRPCrossover, IStochasticOperator {
35    public ILookupParameter<IRandom> RandomParameter {
36      get { return (LookupParameter<IRandom>)Parameters["Random"]; }
37    }
38
39    [StorableConstructor]
40    private GVRCrossover(bool deserializing) : base(deserializing) { }
41    private GVRCrossover(GVRCrossover original, Cloner cloner) : base(original, cloner) { }
42    public override IDeepCloneable Clone(Cloner cloner) {
43      return new GVRCrossover(this, cloner);
44    }
45    public GVRCrossover() {
46      Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic manipulation operators."));
47      //remove unused parameters
48      Parameters.Remove("ReadyTime");
49      Parameters.Remove("DueTime");
50      Parameters.Remove("ServiceTime");
51    }
52
53    private GVREncoding Crossover(IRandom random, GVREncoding parent1, GVREncoding parent2) {
54      GVREncoding child = parent1.Clone() as GVREncoding;
55
56      Tour tour = parent2.Tours[random.Next(parent2.Tours.Count)];
57      int breakPoint1 = random.Next(tour.Cities.Count);
58      int length = random.Next(1, tour.Cities.Count - breakPoint1 + 1);
59      List<int> subroute = tour.Cities.GetRange(breakPoint1, length);
60     
61      //remove duplicates
62      List<Tour> toBeRemoved = new List<Tour>();
63
64      foreach (Tour route in child.Tours) {
65        foreach (int city in subroute) {
66          route.Cities.Remove(city);
67        }
68
69        if (route.Cities.Count == 0)
70          toBeRemoved.Add(route);
71      }
72      foreach (Tour route in toBeRemoved) {
73        child.Tours.Remove(route);
74      }
75
76      //choose nearest customer
77      double minDistance = -1;
78      int customer = -1;
79      for (int i = 1; i <= Cities; i++) {
80        if (!subroute.Contains(i)) {
81          double distance = VRPUtilities.GetDistance(subroute[0], i, CoordinatesParameter.ActualValue,
82            DistanceMatrixParameter, UseDistanceMatrixParameter.ActualValue);
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.Cities.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 Apply() {
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, CapacityParameter.ActualValue, DemandParameter.ActualValue,
111            DistanceMatrixParameter);
112        } else {
113          parents[i] = solution;
114        }
115      }
116      ParentsParameter.ActualValue = parents;
117
118      ChildParameter.ActualValue = Crossover(RandomParameter.ActualValue, parents[0] as GVREncoding, parents[1] as GVREncoding);
119
120      return base.Apply();
121    }
122  }
123}
Note: See TracBrowser for help on using the repository browser.