#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; namespace HeuristicLab.Analysis.FitnessLandscape.VRP { [Item("OrOptManipulator", "Or opt manipulation")] [StorableClass] public sealed class OrOptManipulator : PotvinManipulator { [StorableConstructor] private OrOptManipulator(bool deserializing) : base(deserializing) { } private OrOptManipulator(OrOptManipulator original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new OrOptManipulator(this, cloner); } public OrOptManipulator() : base() { } public static void Apply(IRandom random, PotvinEncoding individual) { List tours = individual.Tours.FindAll(t => t.Cities.Count >= 2); if (tours.Count > 0) { int tourIdx = random.Next(tours.Count); Tour tour = tours[tourIdx]; int segmentStart = random.Next(tour.Cities.Count); int segmentLength; if (segmentStart == 0) { segmentLength = 1 + random.Next(tour.Cities.Count - 1); } else { segmentLength = 1 + random.Next(tour.Cities.Count - segmentStart); } List segment = tour.Cities.GetRange(segmentStart, segmentLength); tour.Cities.RemoveRange(segmentStart, segmentLength); int newPos; if(tour.Cities.Count == 1) newPos = 0; else newPos = random.Next(tour.Cities.Count - 1); if (newPos >= segmentStart) newPos++; tour.Cities.InsertRange(newPos, segment); } } protected override void Manipulate(IRandom random, PotvinEncoding individual) { Apply(random, individual); } } }