Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Alba/AlbaEncoding.cs @ 17097

Last change on this file since 17097 was 17097, checked in by mkommend, 5 years ago

#2520: Merged 16565 - 16579 into stable.

File size: 5.4 KB
RevLine 
[4365]1#region License Information
2/* HeuristicLab
[17097]3 * Copyright (C) 2002-2019 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;
[17097]27using HEAL.Attic;
[4365]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.")]
[17097]32  [StorableType("8BB9735D-8371-46C1-B843-A6864B2ACDA5")]
[12005]33  public class AlbaEncoding : General.PermutationEncoding {
[4365]34    #region IVRPEncoding Members
35    public override List<Tour> GetTours() {
[6851]36      Repair();
[4365]37      List<Tour> result = new List<Tour>();
38
39      int cities = ProblemInstance.Cities.Value;
[8053]40
[4365]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);
[6851]56      }
[4365]57
58      return result;
59    }
60
[6851]61    public override int GetVehicleAssignment(int tour) {
[4376]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
[6851]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          }
[8053]77
[6851]78          i--;
79        }
[8053]80      } else
[4376]81        vehicleAssignment = this[lastStopIndex + 1] - this.ProblemInstance.Cities.Value;
82
83      return vehicleAssignment;
84    }
85    #endregion
86
[6851]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
[4365]103    public AlbaEncoding(Permutation permutation, IVRPProblemInstance instance)
104      : base(permutation, instance) {
105    }
106
107    [StorableConstructor]
[17097]108    protected AlbaEncoding(StorableConstructorFlag _) : base(_) {
[4365]109    }
110
[4752]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
[4365]119    public static AlbaEncoding ConvertFrom(List<int> routeParam, IVRPProblemInstance instance) {
120      List<int> route = new List<int>(routeParam);
[8053]121
[5201]122      int cities = instance.Cities.Value;
[4365]123
124      for (int i = 0; i < route.Count; i++) {
[7543]125        if (route[i] <= 0) {
126          int vehicle = -route[i];
127          route[i] = cities + vehicle;
[4365]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    }
[4376]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
[6851]148      int[] array = new int[cities + tours.Count + emptyVehicles];
[4376]149      int delimiter = 0;
150      int arrayIndex = 0;
[8053]151
[4376]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) {
[6851]159          array[arrayIndex] = cities + encoding.GetVehicleAssignment(delimiter);
[8053]160
[4376]161          delimiter++;
162          arrayIndex++;
163        }
164      }
165
[6851]166      for (int i = 0; i < emptyVehicles; i++) {
167        array[arrayIndex] = cities + encoding.GetVehicleAssignment(delimiter);
[4376]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    }
[4365]177  }
178}
Note: See TracBrowser for help on using the repository browser.