#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; using HeuristicLab.Problems.VehicleRouting.Interfaces; 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() { } public static void Apply(IRandom random, PotvinEncoding individual, IVRPProblemInstance problemInstance, bool allowInfeasible) { bool feasible; int count = 0; do { feasible = true; int cities = individual.Cities; int city = 1 + random.Next(cities); Tour originalTour = individual.Tours.Find(t => t.Stops.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++; } var originalFeasible = problemInstance.TourFeasible(originalTour, individual); int originalPosition = originalTour.Stops.IndexOf(city); originalTour.Stops.RemoveAt(originalPosition); Tour insertionTour; int insertionPosition; if (position <= cities) { insertionTour = individual.Tours.Find(t => t.Stops.Contains(position)); insertionPosition = insertionTour.Stops.IndexOf(position) + 1; } else { // additional insertion positions at beginning of tours insertionTour = individual.Tours[position - cities - 1]; insertionPosition = 0; } originalFeasible &= problemInstance.TourFeasible(insertionTour, individual); insertionTour.Stops.Insert(insertionPosition, city); if (!allowInfeasible && originalFeasible) { feasible = problemInstance.TourFeasible(insertionTour, individual) && problemInstance.TourFeasible(originalTour, individual); if (!feasible) { insertionTour.Stops.RemoveAt(insertionPosition); originalTour.Stops.Insert(originalPosition, city); } } individual.Tours.RemoveAll(t => t.Stops.Count == 0); } while (!feasible && count++ < 100); } protected override void Manipulate(IRandom random, PotvinEncoding individual) { bool allowInfeasible = AllowInfeasibleSolutions.Value.Value; Apply(random, individual, ProblemInstance, allowInfeasible); } } }