#region License Information /* HeuristicLab * Copyright (C) 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.Collections.Generic; using System.Linq; using HEAL.Attic; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Optimization; using HeuristicLab.PluginInfrastructure; namespace HeuristicLab.Encodings.LinearLinkageEncoding { [Item("Linear Linkage Encoding", "Describes a linear linkage (LLE) encoding.")] [StorableType("7AE11F39-E6BD-4FC7-8112-0A5EDCBFBDB6")] public sealed class LinearLinkageEncoding : VectorEncoding { [StorableConstructor] private LinearLinkageEncoding(StorableConstructorFlag _) : base(_) { } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { DiscoverOperators(); } public override IDeepCloneable Clone(Cloner cloner) { return new LinearLinkageEncoding(this, cloner); } private LinearLinkageEncoding(LinearLinkageEncoding original, Cloner cloner) : base(original, cloner) { } public LinearLinkageEncoding() : this("LLE", 10) { } public LinearLinkageEncoding(string name) : this(name, 10) { } public LinearLinkageEncoding(int length) : this("LLE", length) { } public LinearLinkageEncoding(string name, int length) : base(name, length) { DiscoverOperators(); } #region Operator Discovery private static readonly IEnumerable encodingSpecificOperatorTypes; static LinearLinkageEncoding() { encodingSpecificOperatorTypes = new List() { typeof (ILinearLinkageOperator), typeof (ILinearLinkageCreator), typeof (ILinearLinkageCrossover), typeof (ILinearLinkageManipulator), typeof (ILinearLinkageShakingOperator), typeof (ILinearLinkageMoveOperator) }; } private void DiscoverOperators() { var assembly = typeof(ILinearLinkageOperator).Assembly; var discoveredTypes = ApplicationManager.Manager.GetTypes(encodingSpecificOperatorTypes, assembly, true, false, false); var operators = discoveredTypes.Select(t => (IOperator)Activator.CreateInstance(t)); var newOperators = operators.Except(Operators, new TypeEqualityComparer()).ToList(); ConfigureOperators(newOperators); foreach (var @operator in newOperators) AddOperator(@operator); } #endregion public override void ConfigureOperators(IEnumerable operators) { base.ConfigureOperators(operators); ConfigureCreators(operators.OfType()); ConfigureCrossovers(operators.OfType()); ConfigureManipulators(operators.OfType()); ConfigureShakingOperators(operators.OfType()); ConfigureMoveOperators(operators.OfType()); ConfigureSwap2MoveOperators(operators.OfType()); } #region specific operator wiring private void ConfigureCreators(IEnumerable creators) { foreach (var creator in creators) { creator.LengthParameter.ActualName = LengthParameter.Name; creator.LLEParameter.ActualName = Name; } } private void ConfigureCrossovers(IEnumerable crossovers) { foreach (var crossover in crossovers) { crossover.ChildParameter.ActualName = Name; crossover.ParentsParameter.ActualName = Name; } } private void ConfigureManipulators(IEnumerable manipulators) { foreach (var manipulator in manipulators) { manipulator.LLEParameter.ActualName = Name; } } private void ConfigureShakingOperators(IEnumerable shakingOperators) { foreach (var shakingOperator in shakingOperators) { shakingOperator.LLEParameter.ActualName = Name; } } private void ConfigureMoveOperators(IEnumerable moveOperators) { foreach (var moveOperator in moveOperators) { moveOperator.LLEParameter.ActualName = Name; } } private void ConfigureSwap2MoveOperators(IEnumerable swap2MoveOperators) { foreach (var swap2MoveOperator in swap2MoveOperators) { swap2MoveOperator.Swap2MoveParameter.ActualName = Name + ".Swap2Move"; } } #endregion } }