#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.Operators;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Encodings.RealVectorEncoding {
[Item("Swarm Updater", "Updates personal best point and quality as well as global best point and quality.")]
[StorableClass]
public sealed class RealVectorSwarmUpdater : SingleSuccessorOperator, IRealVectorSwarmUpdater {
public override bool CanChangeName {
get { return false; }
}
#region Parameter properties
public IScopeTreeLookupParameter QualityParameter {
get { return (IScopeTreeLookupParameter)Parameters["Quality"]; }
}
public IScopeTreeLookupParameter PersonalBestQualityParameter {
get { return (IScopeTreeLookupParameter)Parameters["PersonalBestQuality"]; }
}
public IScopeTreeLookupParameter NeighborBestQualityParameter {
get { return (IScopeTreeLookupParameter)Parameters["NeighborBestQuality"]; }
}
public IScopeTreeLookupParameter RealVectorParameter {
get { return (IScopeTreeLookupParameter)Parameters["RealVector"]; }
}
public IScopeTreeLookupParameter PersonalBestParameter {
get { return (IScopeTreeLookupParameter)Parameters["PersonalBest"]; }
}
public IScopeTreeLookupParameter NeighborBestParameter {
get { return (IScopeTreeLookupParameter)Parameters["NeighborBest"]; }
}
public ILookupParameter MaximizationParameter {
get { return (ILookupParameter)Parameters["Maximization"]; }
}
public ILookupParameter BestQualityParameter {
get { return (ILookupParameter)Parameters["BestQuality"]; }
}
public ILookupParameter BestRealVectorParameter {
get { return (ILookupParameter)Parameters["BestRealVector"]; }
}
public IScopeTreeLookupParameter NeighborsParameter {
get { return (IScopeTreeLookupParameter)Parameters["Neighbors"]; }
}
public ValueLookupParameter VelocityBoundsUpdaterParameter {
get { return (ValueLookupParameter)Parameters["VelocityBoundsUpdater"]; }
}
public LookupParameter VelocityBoundsParameter {
get { return (LookupParameter)Parameters["VelocityBounds"]; }
}
#endregion
#region Parameter values
private DoubleValue BestQuality {
get { return BestQualityParameter.ActualValue; }
set { BestQualityParameter.ActualValue = value; }
}
private RealVector BestRealVector {
get { return BestRealVectorParameter.ActualValue; }
set { BestRealVectorParameter.ActualValue = value; }
}
private ItemArray Quality {
get { return QualityParameter.ActualValue; }
}
private ItemArray PersonalBestQuality {
get { return PersonalBestQualityParameter.ActualValue; }
set { PersonalBestQualityParameter.ActualValue = value; }
}
private ItemArray NeighborBestQuality {
get { return NeighborBestQualityParameter.ActualValue; }
set { NeighborBestQualityParameter.ActualValue = value; }
}
private ItemArray RealVector {
get { return RealVectorParameter.ActualValue; }
}
private ItemArray PersonalBest {
get { return PersonalBestParameter.ActualValue; }
set { PersonalBestParameter.ActualValue = value; }
}
private ItemArray NeighborBest {
get { return NeighborBestParameter.ActualValue; }
set { NeighborBestParameter.ActualValue = value; }
}
private bool Maximization {
get { return MaximizationParameter.ActualValue.Value; }
}
private ItemArray Neighbors {
get { return NeighborsParameter.ActualValue; }
}
private IDiscreteDoubleMatrixModifier VelocityBoundsUpdater {
get { return VelocityBoundsUpdaterParameter.ActualValue; }
}
private DoubleMatrix VelocityBounds {
get { return VelocityBoundsParameter.ActualValue; }
set { VelocityBoundsParameter.ActualValue = value; }
}
#endregion
#region Construction & Cloning
[StorableConstructor]
private RealVectorSwarmUpdater(bool deserializing) : base(deserializing) { }
private RealVectorSwarmUpdater(RealVectorSwarmUpdater original, Cloner cloner) : base(original, cloner) { }
public RealVectorSwarmUpdater()
: base() {
Parameters.Add(new LookupParameter("BestQuality", "Overall best quality."));
Parameters.Add(new LookupParameter("BestRealVector", "Global best particle position"));
Parameters.Add(new ScopeTreeLookupParameter("Quality", "Particle's quality"));
Parameters.Add(new ScopeTreeLookupParameter("PersonalBestQuality", "Particle's personal best quality"));
Parameters.Add(new ScopeTreeLookupParameter("NeighborBestQuality", "Global best particle quality"));
Parameters.Add(new ScopeTreeLookupParameter("RealVector", "Particle's position"));
Parameters.Add(new ScopeTreeLookupParameter("PersonalBest", "Particle's personal best position"));
Parameters.Add(new ScopeTreeLookupParameter("NeighborBest", "Neighborhood (or global in case of totally connected neighborhood) best particle position"));
Parameters.Add(new ScopeTreeLookupParameter("Neighbors", "The list of neighbors for each particle."));
Parameters.Add(new LookupParameter("Maximization", "True if the problem is a maximization problem, otherwise false."));
Parameters.Add(new ValueLookupParameter("VelocityBoundsUpdater", "Modifies the velocity bounds in the course of optimization."));
Parameters.Add(new LookupParameter("VelocityBounds", "Maximum velocity for each dimension."));
}
public override IDeepCloneable Clone(Cloner cloner) {
return new RealVectorSwarmUpdater(this, cloner);
}
#endregion
public override IOperation Apply() {
UpdateGlobalBest();
UpdateNeighborBest();
UpdatePersonalBest();
return UpdateVelocityBounds();
}
private void UpdateGlobalBest() {
if (BestQuality == null)
BestQuality = new DoubleValue();
BestQuality.Value = Maximization ? Quality.Max(v => v.Value) : Quality.Min(v => v.Value);
BestRealVector = (RealVector)RealVector[Quality.FindIndex(v => v.Value == BestQuality.Value)].Clone();
}
private void UpdateNeighborBest() {
if (Neighbors.Length > 0) {
var neighborBest = new ItemArray(Neighbors.Length);
var neighborBestQuality = new ItemArray(Neighbors.Length);
for (int n = 0; n < Neighbors.Length; n++) {
var pairs = Quality.Zip(RealVector, (q, p) => new { Quality = q, Point = p })
.Where((p, i) => i == n || Neighbors[n].Contains(i));
var bestNeighbor = Maximization ?
pairs.OrderByDescending(p => p.Quality.Value).First() :
pairs.OrderBy(p => p.Quality.Value).First();
neighborBest[n] = bestNeighbor.Point;
neighborBestQuality[n] = bestNeighbor.Quality;
}
NeighborBest = neighborBest;
NeighborBestQuality = neighborBestQuality;
}
}
private void UpdatePersonalBest() {
if (PersonalBestQuality.Length == 0)
PersonalBestQuality = (ItemArray)Quality.Clone();
for (int i = 0; i < RealVector.Length; i++) {
if (Maximization && Quality[i].Value > PersonalBestQuality[i].Value ||
!Maximization && Quality[i].Value < PersonalBestQuality[i].Value) {
PersonalBestQuality[i].Value = Quality[i].Value;
PersonalBest[i] = RealVector[i];
}
}
}
private IOperation UpdateVelocityBounds() {
if (VelocityBounds == null)
VelocityBounds = new DoubleMatrix(new double[,] { { -1, 1 } });
return VelocityBoundsUpdater == null ?
base.Apply() :
new OperationCollection() {
ExecutionContext.CreateChildOperation(VelocityBoundsUpdater),
ExecutionContext.CreateOperation(Successor)
};
}
}
}