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.Common;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Data;
|
---|
25 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 | using System.Collections.Generic;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.Alba {
|
---|
30 | [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.")]
|
---|
31 | [StorableClass]
|
---|
32 | class AlbaEncoding : Permutation, IVRPEncoding {
|
---|
33 | [Storable]
|
---|
34 | private int cities;
|
---|
35 |
|
---|
36 | #region IVRPEncoding Members
|
---|
37 | public ItemList<Tour> Tours {
|
---|
38 | get {
|
---|
39 | ItemList<Tour> result = new ItemList<Tour>();
|
---|
40 |
|
---|
41 | Tour tour = new Tour();
|
---|
42 | for (int i = 0; i < this.array.Length; i++) {
|
---|
43 | if (this.array[i] >= cities) {
|
---|
44 | if (tour.Cities.Count > 0) {
|
---|
45 | result.Add(tour);
|
---|
46 |
|
---|
47 | tour = new Tour();
|
---|
48 | }
|
---|
49 | } else {
|
---|
50 | tour.Cities.Add(this.array[i] + 1);
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | if (tour.Cities.Count > 0) {
|
---|
55 | result.Add(tour);
|
---|
56 | }
|
---|
57 |
|
---|
58 | return result;
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | public int Cities {
|
---|
63 | get { return cities; }
|
---|
64 | }
|
---|
65 |
|
---|
66 | public int MaxVehicles {
|
---|
67 | get { return Length - Cities; }
|
---|
68 | }
|
---|
69 |
|
---|
70 | #endregion
|
---|
71 |
|
---|
72 | public override IDeepCloneable Clone(HeuristicLab.Common.Cloner cloner) {
|
---|
73 | AlbaEncoding clone = new AlbaEncoding(
|
---|
74 | new Permutation(this.PermutationType, this.array), cities);
|
---|
75 | cloner.RegisterClonedObject(this, clone);
|
---|
76 | clone.readOnly = readOnly;
|
---|
77 | return clone;
|
---|
78 | }
|
---|
79 |
|
---|
80 | public AlbaEncoding(Permutation permutation, int cities)
|
---|
81 | : base(PermutationTypes.RelativeUndirected) {
|
---|
82 | this.array = new int[permutation.Length];
|
---|
83 | for (int i = 0; i < array.Length; i++)
|
---|
84 | this.array[i] = permutation[i];
|
---|
85 |
|
---|
86 | this.cities = cities;
|
---|
87 | }
|
---|
88 |
|
---|
89 | [StorableConstructor]
|
---|
90 | private AlbaEncoding(bool serializing)
|
---|
91 | : base() {
|
---|
92 | }
|
---|
93 |
|
---|
94 | public static AlbaEncoding ConvertFrom(IVRPEncoding encoding, int vehicles) {
|
---|
95 | ItemList<Tour> tours = encoding.Tours;
|
---|
96 |
|
---|
97 | int cities = 0;
|
---|
98 | foreach (Tour tour in tours) {
|
---|
99 | cities += tour.Cities.Count;
|
---|
100 | }
|
---|
101 |
|
---|
102 | int emptyVehicles = vehicles - tours.Count;
|
---|
103 |
|
---|
104 | int[] array = new int[cities + tours.Count + emptyVehicles - 1];
|
---|
105 | int delimiter = cities;
|
---|
106 | int arrayIndex = 0;
|
---|
107 |
|
---|
108 | foreach (Tour tour in tours) {
|
---|
109 | foreach (int city in tour.Cities) {
|
---|
110 | array[arrayIndex] = city - 1;
|
---|
111 | arrayIndex++;
|
---|
112 | }
|
---|
113 |
|
---|
114 | if (arrayIndex != array.Length) {
|
---|
115 | array[arrayIndex] = delimiter;
|
---|
116 | delimiter++;
|
---|
117 | arrayIndex++;
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | for (int i = 0; i < emptyVehicles - 1; i++) {
|
---|
122 | array[arrayIndex] = delimiter;
|
---|
123 | delimiter++;
|
---|
124 | arrayIndex++;
|
---|
125 | }
|
---|
126 |
|
---|
127 | AlbaEncoding solution = new AlbaEncoding(new Permutation(PermutationTypes.RelativeUndirected, new IntArray(array)), cities);
|
---|
128 |
|
---|
129 | return solution;
|
---|
130 | }
|
---|
131 |
|
---|
132 | public static AlbaEncoding ConvertFrom(List<int> routeParam) {
|
---|
133 | List<int> route = new List<int>(routeParam);
|
---|
134 |
|
---|
135 | int cities = 0;
|
---|
136 | for (int i = 0; i < route.Count; i++) {
|
---|
137 | if (route[i] != 0) {
|
---|
138 | cities++;
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | int vehicle = cities;
|
---|
143 | for (int i = 0; i < route.Count; i++) {
|
---|
144 | if (route[i] == 0) {
|
---|
145 | route[i] = vehicle;
|
---|
146 | vehicle++;
|
---|
147 | } else {
|
---|
148 | route[i] = route[i] - 1;
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | return new AlbaEncoding(
|
---|
153 | new Permutation(PermutationTypes.RelativeUndirected, route.ToArray()),
|
---|
154 | cities);
|
---|
155 | }
|
---|
156 |
|
---|
157 | internal static void RemoveUnusedParameters(ParameterCollection parameters) {
|
---|
158 | parameters.Remove("DistanceMatrix");
|
---|
159 | parameters.Remove("UseDistanceMatrix");
|
---|
160 | parameters.Remove("Capacity");
|
---|
161 | parameters.Remove("Demand");
|
---|
162 | parameters.Remove("ReadyTime");
|
---|
163 | parameters.Remove("DueTime");
|
---|
164 | parameters.Remove("ServiceTime");
|
---|
165 | }
|
---|
166 | }
|
---|
167 | }
|
---|