#region License Information /* HeuristicLab * Copyright (C) 2002-2012 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 System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.IntegerVectorEncoding; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment { [Item("SwapLocationManipluator", "Swaps two locations by exchanging all equipments between the locations.")] [StorableClass] public class SwapLocationManipluator : GQAPManipulator, ICapacitiesAwareGQAPOperator { public ILookupParameter CapacitiesParameter { get { return (ILookupParameter)Parameters["Capacities"]; } } [StorableConstructor] protected SwapLocationManipluator(bool deserializing) : base(deserializing) { } protected SwapLocationManipluator(SwapLocationManipluator original, Cloner cloner) : base(original, cloner) { } public SwapLocationManipluator() : base() { Parameters.Add(new LookupParameter("Capacities", GeneralizedQuadraticAssignmentProblem.CapacitiesDescription)); } public override IDeepCloneable Clone(Cloner cloner) { return new SwapLocationManipluator(this, cloner); } public static void Apply(IRandom random, IntegerVector assignment, int locations) { if (assignment.Length < 2) throw new InvalidOperationException("Vector is too small for swap operation."); var groupedLocations = assignment .Select((value, index) => new { Index = index, Value = value }) .GroupBy(x => x.Value) .ToDictionary(x => x.Key, y => y.AsEnumerable()); if (locations < 2) throw new InvalidOperationException("There doesn't exist more than one location."); int location1 = random.Next(locations), location2 = random.Next(locations); while (location1 == location2) location2 = random.Next(locations); if (groupedLocations.ContainsKey(location1)) { foreach (var index in groupedLocations[location1]) assignment[index.Index] = location2; } if (groupedLocations.ContainsKey(location2)) { foreach (var index in groupedLocations[location2]) assignment[index.Index] = location1; } } protected override void Manipulate(IRandom random, IntegerVector vector) { Apply(random, vector, CapacitiesParameter.ActualValue.Length); } } }