Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4501 was 4352, checked in by svonolfe, 14 years ago

Merged r4351 of the VRP feature branch into trunk (#1039)

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