#region License Information /* HeuristicLab * Copyright (C) 2002-2019 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.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Operators; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.PluginInfrastructure; namespace HeuristicLab.Encodings.RealVectorEncoding { [Item("RealVectorParticleCreator", "Creates a particle with position, zero velocity vector and personal best.")] [StorableClass] [NonDiscoverableType] [Obsolete("Use SPSOParticleCreator")] internal class RealVectorParticleCreator : AlgorithmOperator, IRealVectorParticleCreator { #region Parameters public ILookupParameter ProblemSizeParameter { get { return (ILookupParameter)Parameters["ProblemSize"]; } } public IValueLookupParameter BoundsParameter { get { return (IValueLookupParameter)Parameters["Bounds"]; } } public ILookupParameter RealVectorParameter { get { return (ILookupParameter)Parameters["RealVector"]; } } public ILookupParameter PersonalBestParameter { get { return (ILookupParameter)Parameters["PersonalBest"]; } } public ILookupParameter VelocityParameter { get { return (ILookupParameter)Parameters["Velocity"]; } } public ILookupParameter SolutionCreatorParameter { get { return (ILookupParameter)Parameters["SolutionCreator"]; } } #endregion #region Parameter Values protected int ProblemSize { get { return ProblemSizeParameter.ActualValue.Value; } } protected RealVector Velocity { set { VelocityParameter.ActualValue = value; } } #endregion #region Construction & Cloning [StorableConstructor] protected RealVectorParticleCreator(bool deserializing) : base(deserializing) { } protected RealVectorParticleCreator(RealVectorParticleCreator original, Cloner cloner) : base(original, cloner) { } public RealVectorParticleCreator() : base() { Parameters.Add(new LookupParameter("ProblemSize", "The dimension of the problem.")); Parameters.Add(new ValueLookupParameter("Bounds", "The lower and upper bounds in each dimension.")); Parameters.Add(new LookupParameter("RealVector", "Particle's current solution")); Parameters.Add(new LookupParameter("PersonalBest", "Particle's personal best solution.")); Parameters.Add(new LookupParameter("Velocity", "Particle's current velocity.")); Parameters.Add(new LookupParameter("SolutionCreator", "The operator that creates the initial position.")); UniformRandomRealVectorCreator realVectorCreater = new UniformRandomRealVectorCreator(); Assigner personalBestPositionAssigner = new Assigner(); OperatorGraph.InitialOperator = realVectorCreater; realVectorCreater.RealVectorParameter.ActualName = RealVectorParameter.Name; realVectorCreater.LengthParameter.ActualName = ProblemSizeParameter.Name; realVectorCreater.BoundsParameter.ActualName = BoundsParameter.Name; realVectorCreater.Successor = personalBestPositionAssigner; personalBestPositionAssigner.LeftSideParameter.ActualName = PersonalBestParameter.Name; personalBestPositionAssigner.RightSideParameter.ActualName = RealVectorParameter.Name; personalBestPositionAssigner.Successor = null; } public override IDeepCloneable Clone(Cloner cloner) { return new RealVectorParticleCreator(this, cloner); } #endregion public override IOperation Apply() { Velocity = new RealVector(ProblemSize); return base.Apply(); } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { if (!Parameters.ContainsKey("SolutionCreator")) Parameters.Add(new LookupParameter("SolutionCreator", "The operator that creates the initial position.")); } } }