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