#region License Information /* HeuristicLab * Copyright (C) 2002-2011 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 HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.RealVectorEncoding; using HeuristicLab.Operators; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Algorithms.ParticleSwarmOptimization { [Item("Swarm Updater", "Updates personal best point and quality as well as global best point and quality.")] [StorableClass] public sealed class SwarmUpdater : SingleSuccessorOperator { public override bool CanChangeName { get { return false; } } #region Parameter properties public ILookupParameter QualityParameter { get { return (ILookupParameter)Parameters["Quality"]; } } public ILookupParameter PersonalBestQualityParameter { get { return (ILookupParameter)Parameters["PersonalBestQuality"]; } } public ILookupParameter BestQualityParameter { get { return (ILookupParameter)Parameters["BestQuality"]; } } public ILookupParameter PointParameter { get { return (ILookupParameter)Parameters["Point"]; } } public ILookupParameter PersonalBestPointParameter { get { return (ILookupParameter)Parameters["PersonalBestPoint"]; } } public ILookupParameter BestPointParameter { get { return (ILookupParameter)Parameters["BestPoint"]; } } public IValueLookupParameter MaximizationParameter { get { return (IValueLookupParameter)Parameters["Maximization"]; } } #endregion #region Parameter values private double Quality { get { return QualityParameter.ActualValue.Value; } } private double PersonalBestQuality { get { return PersonalBestQualityParameter.ActualValue.Value; } set { PersonalBestQualityParameter.ActualValue = new DoubleValue(value); } } private double BestQuality { get { return BestQualityParameter.ActualValue.Value; } set { BestQualityParameter.ActualValue = new DoubleValue(value); } } private RealVector Point { get { return PointParameter.ActualValue; } } private RealVector PersonalBestPoint { get { return PersonalBestPointParameter.ActualValue; } set { PersonalBestPointParameter.ActualValue = value; } } private RealVector BestPoint { get { return BestPointParameter.ActualValue; } set { BestPointParameter.ActualValue = value; } } private bool Maximization { get { return MaximizationParameter.ActualValue.Value; } } #endregion #region Construction & Cloning [StorableConstructor] private SwarmUpdater(bool deserializing) : base(deserializing) { } private SwarmUpdater(SwarmUpdater original, Cloner cloner) : base(original, cloner) { } public SwarmUpdater() : base() { Parameters.Add(new LookupParameter("Quality", "Particle's quality")); Parameters.Add(new LookupParameter("PersonalBestQuality", "Particle's personal best quality")); Parameters.Add(new LookupParameter("BestQuality", "Global best particle quality")); Parameters.Add(new LookupParameter("Point", "Particle's position")); Parameters.Add(new LookupParameter("PersonalBestPoint", "Particle's personal best position")); Parameters.Add(new LookupParameter("BestPoint", "Globa best particle position")); Parameters.Add(new ValueLookupParameter("Maximization", "True if the problem is a maximization problem, otherwise false.")); } public override IDeepCloneable Clone(Cloner cloner) { return new SwarmUpdater(this, cloner); } #endregion public override IOperation Apply() { if (Maximization && Quality > PersonalBestQuality || !Maximization && Quality < PersonalBestQuality) { PersonalBestQuality = Quality; PersonalBestPoint = Point; if (Maximization && PersonalBestQuality > BestQuality || !Maximization && PersonalBestQuality < BestQuality) { BestQuality = PersonalBestQuality; BestPoint = PersonalBestPoint; } } return base.Apply(); } } }