1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2013 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 | public IValueParameter<BoolValue> AllowInfeasibleSolutions {
|
---|
39 | get { return (IValueParameter<BoolValue>)Parameters["AllowInfeasibleSolutions"]; }
|
---|
40 | }
|
---|
41 |
|
---|
42 | [StorableHook(HookType.AfterDeserialization)]
|
---|
43 | private void AfterDeserialization() {
|
---|
44 | // BackwardsCompatibility3.3
|
---|
45 | #region Backwards compatible code (remove with 3.4)
|
---|
46 | if (!Parameters.ContainsKey("AllowInfeasibleSolutions")) {
|
---|
47 | Parameters.Add(new ValueParameter<BoolValue>("AllowInfeasibleSolutions", "Indicates if infeasible solutions should be allowed.", new BoolValue(false)));
|
---|
48 | }
|
---|
49 | #endregion
|
---|
50 | }
|
---|
51 |
|
---|
52 | [StorableConstructor]
|
---|
53 | protected PotvinCrossover(bool deserializing) : base(deserializing) { }
|
---|
54 | protected PotvinCrossover(PotvinCrossover original, Cloner cloner)
|
---|
55 | : base(original, cloner) {
|
---|
56 | }
|
---|
57 |
|
---|
58 | public PotvinCrossover() {
|
---|
59 | Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic manipulation operators."));
|
---|
60 | Parameters.Add(new ValueParameter<BoolValue>("AllowInfeasibleSolutions", "Indicates if infeasible solutions should be allowed.", new BoolValue(false)));
|
---|
61 | }
|
---|
62 |
|
---|
63 | protected abstract PotvinEncoding Crossover(IRandom random, PotvinEncoding parent1, PotvinEncoding parent2);
|
---|
64 |
|
---|
65 | protected static bool FindInsertionPlace(PotvinEncoding individual, int city,
|
---|
66 | DoubleArray dueTime, DoubleArray serviceTime, DoubleArray readyTime, DoubleArray demand,
|
---|
67 | DoubleValue capacity, DistanceMatrix distMatrix, bool allowInfeasible,
|
---|
68 | out int route, out int place) {
|
---|
69 | return individual.FindInsertionPlace(
|
---|
70 | dueTime, serviceTime, readyTime,
|
---|
71 | demand, capacity, distMatrix,
|
---|
72 | city, -1, allowInfeasible,
|
---|
73 | out route, out place);
|
---|
74 | }
|
---|
75 |
|
---|
76 | protected Tour FindRoute(PotvinEncoding solution, int city) {
|
---|
77 | Tour found = null;
|
---|
78 |
|
---|
79 | foreach (Tour tour in solution.Tours) {
|
---|
80 | if (tour.Cities.Contains(city)) {
|
---|
81 | found = tour;
|
---|
82 | break;
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | return found;
|
---|
87 | }
|
---|
88 |
|
---|
89 | protected static bool RouteUnrouted(PotvinEncoding solution, DistanceMatrix distMatrix,
|
---|
90 | DoubleArray dueTime, DoubleArray readyTime, DoubleArray serviceTime, DoubleArray demand, DoubleValue capacity, bool allowInfeasible) {
|
---|
91 | bool success = true;
|
---|
92 | int index = 0;
|
---|
93 | while (index < solution.Unrouted.Count && success) {
|
---|
94 | int unrouted = solution.Unrouted[index];
|
---|
95 |
|
---|
96 | int route, place;
|
---|
97 | if (FindInsertionPlace(solution, unrouted,
|
---|
98 | dueTime, serviceTime, readyTime, demand, capacity,
|
---|
99 | distMatrix, allowInfeasible,
|
---|
100 | out route, out place)) {
|
---|
101 | solution.Tours[route].Cities.Insert(place, unrouted);
|
---|
102 | } else {
|
---|
103 | success = false;
|
---|
104 | }
|
---|
105 |
|
---|
106 | index++;
|
---|
107 | }
|
---|
108 |
|
---|
109 | for (int i = 0; i < index; i++)
|
---|
110 | solution.Unrouted.RemoveAt(0);
|
---|
111 |
|
---|
112 | return success;
|
---|
113 | }
|
---|
114 |
|
---|
115 | protected static bool Repair(IRandom random, PotvinEncoding solution, Tour newTour, DistanceMatrix distmatrix,
|
---|
116 | DoubleArray dueTime, DoubleArray readyTime, DoubleArray serviceTime, DoubleArray demand, DoubleValue capacity,
|
---|
117 | bool allowInfeasible) {
|
---|
118 | bool success = true;
|
---|
119 |
|
---|
120 | //remove duplicates from new tour
|
---|
121 | for (int i = 0; i < newTour.Cities.Count; i++) {
|
---|
122 | for (int j = 0; j < newTour.Cities.Count; j++) {
|
---|
123 | if (newTour.Cities[i] == newTour.Cities[j] && i != j) {
|
---|
124 | if (random.NextDouble() < 0.5)
|
---|
125 | newTour.Cities[i] = 0;
|
---|
126 | else
|
---|
127 | newTour.Cities[j] = 0;
|
---|
128 | }
|
---|
129 | }
|
---|
130 | }
|
---|
131 | while (newTour.Cities.Contains(0))
|
---|
132 | newTour.Cities.Remove(0);
|
---|
133 |
|
---|
134 | //remove duplicates from old tours
|
---|
135 | for (int i = 0; i < newTour.Cities.Count; i++) {
|
---|
136 | foreach (Tour tour in solution.Tours) {
|
---|
137 | if (tour != newTour && tour.Cities.Contains(newTour.Cities[i])) {
|
---|
138 | tour.Cities.Remove(newTour.Cities[i]);
|
---|
139 | }
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | //remove empty tours
|
---|
144 | List<Tour> toBeDeleted = new List<Tour>();
|
---|
145 | foreach (Tour tour in solution.Tours) {
|
---|
146 | if (tour.Cities.Count == 0)
|
---|
147 | toBeDeleted.Add(tour);
|
---|
148 | }
|
---|
149 | foreach (Tour tour in toBeDeleted) {
|
---|
150 | solution.Tours.Remove(tour);
|
---|
151 | }
|
---|
152 |
|
---|
153 | if (!allowInfeasible && !newTour.Feasible(
|
---|
154 | dueTime, serviceTime, readyTime, demand, capacity, distmatrix))
|
---|
155 | return false;
|
---|
156 |
|
---|
157 | //route unrouted vehicles
|
---|
158 | success = RouteUnrouted(solution, distmatrix, dueTime, readyTime, serviceTime, demand, capacity, allowInfeasible);
|
---|
159 |
|
---|
160 | return success;
|
---|
161 | }
|
---|
162 |
|
---|
163 | public override IOperation Apply() {
|
---|
164 | ItemArray<IVRPEncoding> parents = new ItemArray<IVRPEncoding>(ParentsParameter.ActualValue.Length);
|
---|
165 | for (int i = 0; i < ParentsParameter.ActualValue.Length; i++) {
|
---|
166 | IVRPEncoding solution = ParentsParameter.ActualValue[i];
|
---|
167 |
|
---|
168 | if (!(solution is PotvinEncoding)) {
|
---|
169 | parents[i] = PotvinEncoding.ConvertFrom(solution, DistanceMatrixParameter);
|
---|
170 | } else {
|
---|
171 | parents[i] = solution;
|
---|
172 | }
|
---|
173 | }
|
---|
174 | ParentsParameter.ActualValue = parents;
|
---|
175 |
|
---|
176 | ChildParameter.ActualValue = Crossover(RandomParameter.ActualValue, parents[0] as PotvinEncoding, parents[1] as PotvinEncoding);
|
---|
177 |
|
---|
178 | return base.Apply();
|
---|
179 | }
|
---|
180 | }
|
---|
181 | }
|
---|