#region License Information /* HeuristicLab * Copyright (C) 2002-2010 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; using HeuristicLab.Core; using HeuristicLab.Optimization; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Parameters; using System.Collections.Generic; using HeuristicLab.Problems.VehicleRouting.Interfaces; using HeuristicLab.Common; using HeuristicLab.Data; using HeuristicLab.Problems.VehicleRouting.Encodings.General; using HeuristicLab.Problems.VehicleRouting.Variants; namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin { [Item("PotvinPDExchangeSingleMoveGenerator", "Generates a single exchange move from a given PDP encoding.")] [StorableClass] public sealed class PotvinPDExchangeSingleMoveGenerator : PotvinPDExchangeMoveGenerator, ISingleMoveGenerator { public ILookupParameter RandomParameter { get { return (ILookupParameter)Parameters["Random"]; } } public override IDeepCloneable Clone(Cloner cloner) { return new PotvinPDExchangeSingleMoveGenerator(this, cloner); } [StorableConstructor] private PotvinPDExchangeSingleMoveGenerator(bool deserializing) : base(deserializing) { } public PotvinPDExchangeSingleMoveGenerator() : base() { Parameters.Add(new LookupParameter("Random", "The random number generator.")); } private PotvinPDExchangeSingleMoveGenerator(PotvinPDExchangeSingleMoveGenerator original, Cloner cloner) : base(original, cloner) { } public static PotvinPDExchangeMove Apply(PotvinEncoding individual, IVRPProblemInstance problemInstance, IRandom rand) { List cities = new List(); IPickupAndDeliveryProblemInstance pdp = problemInstance as IPickupAndDeliveryProblemInstance; for (int i = 1; i <= individual.Cities; i++) { if (pdp == null || pdp.GetDemand(i) >= 0) cities.Add(i); } PotvinPDExchangeMove move = null; while (cities.Count > 0 && move == null) { int city = cities[rand.Next(cities.Count)]; Tour oldTour = individual.Tours.Find(t => t.Stops.Contains(city)); int oldTourIndex = individual.Tours.IndexOf(oldTour); int max = individual.Tours.Count; if (individual.Tours.Count < problemInstance.Vehicles.Value) max = max - 1; int newTourIndex = rand.Next(max); if (newTourIndex >= oldTourIndex) newTourIndex++; Tour newTour = individual.Tours[newTourIndex]; List tourCities = new List(); foreach (int stop in newTour.Stops) { if (pdp == null || (pdp.GetDemand(stop) >= 0 && pdp.PickupDeliveryLocation[stop] != pdp.PickupDeliveryLocation[city] && pdp.PickupDeliveryLocation[stop] != city && pdp.PickupDeliveryLocation[city] != stop)) tourCities.Add(stop); } if (tourCities.Count > 0) { int replaced = tourCities[rand.Next(tourCities.Count)]; move = new PotvinPDExchangeMove(city, oldTourIndex, newTourIndex, replaced, individual); } } return move; } protected override PotvinPDExchangeMove[] GenerateMoves(PotvinEncoding individual, IVRPProblemInstance problemInstance) { List result = new List(); PotvinPDExchangeMove move = Apply(individual, ProblemInstance, RandomParameter.ActualValue); if (move != null) result.Add(move); return result.ToArray(); } } }