#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 System.Linq; 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("Best Point Initializer", "Determines the best quality and point of the current particle population.")] [StorableClass] public sealed class BestPointInitializer : SingleSuccessorOperator { public override bool CanChangeName { get { return false; } } #region Parameter properties public IScopeTreeLookupParameter QualityParameter { get { return (IScopeTreeLookupParameter)Parameters["Quality"]; } } public ILookupParameter BestQualityParameter { get { return (ILookupParameter)Parameters["BestQuality"]; } } public IScopeTreeLookupParameter PointParameter { get { return (IScopeTreeLookupParameter)Parameters["Point"]; } } public ILookupParameter BestPointParameter { get { return (ILookupParameter)Parameters["BestPoint"]; } } public IValueLookupParameter MaximizationParameter { get { return (IValueLookupParameter)Parameters["Maximization"]; } } #endregion #region Parameter values private ItemArray Quality { get { return QualityParameter.ActualValue; } } private double BestQuality { get { return BestQualityParameter.ActualValue.Value; } set { BestQualityParameter.ActualValue = new DoubleValue(value); } } private ItemArray Point { get { return PointParameter.ActualValue; } } 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 BestPointInitializer(bool deserializing) : base(deserializing) { } private BestPointInitializer(BestPointInitializer original, Cloner cloner) : base(original, cloner) { } public BestPointInitializer() : base() { Parameters.Add(new ScopeTreeLookupParameter("Quality", "Particle's quality")); Parameters.Add(new LookupParameter("BestQuality", "Global best particle quality")); Parameters.Add(new ScopeTreeLookupParameter("Point", "Particle's 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 BestPointInitializer(this, cloner); } #endregion public override IOperation Apply() { BestQuality = Maximization ? Quality.Max(v => v.Value) : Quality.Min(v => v.Value); int bestIndex = Quality.FindIndex(v => v.Value == BestQuality); BestPoint = (RealVector)Point[bestIndex].Clone(); return base.Apply(); } } }