#region License Information /* HeuristicLab * Copyright (C) 2002-2010 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 { [StorableClass] public class BestPointInitializer : SingleSuccessorOperator { #region Parameter properties public ScopeTreeLookupParameter QualityParameter { get { return (ScopeTreeLookupParameter)Parameters["Quality"]; } } public LookupParameter BestQualityParameter { get { return (LookupParameter)Parameters["BestQuality"]; } } public ScopeTreeLookupParameter PointParameter { get { return (ScopeTreeLookupParameter)Parameters["Point"]; } } public LookupParameter BestPointParameter { get { return (LookupParameter)Parameters["BestPoint"]; } } public ValueLookupParameter MaximizationParameter { get { return (ValueLookupParameter)Parameters["Maximization"]; } } #endregion #region Parameter values public ItemArray Quality { get { return QualityParameter.ActualValue; } } public double BestQuality { get { return BestQualityParameter.ActualValue.Value; } set { BestQualityParameter.ActualValue = new DoubleValue(value); } } public ItemArray Point { get { return PointParameter.ActualValue; } } public RealVector BestPoint { get { return BestPointParameter.ActualValue; } set { BestPointParameter.ActualValue = value; } } public bool Maximization { get { return MaximizationParameter.ActualValue.Value; } } #endregion #region Construction & Cloning [StorableConstructor] protected BestPointInitializer(bool deserializing) : base(deserializing) { } protected 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(); } public override bool CanChangeName { get { return false; } } } }