[4365] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14186] | 3 | * Copyright (C) 2002-2016 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] | 22 | using System.Collections.Generic;
|
---|
[4365] | 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
| 29 |
|
---|
| 30 | namespace 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]
|
---|
[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]
|
---|
[7906] | 108 | protected AlbaEncoding(bool serializing)
|
---|
[4365] | 109 | : base(serializing) {
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[4752] | 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 |
|
---|
[4365] | 120 | public static AlbaEncoding ConvertFrom(List<int> routeParam, IVRPProblemInstance instance) {
|
---|
| 121 | List<int> route = new List<int>(routeParam);
|
---|
[8053] | 122 |
|
---|
[5201] | 123 | int cities = instance.Cities.Value;
|
---|
[4365] | 124 |
|
---|
| 125 | for (int i = 0; i < route.Count; i++) {
|
---|
[7543] | 126 | if (route[i] <= 0) {
|
---|
| 127 | int vehicle = -route[i];
|
---|
| 128 | route[i] = cities + vehicle;
|
---|
[4365] | 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 | }
|
---|
[4376] | 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 |
|
---|
[6851] | 149 | int[] array = new int[cities + tours.Count + emptyVehicles];
|
---|
[4376] | 150 | int delimiter = 0;
|
---|
| 151 | int arrayIndex = 0;
|
---|
[8053] | 152 |
|
---|
[4376] | 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) {
|
---|
[6851] | 160 | array[arrayIndex] = cities + encoding.GetVehicleAssignment(delimiter);
|
---|
[8053] | 161 |
|
---|
[4376] | 162 | delimiter++;
|
---|
| 163 | arrayIndex++;
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 |
|
---|
[6851] | 167 | for (int i = 0; i < emptyVehicles; i++) {
|
---|
| 168 | array[arrayIndex] = cities + encoding.GetVehicleAssignment(delimiter);
|
---|
[4376] | 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 | }
|
---|
[4365] | 178 | }
|
---|
| 179 | }
|
---|