#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; namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin { [Item("PotvinOnePointCrossoverSingleMoveGenerator", "Generates a single customer relocation move from a given VRP encoding.")] [StorableClass] public sealed class PotvinOnePointCrossoverSingleMoveGenerator : PotvinOnePointCrossoverMoveGenerator, ISingleMoveGenerator { #region IMultiVRPMoveOperator Members public override ILookupParameter VRPMoveParameter { get { return (ILookupParameter)Parameters["PotvinOnePointCrossoverMove"]; } } #endregion public ILookupParameter RandomParameter { get { return (ILookupParameter)Parameters["Random"]; } } public override IDeepCloneable Clone(Cloner cloner) { return new PotvinOnePointCrossoverSingleMoveGenerator(this, cloner); } [StorableConstructor] private PotvinOnePointCrossoverSingleMoveGenerator(bool deserializing) : base(deserializing) { } public PotvinOnePointCrossoverSingleMoveGenerator() : base() { Parameters.Add(new LookupParameter("Random", "The random number generator.")); } private PotvinOnePointCrossoverSingleMoveGenerator(PotvinOnePointCrossoverMoveGenerator original, Cloner cloner) : base(original, cloner) { } public static PotvinOnePointCrossoverMove Apply(PotvinEncoding individual, IVRPProblemInstance problemInstance, IRandom rand) { int route1Idx = rand.Next(individual.Tours.Count); int route2Idx = rand.Next(individual.Tours.Count - 1); if (route2Idx >= route1Idx) route2Idx++; Tour route1 = individual.Tours[route1Idx]; Tour route2 = individual.Tours[route2Idx]; int x1 = rand.Next(route1.Stops.Count + 1); int x2 = rand.Next(route2.Stops.Count + 1); return new PotvinOnePointCrossoverMove(route1Idx, x1, route2Idx, x2, individual); } protected override PotvinOnePointCrossoverMove[] GenerateMoves(PotvinEncoding individual, IVRPProblemInstance problemInstance) { List result = new List(); PotvinOnePointCrossoverMove move = Apply(individual, ProblemInstance, RandomParameter.ActualValue); if (move != null) result.Add(move); return result.ToArray(); } } }