#region License Information /* HeuristicLab * Copyright (C) 2002-2011 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 System.Collections.Generic; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Data; using HeuristicLab.Parameters; using HeuristicLab.Problems.VehicleRouting.Encodings.Potvin; using HeuristicLab.Problems.VehicleRouting.Encodings; using HeuristicLab.Problems.VehicleRouting; namespace HeuristicLab.Analysis.FitnessLandscape.VRP { [Item("RelocateManipulator", "Relocate manipulation")] [StorableClass] public sealed class RelocateManipulator : PotvinManipulator { [StorableConstructor] private RelocateManipulator(bool deserializing) : base(deserializing) { } private RelocateManipulator(RelocateManipulator original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new RelocateManipulator(this, cloner); } public RelocateManipulator() : base() { } private static bool RouteFeasible(Tour tour, DoubleArray demand, DoubleValue capacity) { double routeLoad = 0; for (int i = 0; i < tour.Cities.Count; i++) { routeLoad += demand[tour.Cities[i]]; } return routeLoad <= capacity.Value; } public static void Apply(IRandom random, PotvinEncoding individual, DoubleArray demand, DoubleValue capacity, bool allowInfeasible) { bool feasible; do { feasible = true; int cities = individual.Cities; int city = 1 + random.Next(cities); Tour originalTour = individual.Tours.Find(t => t.Cities.Contains(city)); //consider creating new route individual.Tours.Add(new Tour()); int position = 1 + random.Next(cities + individual.Tours.Count - 1); if (position >= city) { position++; } int originalPosition = originalTour.Cities.IndexOf(city); originalTour.Cities.RemoveAt(originalPosition); Tour insertionTour; int insertionPosition; if (position <= cities) { insertionTour = individual.Tours.Find(t => t.Cities.Contains(position)); insertionPosition = insertionTour.Cities.IndexOf(position) + 1; } else { insertionTour = individual.Tours[position - cities - 1]; insertionPosition = 0; } if (!allowInfeasible && insertionTour != originalTour) { bool originalFeasible = RouteFeasible(insertionTour, demand, capacity); if (originalFeasible) { double routeLoad = 0; for (int i = 0; i < insertionTour.Cities.Count; i++) { routeLoad += demand[insertionTour.Cities[i]]; } routeLoad += demand[city]; feasible = routeLoad <= capacity.Value; } } if (feasible) { insertionTour.Cities.Insert(insertionPosition, city); } else { originalTour.Cities.Insert(originalPosition, city); } individual.Tours.RemoveAll(t => t.Cities.Count == 0); } while (!feasible); } protected override void Manipulate(IRandom random, PotvinEncoding individual) { BoolValue useDistanceMatrix = UseDistanceMatrixParameter.ActualValue; DoubleMatrix coordinates = CoordinatesParameter.ActualValue; DistanceMatrix distMatrix = VRPUtilities.GetDistanceMatrix(coordinates, DistanceMatrixParameter, useDistanceMatrix); DoubleArray demand = DemandParameter.ActualValue; DoubleValue capacity = CapacityParameter.ActualValue; bool allowInfeasible = AllowInfeasibleSolutions.Value.Value; Apply(random, individual, demand, capacity, allowInfeasible); } } }