Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4218 was 4204, checked in by svonolfe, 14 years ago

Added operations for the Alba encoding (#1039)

File size: 5.0 KB
RevLine 
[3938]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
[4068]22using HeuristicLab.Common;
[3938]23using HeuristicLab.Core;
24using HeuristicLab.Data;
[4068]25using HeuristicLab.Encodings.PermutationEncoding;
[3938]26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[4150]27using System.Collections.Generic;
[3938]28
29namespace HeuristicLab.Problems.VehicleRouting.Encodings.Alba {
[4177]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.")]
[3938]31  [StorableClass]
[4204]32  public class AlbaEncoding : Permutation, IVRPEncoding {
[3938]33    [Storable]
34    private int cities;
[4150]35   
36    #region IVRPEncoding Members
[3938]37    public ItemList<Tour> Tours {
38      get {
39        ItemList<Tour> result = new ItemList<Tour>();
[4150]40       
[3938]41        Tour tour = new Tour();
42        for (int i = 0; i < this.array.Length; i++) {
43          if (this.array[i] >= cities) {
[4174]44            if (tour.Cities.Count > 0) {
[3938]45              result.Add(tour);
46
47              tour = new Tour();
48            }
49          } else {
[4174]50            tour.Cities.Add(this.array[i] + 1);
[4068]51          }
[3938]52        }
53
[4174]54        if (tour.Cities.Count > 0) {
[3938]55          result.Add(tour);
56        }
57
58        return result;
59      }
60    }
61
62    public int Cities {
63      get { return cities; }
64    }
65
[4150]66    public int MaxVehicles {
67      get { return Length - Cities;  }
68    }
69
[3938]70    #endregion
71
[4150]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) {
[4068]95      ItemList<Tour> tours = encoding.Tours;
[3938]96
[4068]97      int cities = 0;
98      foreach (Tour tour in tours) {
[4174]99        cities += tour.Cities.Count;
[4068]100      }
[4150]101
102      int emptyVehicles = vehicles - tours.Count;
103
104      int[] array = new int[cities + tours.Count + emptyVehicles - 1];
[4068]105      int delimiter = cities;
106      int arrayIndex = 0;
[3938]107
[4068]108      foreach (Tour tour in tours) {
[4174]109        foreach (int city in tour.Cities) {
110            array[arrayIndex] = city - 1;
[4150]111            arrayIndex++;
[4068]112        }
[3938]113
[4068]114        if (arrayIndex != array.Length) {
115          array[arrayIndex] = delimiter;
116          delimiter++;
117          arrayIndex++;
[3938]118        }
[4068]119      }
[3938]120
[4150]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);
[3938]128
129      return solution;
130    }
131
[4150]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      }
[3938]141
[4150]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      }
[4068]151
[4150]152      return new AlbaEncoding(
153        new Permutation(PermutationTypes.RelativeUndirected, route.ToArray()),
154        cities);
155    }
[4179]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    }
[3938]166  }
167}
Note: See TracBrowser for help on using the repository browser.