#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.Collections.Generic; 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; using HeuristicLab.Random; namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment { [Item("DiscreteLocationCrossover", "Combines the assignment to locations from various parents.")] [StorableClass] public class DiscreteLocationCrossover : GQAPCrossover, ICapacitiesAwareGQAPOperator { public ILookupParameter CapacitiesParameter { get { return (ILookupParameter)Parameters["Capacities"]; } } [StorableConstructor] protected DiscreteLocationCrossover(bool deserializing) : base(deserializing) { } protected DiscreteLocationCrossover(DiscreteLocationCrossover original, Cloner cloner) : base(original, cloner) { } public DiscreteLocationCrossover() : base() { Parameters.Add(new LookupParameter("Capacities", GeneralizedQuadraticAssignmentProblem.CapacitiesDescription)); } public override IDeepCloneable Clone(Cloner cloner) { return new DiscreteLocationCrossover(this, cloner); } public static IntegerVector Apply(IRandom random, ItemArray parents, DoubleArray capacities) { var groupedLocations = parents .Select(p => p.Select((value, index) => new { Equipment = index, Location = value }) .GroupBy(x => x.Location) .ToDictionary(x => x.Key, y => y.AsEnumerable())).ToArray(); IntegerVector child = new IntegerVector(parents[0].Length); HashSet remainingEquipment = new HashSet(Enumerable.Range(0, child.Length)); int locations = capacities.Length; foreach (var i in Enumerable.Range(0, locations).Shuffle(random)) { int parent = random.Next(parents.Length); if (!groupedLocations[parent].ContainsKey(i)) { int tmp = parent; do { tmp = (tmp + 1) % parents.Length; } while (tmp != parent && !groupedLocations[tmp].ContainsKey(i)); if (parent == tmp) continue; else parent = tmp; } foreach (var item in groupedLocations[parent][i]) { if (remainingEquipment.Contains(item.Equipment)) { child[item.Equipment] = i; remainingEquipment.Remove(item.Equipment); } } } foreach (var equipment in remainingEquipment) { int parent = random.Next(parents.Length); child[equipment] = parents[parent][equipment]; } return child; } protected override IntegerVector Cross(IRandom random, ItemArray parents) { return Apply(random, parents, CapacitiesParameter.ActualValue); } } }