1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 | using HeuristicLab.Problems.VehicleRouting.Encodings.General;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.Zhu {
|
---|
32 | [Item("ZhuEncoding", "Represents a Zhu encoding of VRP solutions. It is implemented as described in Zhu, K.Q. (2000). A New Genetic Algorithm For VRPTW. Proceedings of the International Conference on Artificial Intelligence.")]
|
---|
33 | [StorableClass]
|
---|
34 | public class ZhuEncoding : PermutationEncoding {
|
---|
35 | [Storable]
|
---|
36 | private int cities;
|
---|
37 |
|
---|
38 | [Storable]
|
---|
39 | private DoubleMatrix coordinates;
|
---|
40 |
|
---|
41 | [Storable]
|
---|
42 | private BoolValue useDistanceMatrix;
|
---|
43 |
|
---|
44 | [Storable]
|
---|
45 | DoubleArray dueTimeArray;
|
---|
46 |
|
---|
47 | [Storable]
|
---|
48 | DoubleArray serviceTimeArray;
|
---|
49 |
|
---|
50 | [Storable]
|
---|
51 | DoubleArray readyTimeArray;
|
---|
52 |
|
---|
53 | [Storable]
|
---|
54 | DoubleArray demandArray;
|
---|
55 |
|
---|
56 | [Storable]
|
---|
57 | DoubleValue capacity;
|
---|
58 |
|
---|
59 | #region IVRPEncoding Members
|
---|
60 | public override List<Tour> GetTours(ILookupParameter<DoubleMatrix> distanceMatrix, int maxVehicles = int.MaxValue) {
|
---|
61 | List<Tour> result = new List<Tour>();
|
---|
62 |
|
---|
63 | Tour newTour = new Tour();
|
---|
64 |
|
---|
65 | DistanceMatrix distMatrix = VRPUtilities.GetDistanceMatrix(coordinates, distanceMatrix, useDistanceMatrix);
|
---|
66 |
|
---|
67 | for (int i = 0; i < this.Length; i++) {
|
---|
68 | int city = this[i] + 1;
|
---|
69 | newTour.Cities.Add(city);
|
---|
70 | if (!newTour.Feasible(
|
---|
71 | dueTimeArray,
|
---|
72 | serviceTimeArray,
|
---|
73 | readyTimeArray,
|
---|
74 | demandArray,
|
---|
75 | capacity,
|
---|
76 | distMatrix)) {
|
---|
77 | newTour.Cities.Remove(city);
|
---|
78 | if (newTour.Cities.Count > 0)
|
---|
79 | result.Add(newTour);
|
---|
80 |
|
---|
81 | newTour = new Tour();
|
---|
82 | newTour.Cities.Add(city);
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | if (newTour.Cities.Count > 0)
|
---|
87 | result.Add(newTour);
|
---|
88 |
|
---|
89 | //if there are too many vehicles - repair
|
---|
90 | while (result.Count > maxVehicles) {
|
---|
91 | Tour tour = result[result.Count - 1];
|
---|
92 |
|
---|
93 | //find predecessor / successor in permutation
|
---|
94 | int predecessorIndex = Array.IndexOf(this.array, tour.Cities[0] - 1) - 1;
|
---|
95 | if (predecessorIndex >= 0) {
|
---|
96 | int predecessor = this[predecessorIndex] + 1;
|
---|
97 |
|
---|
98 | foreach (Tour t in result) {
|
---|
99 | int insertPosition = t.Cities.IndexOf(predecessor) + 1;
|
---|
100 | if (insertPosition != -1) {
|
---|
101 | t.Cities.InsertRange(insertPosition, tour.Cities);
|
---|
102 | break;
|
---|
103 | }
|
---|
104 | }
|
---|
105 | } else {
|
---|
106 | int successorIndex = Array.IndexOf(this.array,
|
---|
107 | tour.Cities[tour.Cities.Count - 1] - 1) + 1;
|
---|
108 | int successor = this[successorIndex] + 1;
|
---|
109 |
|
---|
110 | foreach (Tour t in result) {
|
---|
111 | int insertPosition = t.Cities.IndexOf(successor);
|
---|
112 | if (insertPosition != -1) {
|
---|
113 | t.Cities.InsertRange(insertPosition, tour.Cities);
|
---|
114 | break;
|
---|
115 | }
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | result.Remove(tour);
|
---|
120 | }
|
---|
121 |
|
---|
122 | return result;
|
---|
123 | }
|
---|
124 | #endregion
|
---|
125 |
|
---|
126 | [StorableConstructor]
|
---|
127 | protected ZhuEncoding(bool deserializing) : base(deserializing) { }
|
---|
128 | protected ZhuEncoding(ZhuEncoding original, Cloner cloner)
|
---|
129 | : base(original, cloner) {
|
---|
130 | this.cities = original.cities;
|
---|
131 | this.dueTimeArray = original.dueTimeArray;
|
---|
132 | this.serviceTimeArray = original.serviceTimeArray;
|
---|
133 | this.readyTimeArray = original.readyTimeArray;
|
---|
134 | this.demandArray = original.demandArray;
|
---|
135 | this.capacity = original.capacity;
|
---|
136 | this.coordinates = original.coordinates;
|
---|
137 | this.useDistanceMatrix = original.useDistanceMatrix;
|
---|
138 | }
|
---|
139 | public ZhuEncoding(Permutation permutation, int cities,
|
---|
140 | DoubleArray dueTimeArray, DoubleArray serviceTimeArray, DoubleArray readyTimeArray, DoubleArray demandArray, DoubleValue capacity,
|
---|
141 | DoubleMatrix coordinates, BoolValue useDistanceMatrix)
|
---|
142 | : base(permutation) {
|
---|
143 | this.cities = cities;
|
---|
144 | this.dueTimeArray = dueTimeArray;
|
---|
145 | this.serviceTimeArray = serviceTimeArray;
|
---|
146 | this.readyTimeArray = readyTimeArray;
|
---|
147 | this.demandArray = demandArray;
|
---|
148 | this.capacity = capacity;
|
---|
149 | this.coordinates = coordinates;
|
---|
150 | this.useDistanceMatrix = useDistanceMatrix;
|
---|
151 | }
|
---|
152 |
|
---|
153 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
154 | return new ZhuEncoding(this, cloner);
|
---|
155 | }
|
---|
156 |
|
---|
157 | public static ZhuEncoding ConvertFrom(IVRPEncoding encoding, int cities,
|
---|
158 | DoubleArray dueTimeArray, DoubleArray serviceTimeArray, DoubleArray readyTimeArray, DoubleArray demandArray, DoubleValue capacity,
|
---|
159 | DoubleMatrix coordinates, ILookupParameter<DoubleMatrix> distanceMatrix, BoolValue useDistanceMatrix) {
|
---|
160 | List<Tour> tours = encoding.GetTours(distanceMatrix);
|
---|
161 | List<int> route = new List<int>();
|
---|
162 |
|
---|
163 | foreach (Tour tour in tours) {
|
---|
164 | foreach (int city in tour.Cities)
|
---|
165 | route.Add(city - 1);
|
---|
166 | }
|
---|
167 |
|
---|
168 | return new ZhuEncoding(
|
---|
169 | new Permutation(PermutationTypes.RelativeUndirected, route.ToArray()), cities,
|
---|
170 | dueTimeArray, serviceTimeArray, readyTimeArray, demandArray, capacity,
|
---|
171 | coordinates, useDistanceMatrix);
|
---|
172 | }
|
---|
173 |
|
---|
174 | public static ZhuEncoding ConvertFrom(List<int> routeParam, int cities,
|
---|
175 | DoubleArray dueTimeArray, DoubleArray serviceTimeArray, DoubleArray readyTimeArray, DoubleArray demandArray, DoubleValue capacity,
|
---|
176 | DoubleMatrix coordinates, BoolValue useDistanceMatrix) {
|
---|
177 | List<int> route = new List<int>(routeParam);
|
---|
178 |
|
---|
179 | while (route.Remove(0)) { //remove all delimiters (0)
|
---|
180 | }
|
---|
181 |
|
---|
182 | for (int i = 0; i < route.Count; i++)
|
---|
183 | route[i]--;
|
---|
184 |
|
---|
185 | return new ZhuEncoding(
|
---|
186 | new Permutation(PermutationTypes.RelativeUndirected, route.ToArray()), cities,
|
---|
187 | dueTimeArray, serviceTimeArray, readyTimeArray, demandArray, capacity,
|
---|
188 | coordinates, useDistanceMatrix);
|
---|
189 | }
|
---|
190 | }
|
---|
191 | }
|
---|