Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Alba/AlbaEncoding.cs @ 4722

Last change on this file since 4722 was 4722, checked in by swagner, 14 years ago

Merged cloning refactoring branch back into trunk (#922)

File size: 4.9 KB
Line 
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
22using System.Collections.Generic;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.PermutationEncoding;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Problems.VehicleRouting.Encodings.General;
29
30namespace HeuristicLab.Problems.VehicleRouting.Encodings.Alba {
31  [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.")]
32  [StorableClass]
33  public class AlbaEncoding : PermutationEncoding {
34    [Storable]
35    private int cities;
36
37    #region IVRPEncoding Members
38    public override List<Tour> GetTours(ILookupParameter<DoubleMatrix> distanceMatrix = null, int maxVehicles = int.MaxValue) {
39      List<Tour> result = new List<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    public int Cities {
62      get { return cities; }
63    }
64
65    public int MaxVehicles {
66      get { return Length - Cities + 1; }
67    }
68
69    #endregion
70
71
72    [StorableConstructor]
73    protected AlbaEncoding(bool deserializing) : base(deserializing) { }
74    protected AlbaEncoding(AlbaEncoding original, Cloner cloner)
75      : base(original, cloner) {
76      cities = original.cities;
77      readOnly = original.readOnly;
78    }
79
80    public AlbaEncoding(Permutation permutation, int cities)
81      : base(permutation) {
82      this.cities = cities;
83    }
84
85    public override IDeepCloneable Clone(Cloner cloner) {
86      return new AlbaEncoding(this, cloner);
87    }
88
89    public static AlbaEncoding ConvertFrom(IVRPEncoding encoding, int vehicles, ILookupParameter<DoubleMatrix> distanceMatrix) {
90      List<Tour> tours = encoding.GetTours(distanceMatrix, vehicles);
91
92      int cities = 0;
93      foreach (Tour tour in tours) {
94        cities += tour.Cities.Count;
95      }
96
97      int emptyVehicles = vehicles - tours.Count;
98
99      int[] array = new int[cities + tours.Count + emptyVehicles - 1];
100      int delimiter = cities;
101      int arrayIndex = 0;
102
103      foreach (Tour tour in tours) {
104        foreach (int city in tour.Cities) {
105          array[arrayIndex] = city - 1;
106          arrayIndex++;
107        }
108
109        if (arrayIndex != array.Length) {
110          array[arrayIndex] = delimiter;
111          delimiter++;
112          arrayIndex++;
113        }
114      }
115
116      for (int i = 0; i < emptyVehicles - 1; i++) {
117        array[arrayIndex] = delimiter;
118        delimiter++;
119        arrayIndex++;
120      }
121
122      AlbaEncoding solution = new AlbaEncoding(new Permutation(PermutationTypes.RelativeUndirected, new IntArray(array)), cities);
123
124      return solution;
125    }
126
127    public static AlbaEncoding ConvertFrom(List<int> routeParam) {
128      List<int> route = new List<int>(routeParam);
129      route.RemoveAt(routeParam.Count - 1);
130
131      int cities = 0;
132      for (int i = 0; i < route.Count; i++) {
133        if (route[i] != 0) {
134          cities++;
135        }
136      }
137
138      int vehicle = cities;
139      for (int i = 0; i < route.Count; i++) {
140        if (route[i] == 0) {
141          route[i] = vehicle;
142          vehicle++;
143        } else {
144          route[i] = route[i] - 1;
145        }
146      }
147
148      return new AlbaEncoding(
149        new Permutation(PermutationTypes.RelativeUndirected, route.ToArray()),
150        cities);
151    }
152
153    internal static void RemoveUnusedParameters(ParameterCollection parameters) {
154      parameters.Remove("UseDistanceMatrix");
155      parameters.Remove("Capacity");
156      parameters.Remove("Demand");
157      parameters.Remove("ReadyTime");
158      parameters.Remove("DueTime");
159      parameters.Remove("ServiceTime");
160    }
161  }
162}
Note: See TracBrowser for help on using the repository browser.