Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Alba/AlbaEncoding.cs @ 6710

Last change on this file since 6710 was 6710, checked in by svonolfe, 13 years ago

Added support for pickups and deliveries (#1177)

File size: 5.4 KB
RevLine 
[4365]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 HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Encodings.PermutationEncoding;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using System.Collections.Generic;
28using HeuristicLab.Problems.VehicleRouting.Encodings.General;
29using HeuristicLab.Problems.VehicleRouting.Interfaces;
[4376]30using HeuristicLab.Problems.VehicleRouting.Variants;
[4365]31
32namespace HeuristicLab.Problems.VehicleRouting.Encodings.Alba {
33  [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.")]
34  [StorableClass]
[6710]35  public class AlbaEncoding : PermutationEncoding, IHeterogenousCapacitatedEncoding  {   
[4365]36    #region IVRPEncoding Members
37    public override List<Tour> GetTours() {
38      List<Tour> result = new List<Tour>();
39
40      int cities = ProblemInstance.Cities.Value;
41     
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);
57      }   
58
59      return result;
60    }
61    #endregion
62
[4376]63    #region IHeterogenousCapacitatedEncoding Members
64
65    public int GetVehicleAssignment(int tour) {
66      int vehicleAssignment = -1;
67      Tour currentTour = GetTours()[tour];
68
69      int lastStop = currentTour.Stops[
70        currentTour.Stops.Count - 1] - 1;
71
72      int lastStopIndex = this.IndexOf(lastStop);
73
74      if (lastStopIndex == this.Length - 1)
75        vehicleAssignment = this.ProblemInstance.Vehicles.Value - 1;
76      else
77        vehicleAssignment = this[lastStopIndex + 1] - this.ProblemInstance.Cities.Value;
78
79      return vehicleAssignment;
80    }
81    #endregion
82
[4365]83    public AlbaEncoding(Permutation permutation, IVRPProblemInstance instance)
84      : base(permutation, instance) {
85    }
86
87    [StorableConstructor]
88    private AlbaEncoding(bool serializing)
89      : base(serializing) {
90    }
91
[4752]92    public override IDeepCloneable Clone(Cloner cloner) {
93      return new AlbaEncoding(this, cloner);
94    }
95
96    protected AlbaEncoding(AlbaEncoding original, Cloner cloner)
97      : base(original, cloner) {
98    }
99
[4365]100    public static AlbaEncoding ConvertFrom(List<int> routeParam, IVRPProblemInstance instance) {
101      List<int> route = new List<int>(routeParam);
102      route.RemoveAt(routeParam.Count - 1);
103     
[5201]104      int cities = instance.Cities.Value;
[4365]105
106      int vehicle = cities;
107      for (int i = 0; i < route.Count; i++) {
108        if (route[i] == 0) {
109          route[i] = vehicle;
110          vehicle++;
111        } else {
112          route[i] = route[i] - 1;
113        }
114      }
115
116      return new AlbaEncoding(
117        new Permutation(PermutationTypes.RelativeUndirected, route.ToArray()),
118        instance);
119    }
[4376]120
121    public static AlbaEncoding ConvertFrom(IVRPEncoding encoding, IVRPProblemInstance instance) {
122      List<Tour> tours = encoding.GetTours();
123
124      int cities = 0;
125      foreach (Tour tour in tours) {
126        cities += tour.Stops.Count;
127      }
128
129      int emptyVehicles = instance.Vehicles.Value - tours.Count;
130
131      int[] array = new int[cities + tours.Count + emptyVehicles - 1];
132      int delimiter = 0;
133      int arrayIndex = 0;
134     
135      foreach (Tour tour in tours) {
136        foreach (int city in tour.Stops) {
137          array[arrayIndex] = city - 1;
138          arrayIndex++;
139        }
140
141        if (arrayIndex != array.Length) {
142          if (encoding is IHeterogenousCapacitatedEncoding) {
143            array[arrayIndex] = cities + (encoding as IHeterogenousCapacitatedEncoding).GetVehicleAssignment(delimiter);
144          } else {
145            array[arrayIndex] = cities + delimiter;
146          }
147         
148          delimiter++;
149          arrayIndex++;
150        }
151      }
152
153      for (int i = 0; i < emptyVehicles - 1; i++) {
154        if (encoding is IHeterogenousCapacitatedEncoding) {
155          array[arrayIndex] = cities + (encoding as IHeterogenousCapacitatedEncoding).GetVehicleAssignment(delimiter);
156        } else {
157          array[arrayIndex] = cities + delimiter;
158        }
159
160        delimiter++;
161        arrayIndex++;
162      }
163
164      AlbaEncoding solution = new AlbaEncoding(new Permutation(PermutationTypes.RelativeUndirected, new IntArray(array)), instance);
165
166      return solution;
167    }
[4365]168  }
169}
Note: See TracBrowser for help on using the repository browser.