Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7259 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

File size: 4.9 KB
RevLine 
[3938]1#region License Information
2/* HeuristicLab
[7259]3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3938]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
[4722]22using System.Collections.Generic;
[4068]23using HeuristicLab.Common;
[3938]24using HeuristicLab.Core;
25using HeuristicLab.Data;
[4068]26using HeuristicLab.Encodings.PermutationEncoding;
[3938]27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[4352]28using HeuristicLab.Problems.VehicleRouting.Encodings.General;
[3938]29
30namespace HeuristicLab.Problems.VehicleRouting.Encodings.Alba {
[4177]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.")]
[3938]32  [StorableClass]
[4352]33  public class AlbaEncoding : PermutationEncoding {
[3938]34    [Storable]
35    private int cities;
[4722]36
[4150]37    #region IVRPEncoding Members
[4352]38    public override List<Tour> GetTours(ILookupParameter<DoubleMatrix> distanceMatrix = null, int maxVehicles = int.MaxValue) {
39      List<Tour> result = new List<Tour>();
[3938]40
[4352]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();
[4068]48          }
[4352]49        } else {
50          tour.Cities.Add(this.array[i] + 1);
[3938]51        }
[4352]52      }
[3938]53
[4352]54      if (tour.Cities.Count > 0) {
55        result.Add(tour);
56      }
[3938]57
[4352]58      return result;
[3938]59    }
60
61    public int Cities {
62      get { return cities; }
63    }
64
[4150]65    public int MaxVehicles {
[4722]66      get { return Length - Cities + 1; }
[4150]67    }
68
[3938]69    #endregion
70
[4722]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;
[4150]78    }
79
80    public AlbaEncoding(Permutation permutation, int cities)
[4352]81      : base(permutation) {
[4150]82      this.cities = cities;
83    }
84
[4722]85    public override IDeepCloneable Clone(Cloner cloner) {
86      return new AlbaEncoding(this, cloner);
[4150]87    }
88
[4352]89    public static AlbaEncoding ConvertFrom(IVRPEncoding encoding, int vehicles, ILookupParameter<DoubleMatrix> distanceMatrix) {
90      List<Tour> tours = encoding.GetTours(distanceMatrix, vehicles);
[3938]91
[4068]92      int cities = 0;
93      foreach (Tour tour in tours) {
[4174]94        cities += tour.Cities.Count;
[4068]95      }
[4150]96
97      int emptyVehicles = vehicles - tours.Count;
98
99      int[] array = new int[cities + tours.Count + emptyVehicles - 1];
[4068]100      int delimiter = cities;
101      int arrayIndex = 0;
[3938]102
[4068]103      foreach (Tour tour in tours) {
[4174]104        foreach (int city in tour.Cities) {
[4722]105          array[arrayIndex] = city - 1;
106          arrayIndex++;
[4068]107        }
[3938]108
[4068]109        if (arrayIndex != array.Length) {
110          array[arrayIndex] = delimiter;
111          delimiter++;
112          arrayIndex++;
[3938]113        }
[4068]114      }
[3938]115
[4150]116      for (int i = 0; i < emptyVehicles - 1; i++) {
117        array[arrayIndex] = delimiter;
118        delimiter++;
119        arrayIndex++;
120      }
[4722]121
[4150]122      AlbaEncoding solution = new AlbaEncoding(new Permutation(PermutationTypes.RelativeUndirected, new IntArray(array)), cities);
[3938]123
124      return solution;
125    }
126
[4150]127    public static AlbaEncoding ConvertFrom(List<int> routeParam) {
128      List<int> route = new List<int>(routeParam);
[4352]129      route.RemoveAt(routeParam.Count - 1);
[4722]130
[4150]131      int cities = 0;
132      for (int i = 0; i < route.Count; i++) {
133        if (route[i] != 0) {
134          cities++;
135        }
136      }
[3938]137
[4150]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      }
[4068]147
[4150]148      return new AlbaEncoding(
149        new Permutation(PermutationTypes.RelativeUndirected, route.ToArray()),
150        cities);
151    }
[4179]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    }
[3938]161  }
162}
Note: See TracBrowser for help on using the repository browser.