Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Alba/AlbaEncoding.cs

Last change on this file was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

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