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 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Optimization;
|
---|
26 | using HeuristicLab.Parameters;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
|
---|
31 | [Item("PotvinCrossover", "A VRP crossover operation.")]
|
---|
32 | [StorableClass]
|
---|
33 | public abstract class PotvinCrossover : VRPCrossover, IStochasticOperator {
|
---|
34 | public ILookupParameter<IRandom> RandomParameter {
|
---|
35 | get { return (LookupParameter<IRandom>)Parameters["Random"]; }
|
---|
36 | }
|
---|
37 |
|
---|
38 | [StorableConstructor]
|
---|
39 | protected PotvinCrossover(bool deserializing) : base(deserializing) { }
|
---|
40 | protected PotvinCrossover(PotvinCrossover original, Cloner cloner)
|
---|
41 | : base(original, cloner) {
|
---|
42 | }
|
---|
43 |
|
---|
44 | public PotvinCrossover() {
|
---|
45 | Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic manipulation operators."));
|
---|
46 | }
|
---|
47 |
|
---|
48 | protected abstract PotvinEncoding Crossover(IRandom random, PotvinEncoding parent1, PotvinEncoding parent2);
|
---|
49 |
|
---|
50 | protected static bool FindInsertionPlace(PotvinEncoding individual, int city,
|
---|
51 | DoubleArray dueTime, DoubleArray serviceTime, DoubleArray readyTime, DoubleArray demand,
|
---|
52 | DoubleValue capacity, DistanceMatrix distMatrix,
|
---|
53 | out int route, out int place) {
|
---|
54 | return individual.FindInsertionPlace(
|
---|
55 | dueTime, serviceTime, readyTime,
|
---|
56 | demand, capacity, distMatrix,
|
---|
57 | city, -1, out route, out place);
|
---|
58 | }
|
---|
59 |
|
---|
60 | protected Tour FindRoute(PotvinEncoding solution, int city) {
|
---|
61 | Tour found = null;
|
---|
62 |
|
---|
63 | foreach (Tour tour in solution.Tours) {
|
---|
64 | if (tour.Cities.Contains(city)) {
|
---|
65 | found = tour;
|
---|
66 | break;
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | return found;
|
---|
71 | }
|
---|
72 |
|
---|
73 | protected static bool RouteUnrouted(PotvinEncoding solution, DistanceMatrix distMatrix,
|
---|
74 | DoubleArray dueTime, DoubleArray readyTime, DoubleArray serviceTime, DoubleArray demand, DoubleValue capacity) {
|
---|
75 | bool success = true;
|
---|
76 | int index = 0;
|
---|
77 | while (index < solution.Unrouted.Count && success) {
|
---|
78 | int unrouted = solution.Unrouted[index];
|
---|
79 |
|
---|
80 | int route, place;
|
---|
81 | if (FindInsertionPlace(solution, unrouted,
|
---|
82 | dueTime, serviceTime, readyTime, demand, capacity,
|
---|
83 | distMatrix,
|
---|
84 | out route, out place)) {
|
---|
85 | solution.Tours[route].Cities.Insert(place, unrouted);
|
---|
86 | } else {
|
---|
87 | success = false;
|
---|
88 | }
|
---|
89 |
|
---|
90 | index++;
|
---|
91 | }
|
---|
92 |
|
---|
93 | for (int i = 0; i < index; i++)
|
---|
94 | solution.Unrouted.RemoveAt(0);
|
---|
95 |
|
---|
96 | return success;
|
---|
97 | }
|
---|
98 |
|
---|
99 | protected static bool Repair(IRandom random, PotvinEncoding solution, Tour newTour, DistanceMatrix distmatrix,
|
---|
100 | DoubleArray dueTime, DoubleArray readyTime, DoubleArray serviceTime, DoubleArray demand, DoubleValue capacity) {
|
---|
101 | bool success = true;
|
---|
102 |
|
---|
103 | //remove duplicates from new tour
|
---|
104 | for (int i = 0; i < newTour.Cities.Count; i++) {
|
---|
105 | for (int j = 0; j < newTour.Cities.Count; j++) {
|
---|
106 | if (newTour.Cities[i] == newTour.Cities[j] && i != j) {
|
---|
107 | if (random.NextDouble() < 0.5)
|
---|
108 | newTour.Cities[i] = 0;
|
---|
109 | else
|
---|
110 | newTour.Cities[j] = 0;
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|
114 | while (newTour.Cities.Contains(0))
|
---|
115 | newTour.Cities.Remove(0);
|
---|
116 |
|
---|
117 | if (!newTour.Feasible(
|
---|
118 | dueTime, serviceTime, readyTime, demand, capacity, distmatrix))
|
---|
119 | return false;
|
---|
120 |
|
---|
121 | //remove duplicates from old tours
|
---|
122 | for (int i = 0; i < newTour.Cities.Count; i++) {
|
---|
123 | foreach (Tour tour in solution.Tours) {
|
---|
124 | if (tour != newTour && tour.Cities.Contains(newTour.Cities[i])) {
|
---|
125 | tour.Cities.Remove(newTour.Cities[i]);
|
---|
126 | }
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | //remove empty tours
|
---|
131 | List<Tour> toBeDeleted = new List<Tour>();
|
---|
132 | foreach (Tour tour in solution.Tours) {
|
---|
133 | if (tour.Cities.Count == 0)
|
---|
134 | toBeDeleted.Add(tour);
|
---|
135 | }
|
---|
136 | foreach (Tour tour in toBeDeleted) {
|
---|
137 | solution.Tours.Remove(tour);
|
---|
138 | }
|
---|
139 |
|
---|
140 | //route unrouted vehicles
|
---|
141 | success = RouteUnrouted(solution, distmatrix, dueTime, readyTime, serviceTime, demand, capacity);
|
---|
142 |
|
---|
143 | return success;
|
---|
144 | }
|
---|
145 |
|
---|
146 | public override IOperation Apply() {
|
---|
147 | ItemArray<IVRPEncoding> parents = new ItemArray<IVRPEncoding>(ParentsParameter.ActualValue.Length);
|
---|
148 | for (int i = 0; i < ParentsParameter.ActualValue.Length; i++) {
|
---|
149 | IVRPEncoding solution = ParentsParameter.ActualValue[i];
|
---|
150 |
|
---|
151 | if (!(solution is PotvinEncoding)) {
|
---|
152 | parents[i] = PotvinEncoding.ConvertFrom(solution, DistanceMatrixParameter);
|
---|
153 | } else {
|
---|
154 | parents[i] = solution;
|
---|
155 | }
|
---|
156 | }
|
---|
157 | ParentsParameter.ActualValue = parents;
|
---|
158 |
|
---|
159 | ChildParameter.ActualValue = Crossover(RandomParameter.ActualValue, parents[0] as PotvinEncoding, parents[1] as PotvinEncoding);
|
---|
160 |
|
---|
161 | return base.Apply();
|
---|
162 | }
|
---|
163 | }
|
---|
164 | }
|
---|