#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("ExchangeManipulator", "Exchange manipulation")] [StorableClass] public sealed class ExchangeManipulator : PotvinManipulator { [StorableConstructor] private ExchangeManipulator(bool deserializing) : base(deserializing) { } private ExchangeManipulator(ExchangeManipulator original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new ExchangeManipulator(this, cloner); } public ExchangeManipulator() : base() { } public static void Apply(IRandom random, PotvinEncoding individual, IVRPProblemInstance problemInstance, bool allowInfeasible) { bool feasible; int count = 0; do { feasible = true; if (individual.Tours.Count > 1) { int tour1Idx = random.Next(individual.Tours.Count); int tour2Idx = random.Next(individual.Tours.Count - 1); if (tour2Idx >= tour1Idx) tour2Idx++; Tour tour1 = individual.Tours[tour1Idx]; Tour tour2 = individual.Tours[tour2Idx]; int index1 = random.Next(tour1.Stops.Count); int city1 = tour1.Stops[index1]; int index2 = random.Next(tour2.Stops.Count); int city2 = tour2.Stops[index2]; bool originalFeasible = problemInstance.TourFeasible(tour1, individual) && problemInstance.TourFeasible(tour2, individual); tour1.Stops[index1] = city2; tour2.Stops[index2] = city1; if (!allowInfeasible && originalFeasible) { feasible = problemInstance.TourFeasible(tour1, individual) && problemInstance.TourFeasible(tour2, individual); if (!feasible) { tour1.Stops[index1] = city1; tour2.Stops[index2] = city2; } } } } while (!feasible && count++ < 100); } protected override void Manipulate(IRandom random, PotvinEncoding individual) { bool allowInfeasible = AllowInfeasibleSolutions.Value.Value; Apply(random, individual, ProblemInstance, allowInfeasible); } } }