Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/GVR/Crossovers/GVRCrossover.cs @ 4383

Last change on this file since 4383 was 4379, checked in by svonolfe, 14 years ago

Added remaining encodings (#1177)

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