Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added support for multi depot CVRP instances (#1177)

File size: 5.5 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 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;
30using HeuristicLab.Problems.VehicleRouting.Variants;
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]
35  public class AlbaEncoding : PermutationEncoding  {   
36    #region IVRPEncoding Members
37    public override List<Tour> GetTours() {
38      Repair();
39      List<Tour> result = new List<Tour>();
40
41      int cities = ProblemInstance.Cities.Value;
42     
43      Tour tour = new Tour();
44      for (int i = 0; i < this.array.Length; i++) {
45        if (this.array[i] >= cities) {
46          if (tour.Stops.Count > 0) {
47            result.Add(tour);
48
49            tour = new Tour();
50          }
51        } else {
52          tour.Stops.Add(this.array[i] + 1);
53        }
54      }
55
56      if (tour.Stops.Count > 0) {
57        result.Add(tour);
58      }
59
60      return result;
61    }
62
63    public override int GetVehicleAssignment(int tour) {
64      int vehicleAssignment = -1;
65      Tour currentTour = GetTours()[tour];
66
67      int lastStop = currentTour.Stops[
68        currentTour.Stops.Count - 1] - 1;
69
70      int lastStopIndex = this.IndexOf(lastStop);
71
72      if (lastStopIndex == this.Length - 1) {
73        int i = this.Length - 1;
74
75        while (vehicleAssignment == -1) {
76          if (this.array[i] >= ProblemInstance.Cities.Value) {
77            vehicleAssignment = this.array[i] - ProblemInstance.Cities.Value;
78          }
79         
80          i--;
81        }
82      }
83      else
84        vehicleAssignment = this[lastStopIndex + 1] - this.ProblemInstance.Cities.Value;
85
86      return vehicleAssignment;
87    }
88    #endregion
89
90    public void Repair() {
91      int cities = ProblemInstance.Cities.Value;
92
93      if (this[this.Length - 1] < cities) {
94        int index = this.Length - 2;
95        while (this[index] < cities) {
96          index--;
97        }
98
99        int vehicle = this[index];
100        for (int i = index; i < this.Length - 1; i++)
101          this[i] = this[i + 1];
102        this[Length - 1] = vehicle;
103      }
104    }
105
106    public AlbaEncoding(Permutation permutation, IVRPProblemInstance instance)
107      : base(permutation, instance) {
108    }
109
110    [StorableConstructor]
111    private AlbaEncoding(bool serializing)
112      : base(serializing) {
113    }
114
115    public override IDeepCloneable Clone(Cloner cloner) {
116      return new AlbaEncoding(this, cloner);
117    }
118
119    protected AlbaEncoding(AlbaEncoding original, Cloner cloner)
120      : base(original, cloner) {
121    }
122
123    public static AlbaEncoding ConvertFrom(List<int> routeParam, IVRPProblemInstance instance) {
124      List<int> route = new List<int>(routeParam);
125     
126      int cities = instance.Cities.Value;
127
128      int vehicle = cities;
129      for (int i = 0; i < route.Count; i++) {
130        if (route[i] == 0) {
131          route[i] = vehicle;
132          vehicle++;
133        } else {
134          route[i] = route[i] - 1;
135        }
136      }
137
138      return new AlbaEncoding(
139        new Permutation(PermutationTypes.RelativeUndirected, route.ToArray()),
140        instance);
141    }
142
143    public static AlbaEncoding ConvertFrom(IVRPEncoding encoding, IVRPProblemInstance instance) {
144      List<Tour> tours = encoding.GetTours();
145
146      int cities = 0;
147      foreach (Tour tour in tours) {
148        cities += tour.Stops.Count;
149      }
150
151      int emptyVehicles = instance.Vehicles.Value - tours.Count;
152
153      int[] array = new int[cities + tours.Count + emptyVehicles];
154      int delimiter = 0;
155      int arrayIndex = 0;
156     
157      foreach (Tour tour in tours) {
158        foreach (int city in tour.Stops) {
159          array[arrayIndex] = city - 1;
160          arrayIndex++;
161        }
162
163        if (arrayIndex != array.Length) {
164          array[arrayIndex] = cities + encoding.GetVehicleAssignment(delimiter);
165         
166          delimiter++;
167          arrayIndex++;
168        }
169      }
170
171      for (int i = 0; i < emptyVehicles; i++) {
172        array[arrayIndex] = cities + encoding.GetVehicleAssignment(delimiter);
173
174        delimiter++;
175        arrayIndex++;
176      }
177
178      AlbaEncoding solution = new AlbaEncoding(new Permutation(PermutationTypes.RelativeUndirected, new IntArray(array)), instance);
179
180      return solution;
181    }
182  }
183}
Note: See TracBrowser for help on using the repository browser.