Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2707_HeuristicLab.VRPEnhancements/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Alba/AlbaEncoding.cs @ 17010

Last change on this file since 17010 was 17010, checked in by pfleck, 5 years ago

#2707 Merged trunk into branch

File size: 5.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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 HEAL.Attic;
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  [StorableType("8BB9735D-8371-46C1-B843-A6864B2ACDA5")]
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(StorableConstructorFlag _) : base(_) {
109    }
110
111    public override IDeepCloneable Clone(Cloner cloner) {
112      return new AlbaEncoding(this, cloner);
113    }
114
115    protected AlbaEncoding(AlbaEncoding original, Cloner cloner)
116      : base(original, cloner) {
117    }
118
119    public static AlbaEncoding ConvertFrom(List<int> routeParam, IVRPProblemInstance instance) {
120      List<int> route = new List<int>(routeParam);
121
122      int cities = instance.Cities.Value;
123
124      for (int i = 0; i < route.Count; i++) {
125        if (route[i] <= 0) {
126          int vehicle = -route[i];
127          route[i] = cities + vehicle;
128        } else {
129          route[i] = route[i] - 1;
130        }
131      }
132
133      return new AlbaEncoding(
134        new Permutation(PermutationTypes.RelativeUndirected, route.ToArray()),
135        instance);
136    }
137
138    public static AlbaEncoding ConvertFrom(IVRPEncoding encoding, IVRPProblemInstance instance) {
139      List<Tour> tours = encoding.GetTours();
140
141      int cities = 0;
142      foreach (Tour tour in tours) {
143        cities += tour.Stops.Count;
144      }
145
146      int emptyVehicles = instance.Vehicles.Value - tours.Count;
147
148      int[] array = new int[cities + tours.Count + emptyVehicles];
149      int delimiter = 0;
150      int arrayIndex = 0;
151
152      foreach (Tour tour in tours) {
153        foreach (int city in tour.Stops) {
154          array[arrayIndex] = city - 1;
155          arrayIndex++;
156        }
157
158        if (arrayIndex != array.Length) {
159          array[arrayIndex] = cities + encoding.GetVehicleAssignment(delimiter);
160
161          delimiter++;
162          arrayIndex++;
163        }
164      }
165
166      for (int i = 0; i < emptyVehicles; i++) {
167        array[arrayIndex] = cities + encoding.GetVehicleAssignment(delimiter);
168
169        delimiter++;
170        arrayIndex++;
171      }
172
173      AlbaEncoding solution = new AlbaEncoding(new Permutation(PermutationTypes.RelativeUndirected, new IntArray(array)), instance);
174
175      return solution;
176    }
177  }
178}
Note: See TracBrowser for help on using the repository browser.