Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Alba/AlbaEncodedSolution.cs @ 18242

Last change on this file since 18242 was 17717, checked in by abeham, 4 years ago

#2521: working on VRP (refactoring all the capabilities, features, and operator discovery)

File size: 5.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 System.Linq;
24using HEAL.Attic;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.PermutationEncoding;
29using HeuristicLab.Problems.VehicleRouting.Interfaces;
30
31namespace HeuristicLab.Problems.VehicleRouting.Encodings.Alba {
32  [Item("AlbaEncodedSolution", "Represents an Alba encoded solution for the VRP. It is implemented as described in Alba, E. and Dorronsoro, B. (2004). Solving the Vehicle Routing Problem by Using Cellular Genetic Algorithms.")]
33  [StorableType("8BB9735D-8371-46C1-B843-A6864B2ACDA5")]
34  public class AlbaEncodedSolution : General.PermutationEncodedSolution {
35    #region IVRPEncoding Members
36    public override List<Tour> GetTours() {
37      Repair();
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
62    public override int GetVehicleAssignment(int tour) {
63      int vehicleAssignment = -1;
64      Tour currentTour = GetTours()[tour];
65
66      int lastStop = currentTour.Stops[
67        currentTour.Stops.Count - 1] - 1;
68
69      int lastStopIndex = this.IndexOf(lastStop);
70
71      if (lastStopIndex == this.Length - 1) {
72        int i = this.Length - 1;
73
74        while (vehicleAssignment == -1) {
75          if (this.array[i] >= ProblemInstance.Cities.Value) {
76            vehicleAssignment = this.array[i] - ProblemInstance.Cities.Value;
77          }
78
79          i--;
80        }
81      } else
82        vehicleAssignment = this[lastStopIndex + 1] - this.ProblemInstance.Cities.Value;
83
84      return vehicleAssignment;
85    }
86    #endregion
87
88    public void Repair() {
89      int cities = ProblemInstance.Cities.Value;
90
91      if (this[this.Length - 1] < cities) {
92        int index = this.Length - 2;
93        while (this[index] < cities) {
94          index--;
95        }
96
97        int vehicle = this[index];
98        for (int i = index; i < this.Length - 1; i++)
99          this[i] = this[i + 1];
100        this[Length - 1] = vehicle;
101      }
102    }
103
104    public AlbaEncodedSolution(Permutation permutation, IVRPProblemInstance instance)
105      : base(permutation, instance) {
106    }
107
108    [StorableConstructor]
109    protected AlbaEncodedSolution(StorableConstructorFlag _) : base(_) {
110    }
111
112    public override IDeepCloneable Clone(Cloner cloner) {
113      return new AlbaEncodedSolution(this, cloner);
114    }
115
116    protected AlbaEncodedSolution(AlbaEncodedSolution original, Cloner cloner)
117      : base(original, cloner) {
118    }
119
120    public static AlbaEncodedSolution 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 AlbaEncodedSolution(
135        new Permutation(PermutationTypes.RelativeUndirected, route.ToArray()),
136        instance);
137    }
138
139    public static AlbaEncodedSolution ConvertFrom(IVRPEncodedSolution encoding, IVRPProblemInstance instance) {
140      List<Tour> tours = encoding.GetTours();
141      var cities = tours.Sum(x => x.Stops.Count);
142
143      int emptyVehicles = instance.Vehicles.Value - tours.Count;
144
145      int[] array = new int[cities + tours.Count + emptyVehicles];
146      int delimiter = 0;
147      int arrayIndex = 0;
148
149      foreach (Tour tour in tours) {
150        foreach (int city in tour.Stops) {
151          array[arrayIndex] = city - 1;
152          arrayIndex++;
153        }
154
155        if (arrayIndex != array.Length) {
156          array[arrayIndex] = cities + encoding.GetVehicleAssignment(delimiter);
157
158          delimiter++;
159          arrayIndex++;
160        }
161      }
162
163      for (int i = 0; i < emptyVehicles; i++) {
164        array[arrayIndex] = cities + encoding.GetVehicleAssignment(delimiter);
165
166        delimiter++;
167        arrayIndex++;
168      }
169
170      return new AlbaEncodedSolution(new Permutation(PermutationTypes.RelativeUndirected, new IntArray(array)), instance);
171    }
172  }
173}
Note: See TracBrowser for help on using the repository browser.