Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Alba/AlbaEncoding.cs @ 10355

Last change on this file since 10355 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 5.4 KB
RevLine 
[4365]1#region License Information
2/* HeuristicLab
[9456]3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[4365]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
[8053]22using System.Collections.Generic;
[4365]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;
29using HeuristicLab.Problems.VehicleRouting.Interfaces;
30
31namespace 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]
[8053]34  public class AlbaEncoding : PermutationEncoding {
[4365]35    #region IVRPEncoding Members
36    public override List<Tour> GetTours() {
[6851]37      Repair();
[4365]38      List<Tour> result = new List<Tour>();
39
40      int cities = ProblemInstance.Cities.Value;
[8053]41
[4365]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);
[6851]57      }
[4365]58
59      return result;
60    }
61
[6851]62    public override int GetVehicleAssignment(int tour) {
[4376]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
[6851]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          }
[8053]78
[6851]79          i--;
80        }
[8053]81      } else
[4376]82        vehicleAssignment = this[lastStopIndex + 1] - this.ProblemInstance.Cities.Value;
83
84      return vehicleAssignment;
85    }
86    #endregion
87
[6851]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
[4365]104    public AlbaEncoding(Permutation permutation, IVRPProblemInstance instance)
105      : base(permutation, instance) {
106    }
107
108    [StorableConstructor]
[7906]109    protected AlbaEncoding(bool serializing)
[4365]110      : base(serializing) {
111    }
112
[4752]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
[4365]121    public static AlbaEncoding ConvertFrom(List<int> routeParam, IVRPProblemInstance instance) {
122      List<int> route = new List<int>(routeParam);
[8053]123
[5201]124      int cities = instance.Cities.Value;
[4365]125
126      for (int i = 0; i < route.Count; i++) {
[7543]127        if (route[i] <= 0) {
128          int vehicle = -route[i];
129          route[i] = cities + vehicle;
[4365]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    }
[4376]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
[6851]150      int[] array = new int[cities + tours.Count + emptyVehicles];
[4376]151      int delimiter = 0;
152      int arrayIndex = 0;
[8053]153
[4376]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) {
[6851]161          array[arrayIndex] = cities + encoding.GetVehicleAssignment(delimiter);
[8053]162
[4376]163          delimiter++;
164          arrayIndex++;
165        }
166      }
167
[6851]168      for (int i = 0; i < emptyVehicles; i++) {
169        array[arrayIndex] = cities + encoding.GetVehicleAssignment(delimiter);
[4376]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    }
[4365]179  }
180}
Note: See TracBrowser for help on using the repository browser.