#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;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HeuristicLab.Operators;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.RealVectorEncoding;
using HeuristicLab.Parameters;
namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
public class SwarmUpdater : SingleSuccessorOperator {
#region Parameter properties
public ILookupParameter CurrentQualityParameter {
get { return (ILookupParameter)Parameters["CurrentQuality"]; }
}
public ILookupParameter LocalBestQualityParameter {
get { return (ILookupParameter)Parameters["LocalBestQuality"]; }
}
public ILookupParameter GlobalBestQualityParameter {
get { return (ILookupParameter)Parameters["GlobalBestQuality"]; }
}
public ILookupParameter CurrentPositionParameter {
get { return (ILookupParameter)Parameters["CurrentPosition"]; }
}
public ILookupParameter BestLocalParameter {
get { return (ILookupParameter)Parameters["BestLocal"]; }
}
public ILookupParameter BestGlobalParameter {
get { return (ILookupParameter)Parameters["BestGlobal"]; }
}
public ValueLookupParameter MaximizationParameter {
get { return (ValueLookupParameter)Parameters["Maximization"]; }
}
#endregion
public SwarmUpdater()
: base() {
Parameters.Add(new LookupParameter("CurrentPosition", "Current position"));
Parameters.Add(new LookupParameter("BestLocal", "Best local position"));
Parameters.Add(new LookupParameter("BestGlobal", "Best global position"));
Parameters.Add(new LookupParameter("LocalBestQuality", "Best local quality"));
Parameters.Add(new LookupParameter("GlobalBestQuality", "Best global quality"));
Parameters.Add(new LookupParameter("CurrentQuality", "Current quality"));
Parameters.Add(new ValueLookupParameter("Maximization", "True if the problem is a maximization problem, otherwise false."));
}
public override IOperation Apply() {
if (MaximizationParameter.ActualValue.Value) {
if (CurrentQualityParameter.ActualValue.Value > LocalBestQualityParameter.ActualValue.Value) {
LocalBestQualityParameter.ActualValue.Value = CurrentQualityParameter.ActualValue.Value;
BestLocalParameter.ActualValue = (RealVector) CurrentPositionParameter.ActualValue.Clone();
}
if (CurrentQualityParameter.ActualValue.Value > GlobalBestQualityParameter.ActualValue.Value) {
GlobalBestQualityParameter.ActualValue.Value = CurrentQualityParameter.ActualValue.Value;
BestGlobalParameter.ActualValue = (RealVector)CurrentPositionParameter.ActualValue.Clone();
}
} else {
if (CurrentQualityParameter.ActualValue.Value < LocalBestQualityParameter.ActualValue.Value) {
LocalBestQualityParameter.ActualValue.Value = CurrentQualityParameter.ActualValue.Value;
BestLocalParameter.ActualValue = (RealVector)CurrentPositionParameter.ActualValue.Clone();
}
if (CurrentQualityParameter.ActualValue.Value < GlobalBestQualityParameter.ActualValue.Value) {
GlobalBestQualityParameter.ActualValue.Value = CurrentQualityParameter.ActualValue.Value;
BestGlobalParameter.ActualValue = (RealVector)CurrentPositionParameter.ActualValue.Clone();
}
}
return base.Apply();
}
public override bool CanChangeName {
get { return false; }
}
}
}