#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 System; using HeuristicLab.Problems.VehicleRouting.Interfaces; namespace HeuristicLab.Analysis.FitnessLandscape.VRP { [Item("TwoOptManipulator", "Two opt manipulation")] [StorableClass] public sealed class TwoOptManipulator : PotvinManipulator { [StorableConstructor] private TwoOptManipulator(bool deserializing) : base(deserializing) { } private TwoOptManipulator(TwoOptManipulator original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new TwoOptManipulator(this, cloner); } public TwoOptManipulator() : base() { } public static void Apply(IRandom random, PotvinEncoding individual, IVRPProblemInstance instance, bool allowInfeasible) { List tours = individual.Tours.FindAll(t => t.Stops.Count >= 2); if (tours.Count == 0) return; bool feasible; int count = 0; do { feasible = true; int tourIdx = random.Next(tours.Count); Tour tour = tours[tourIdx]; int first; int length; if (tour.Stops.Count == 2) { first = 0; length = 2; } else { first = random.Next(tour.Stops.Count - 1); if (first == tour.Stops.Count - 2) { length = tour.Stops.Count - first; } else { length = random.Next(tour.Stops.Count - first - 1) + 2; } } var originalFeasible = instance.TourFeasible(tour, individual); var segment = tour.Stops.GetRange(first, length); tour.Stops.RemoveRange(first, length); segment.Reverse(); tour.Stops.InsertRange(first, segment); if (!allowInfeasible && originalFeasible) { feasible = instance.TourFeasible(tour, individual); if (!feasible) { tour.Stops.RemoveRange(first, length); segment.Reverse(); tour.Stops.InsertRange(first, segment); } } } while(!feasible && count++ < 100); } protected override void Manipulate(IRandom random, PotvinEncoding individual) { bool allowInfeasible = AllowInfeasibleSolutions.Value.Value; Apply(random, individual, ProblemInstance, allowInfeasible); } } }