Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/GVR/Crossovers/GVRCrossover.cs @ 5959

Last change on this file since 5959 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 4.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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;
28
29namespace HeuristicLab.Problems.VehicleRouting.Encodings.GVR {
30  [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.")]
31  [StorableClass]
32  public sealed class GVRCrossover : VRPCrossover, IStochasticOperator {
33    public ILookupParameter<IRandom> RandomParameter {
34      get { return (LookupParameter<IRandom>)Parameters["Random"]; }
35    }
36
37    [StorableConstructor]
38    private GVRCrossover(bool deserializing) : base(deserializing) { }
39    private GVRCrossover(GVRCrossover original, Cloner cloner) : base(original, cloner) { }
40    public override IDeepCloneable Clone(Cloner cloner) {
41      return new GVRCrossover(this, cloner);
42    }
43    public GVRCrossover() {
44      Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic manipulation operators."));
45      //remove unused parameters
46      Parameters.Remove("ReadyTime");
47      Parameters.Remove("DueTime");
48      Parameters.Remove("ServiceTime");
49    }
50
51    private GVREncoding Crossover(IRandom random, GVREncoding parent1, GVREncoding parent2) {
52      GVREncoding child = parent1.Clone() as GVREncoding;
53
54      Tour tour = parent2.Tours[random.Next(parent2.Tours.Count)];
55      int breakPoint1 = random.Next(tour.Cities.Count);
56      int length = random.Next(1, tour.Cities.Count - breakPoint1 + 1);
57      List<int> subroute = tour.Cities.GetRange(breakPoint1, length);
58     
59      //remove duplicates
60      List<Tour> toBeRemoved = new List<Tour>();
61
62      foreach (Tour route in child.Tours) {
63        foreach (int city in subroute) {
64          route.Cities.Remove(city);
65        }
66
67        if (route.Cities.Count == 0)
68          toBeRemoved.Add(route);
69      }
70      foreach (Tour route in toBeRemoved) {
71        child.Tours.Remove(route);
72      }
73
74      //choose nearest customer
75      double minDistance = -1;
76      int customer = -1;
77      for (int i = 1; i <= Cities; i++) {
78        if (!subroute.Contains(i)) {
79          double distance = VRPUtilities.GetDistance(subroute[0], i, CoordinatesParameter.ActualValue,
80            DistanceMatrixParameter, UseDistanceMatrixParameter.ActualValue);
81
82          if (customer == -1 || distance < minDistance) {
83            customer = i;
84            minDistance = distance;
85          }
86        }
87      }
88
89      //insert
90      if (customer != -1) {
91        Tour newTour;
92        int newPosition;
93        child.FindCustomer(customer, out newTour, out newPosition);
94        newTour.Cities.InsertRange(newPosition + 1, subroute);
95      } else {
96        //special case -> only one tour, whole tour has been chosen as subroute
97        child = parent1.Clone() as GVREncoding;
98      }
99
100      return child;
101    }
102   
103    public override IOperation Apply() {
104      ItemArray<IVRPEncoding> parents = new ItemArray<IVRPEncoding>(ParentsParameter.ActualValue.Length);
105      for (int i = 0; i < ParentsParameter.ActualValue.Length; i++) {
106        IVRPEncoding solution = ParentsParameter.ActualValue[i];
107        if (!(solution is GVREncoding)) {
108          parents[i] = GVREncoding.ConvertFrom(solution, CapacityParameter.ActualValue, DemandParameter.ActualValue,
109            DistanceMatrixParameter);
110        } else {
111          parents[i] = solution;
112        }
113      }
114      ParentsParameter.ActualValue = parents;
115
116      ChildParameter.ActualValue = Crossover(RandomParameter.ActualValue, parents[0] as GVREncoding, parents[1] as GVREncoding);
117
118      return base.Apply();
119    }
120  }
121}
Note: See TracBrowser for help on using the repository browser.