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.Collections.Generic;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 | using HeuristicLab.Problems.VehicleRouting.Encodings.General;
|
---|
29 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.Alba {
|
---|
32 | [Item("AlbaEncoding", "Represents an Alba encoding of VRP solutions. It is implemented as described in Alba, E. and Dorronsoro, B. (2004). Solving the Vehicle Routing Problem by Using Cellular Genetic Algorithms.")]
|
---|
33 | [StorableClass]
|
---|
34 | public class AlbaEncoding : PermutationEncoding {
|
---|
35 | #region IVRPEncoding Members
|
---|
36 | public override List<Tour> GetTours() {
|
---|
37 | Repair();
|
---|
38 | List<Tour> result = new List<Tour>();
|
---|
39 |
|
---|
40 | int cities = ProblemInstance.Cities.Value;
|
---|
41 |
|
---|
42 | Tour tour = new Tour();
|
---|
43 | for (int i = 0; i < this.array.Length; i++) {
|
---|
44 | if (this.array[i] >= cities) {
|
---|
45 | if (tour.Stops.Count > 0) {
|
---|
46 | result.Add(tour);
|
---|
47 |
|
---|
48 | tour = new Tour();
|
---|
49 | }
|
---|
50 | } else {
|
---|
51 | tour.Stops.Add(this.array[i] + 1);
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | if (tour.Stops.Count > 0) {
|
---|
56 | result.Add(tour);
|
---|
57 | }
|
---|
58 |
|
---|
59 | return result;
|
---|
60 | }
|
---|
61 |
|
---|
62 | public override int GetVehicleAssignment(int tour) {
|
---|
63 | int vehicleAssignment = -1;
|
---|
64 | Tour currentTour = GetTours()[tour];
|
---|
65 |
|
---|
66 | int lastStop = currentTour.Stops[
|
---|
67 | currentTour.Stops.Count - 1] - 1;
|
---|
68 |
|
---|
69 | int lastStopIndex = this.IndexOf(lastStop);
|
---|
70 |
|
---|
71 | if (lastStopIndex == this.Length - 1) {
|
---|
72 | int i = this.Length - 1;
|
---|
73 |
|
---|
74 | while (vehicleAssignment == -1) {
|
---|
75 | if (this.array[i] >= ProblemInstance.Cities.Value) {
|
---|
76 | vehicleAssignment = this.array[i] - ProblemInstance.Cities.Value;
|
---|
77 | }
|
---|
78 |
|
---|
79 | i--;
|
---|
80 | }
|
---|
81 | } else
|
---|
82 | vehicleAssignment = this[lastStopIndex + 1] - this.ProblemInstance.Cities.Value;
|
---|
83 |
|
---|
84 | return vehicleAssignment;
|
---|
85 | }
|
---|
86 | #endregion
|
---|
87 |
|
---|
88 | public void Repair() {
|
---|
89 | int cities = ProblemInstance.Cities.Value;
|
---|
90 |
|
---|
91 | if (this[this.Length - 1] < cities) {
|
---|
92 | int index = this.Length - 2;
|
---|
93 | while (this[index] < cities) {
|
---|
94 | index--;
|
---|
95 | }
|
---|
96 |
|
---|
97 | int vehicle = this[index];
|
---|
98 | for (int i = index; i < this.Length - 1; i++)
|
---|
99 | this[i] = this[i + 1];
|
---|
100 | this[Length - 1] = vehicle;
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | public AlbaEncoding(Permutation permutation, IVRPProblemInstance instance)
|
---|
105 | : base(permutation, instance) {
|
---|
106 | }
|
---|
107 |
|
---|
108 | [StorableConstructor]
|
---|
109 | protected AlbaEncoding(bool serializing)
|
---|
110 | : base(serializing) {
|
---|
111 | }
|
---|
112 |
|
---|
113 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
114 | return new AlbaEncoding(this, cloner);
|
---|
115 | }
|
---|
116 |
|
---|
117 | protected AlbaEncoding(AlbaEncoding original, Cloner cloner)
|
---|
118 | : base(original, cloner) {
|
---|
119 | }
|
---|
120 |
|
---|
121 | public static AlbaEncoding ConvertFrom(List<int> routeParam, IVRPProblemInstance instance) {
|
---|
122 | List<int> route = new List<int>(routeParam);
|
---|
123 |
|
---|
124 | int cities = instance.Cities.Value;
|
---|
125 |
|
---|
126 | for (int i = 0; i < route.Count; i++) {
|
---|
127 | if (route[i] <= 0) {
|
---|
128 | int vehicle = -route[i];
|
---|
129 | route[i] = cities + vehicle;
|
---|
130 | } else {
|
---|
131 | route[i] = route[i] - 1;
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | return new AlbaEncoding(
|
---|
136 | new Permutation(PermutationTypes.RelativeUndirected, route.ToArray()),
|
---|
137 | instance);
|
---|
138 | }
|
---|
139 |
|
---|
140 | public static AlbaEncoding ConvertFrom(IVRPEncoding encoding, IVRPProblemInstance instance) {
|
---|
141 | List<Tour> tours = encoding.GetTours();
|
---|
142 |
|
---|
143 | int cities = 0;
|
---|
144 | foreach (Tour tour in tours) {
|
---|
145 | cities += tour.Stops.Count;
|
---|
146 | }
|
---|
147 |
|
---|
148 | int emptyVehicles = instance.Vehicles.Value - tours.Count;
|
---|
149 |
|
---|
150 | int[] array = new int[cities + tours.Count + emptyVehicles];
|
---|
151 | int delimiter = 0;
|
---|
152 | int arrayIndex = 0;
|
---|
153 |
|
---|
154 | foreach (Tour tour in tours) {
|
---|
155 | foreach (int city in tour.Stops) {
|
---|
156 | array[arrayIndex] = city - 1;
|
---|
157 | arrayIndex++;
|
---|
158 | }
|
---|
159 |
|
---|
160 | if (arrayIndex != array.Length) {
|
---|
161 | array[arrayIndex] = cities + encoding.GetVehicleAssignment(delimiter);
|
---|
162 |
|
---|
163 | delimiter++;
|
---|
164 | arrayIndex++;
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | for (int i = 0; i < emptyVehicles; i++) {
|
---|
169 | array[arrayIndex] = cities + encoding.GetVehicleAssignment(delimiter);
|
---|
170 |
|
---|
171 | delimiter++;
|
---|
172 | arrayIndex++;
|
---|
173 | }
|
---|
174 |
|
---|
175 | AlbaEncoding solution = new AlbaEncoding(new Permutation(PermutationTypes.RelativeUndirected, new IntArray(array)), instance);
|
---|
176 |
|
---|
177 | return solution;
|
---|
178 | }
|
---|
179 | }
|
---|
180 | }
|
---|