#region License Information /* HeuristicLab * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.PermutationEncoding; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using System.Collections.Generic; using HeuristicLab.Problems.VehicleRouting.Encodings.General; using HeuristicLab.Problems.VehicleRouting.Interfaces; using HeuristicLab.Problems.VehicleRouting.Variants; namespace HeuristicLab.Problems.VehicleRouting.Encodings.Alba { [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.")] [StorableClass] public class AlbaEncoding : PermutationEncoding, ISingleDepotEncoding, ITimeWindowedEncoding, IHeterogenousCapacitatedEncoding { #region IVRPEncoding Members public override List GetTours() { List result = new List(); int cities = ProblemInstance.Cities.Value; Tour tour = new Tour(); for (int i = 0; i < this.array.Length; i++) { if (this.array[i] >= cities) { if (tour.Stops.Count > 0) { result.Add(tour); tour = new Tour(); } } else { tour.Stops.Add(this.array[i] + 1); } } if (tour.Stops.Count > 0) { result.Add(tour); } return result; } #endregion #region IHeterogenousCapacitatedEncoding Members public int GetVehicleAssignment(int tour) { int vehicleAssignment = -1; Tour currentTour = GetTours()[tour]; int lastStop = currentTour.Stops[ currentTour.Stops.Count - 1] - 1; int lastStopIndex = this.IndexOf(lastStop); if (lastStopIndex == this.Length - 1) vehicleAssignment = this.ProblemInstance.Vehicles.Value - 1; else vehicleAssignment = this[lastStopIndex + 1] - this.ProblemInstance.Cities.Value; return vehicleAssignment; } #endregion public override IDeepCloneable Clone(HeuristicLab.Common.Cloner cloner) { AlbaEncoding clone = new AlbaEncoding( new Permutation(this.PermutationType, new IntArray(this.array)), ProblemInstance); cloner.RegisterClonedObject(this, clone); clone.readOnly = readOnly; return clone; } public AlbaEncoding(Permutation permutation, IVRPProblemInstance instance) : base(permutation, instance) { } [StorableConstructor] private AlbaEncoding(bool serializing) : base(serializing) { } public static AlbaEncoding ConvertFrom(List routeParam, IVRPProblemInstance instance) { List route = new List(routeParam); route.RemoveAt(routeParam.Count - 1); int cities = 0; for (int i = 0; i < route.Count; i++) { if (route[i] != 0) { cities++; } } int vehicle = cities; for (int i = 0; i < route.Count; i++) { if (route[i] == 0) { route[i] = vehicle; vehicle++; } else { route[i] = route[i] - 1; } } return new AlbaEncoding( new Permutation(PermutationTypes.RelativeUndirected, route.ToArray()), instance); } public static AlbaEncoding ConvertFrom(IVRPEncoding encoding, IVRPProblemInstance instance) { List tours = encoding.GetTours(); int cities = 0; foreach (Tour tour in tours) { cities += tour.Stops.Count; } int emptyVehicles = instance.Vehicles.Value - tours.Count; int[] array = new int[cities + tours.Count + emptyVehicles - 1]; int delimiter = 0; int arrayIndex = 0; foreach (Tour tour in tours) { foreach (int city in tour.Stops) { array[arrayIndex] = city - 1; arrayIndex++; } if (arrayIndex != array.Length) { if (encoding is IHeterogenousCapacitatedEncoding) { array[arrayIndex] = cities + (encoding as IHeterogenousCapacitatedEncoding).GetVehicleAssignment(delimiter); } else { array[arrayIndex] = cities + delimiter; } delimiter++; arrayIndex++; } } for (int i = 0; i < emptyVehicles - 1; i++) { if (encoding is IHeterogenousCapacitatedEncoding) { array[arrayIndex] = cities + (encoding as IHeterogenousCapacitatedEncoding).GetVehicleAssignment(delimiter); } else { array[arrayIndex] = cities + delimiter; } delimiter++; arrayIndex++; } AlbaEncoding solution = new AlbaEncoding(new Permutation(PermutationTypes.RelativeUndirected, new IntArray(array)), instance); return solution; } } }