#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.IntegerVectorEncoding; using HeuristicLab.Encodings.RealVectorEncoding; using HeuristicLab.Operators; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Algorithms.ParticleSwarmOptimization { [Item("Neighbor Updater", "Determines the best quality and point among the defined list of neigbors for every particle and its individual neighborhood.")] [StorableClass] public class NeighborUpdater : SingleSuccessorOperator { #region Parameters public IScopeTreeLookupParameter PointsParameter { get { return (IScopeTreeLookupParameter)Parameters["Point"]; } } public IScopeTreeLookupParameter NeighborsParameter { get { return (IScopeTreeLookupParameter)Parameters["Neighbors"]; } } public IScopeTreeLookupParameter BestNeighborPointParameter { get { return (IScopeTreeLookupParameter)Parameters["BestNeighborPoint"]; } } public IScopeTreeLookupParameter QualityParameter { get { return (IScopeTreeLookupParameter)Parameters["Quality"]; } } public ILookupParameter MaximizationParameter { get { return (ILookupParameter)Parameters["Maximization"]; } } #endregion #region Parameter Values public ItemArray Points { get { return PointsParameter.ActualValue; } } public ItemArray Neighbors { get { return NeighborsParameter.ActualValue; } } public ItemArray BestNeighborPoints { get { return BestNeighborPointParameter.ActualValue; } set { BestNeighborPointParameter.ActualValue = value; } } public ItemArray Qualities { get { return QualityParameter.ActualValue; } } public bool Maximization { get { return MaximizationParameter.ActualValue.Value; } } #endregion #region Construction & Cloning public NeighborUpdater() { Parameters.Add(new ScopeTreeLookupParameter("Point", "The position of the particle.")); Parameters.Add(new ScopeTreeLookupParameter("Neighbors", "The list of neighbors for each particle.")); Parameters.Add(new ScopeTreeLookupParameter("BestNeighborPoint", "The position of the best neighboring particle.")); Parameters.Add(new ScopeTreeLookupParameter("Quality", "The list of qualities of all particles.")); Parameters.Add(new LookupParameter("Maximization", "Whether the problem is a maximization problem.")); } [StorableConstructor] protected NeighborUpdater(bool deserializing) : base(deserializing) { } protected NeighborUpdater(NeighborUpdater original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new NeighborUpdater(this, cloner); } #endregion public override IOperation Apply() { if (Neighbors != null & Neighbors.Length > 0) { if (BestNeighborPoints == null || BestNeighborPoints.Length != Neighbors.Length) BestNeighborPoints = new ItemArray(Neighbors.Length); for (int n = 0; n new { Quality = q, Point = p }) .Where((p, i) => i == n || Neighbors[n].Contains(i)); BestNeighborPoints[n] = Maximization ? pairs.OrderByDescending(p => p.Quality.Value).First().Point : pairs.OrderBy(p => p.Quality.Value).First().Point; } BestNeighborPointParameter.ActualValue = BestNeighborPoints; } return base.Apply(); } } }