#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.Parameters; using HeuristicLab.Encodings.RealVectorEncoding; using HeuristicLab.Data; using HeuristicLab.Core; namespace HeuristicLab.Algorithms.ParticleSwarmOptimization { public class ParticleUpdater : SingleSuccessorOperator { // ParticleUpdater #region Parameter properties public ILookupParameter VelocityParameter { get { return (ILookupParameter)Parameters["Velocity"]; } } public IParameter RandomParameter { get { return (IParameter)Parameters["Random"]; } } 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 ILookupParameter BoundsParameter { get { return (ILookupParameter)Parameters["Bounds"]; } } #endregion public ParticleUpdater() : base() { Parameters.Add(new LookupParameter("Random", "Random number generator (to generate alpha an beta).")); Parameters.Add(new LookupParameter("Velocity", "The velocity vector to update.")); 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("Bounds", "The lower and upper bounds for each dimension of the position vector.")); } public override IOperation Apply() { double alpha = ((IRandom)RandomParameter.ActualValue).NextDouble(); double beta = ((IRandom)RandomParameter.ActualValue).NextDouble(); RealVector velocity = (RealVector) VelocityParameter.ActualValue; for (int i = 0; i < velocity.Length; i++) { velocity[i] = velocity[i] + alpha * (BestLocalParameter.ActualValue[i] - CurrentPositionParameter.ActualValue[i]) + beta * (BestGlobalParameter.ActualValue[i] - CurrentPositionParameter.ActualValue[i]); } VelocityParameter.ActualValue = velocity; for (int i = 0; i < CurrentPositionParameter.ActualValue.Length; i++) { CurrentPositionParameter.ActualValue[i] = CurrentPositionParameter.ActualValue[i] + VelocityParameter.ActualValue[i]; if (CurrentPositionParameter.ActualValue[i] < BoundsParameter.ActualValue[0,0]) { CurrentPositionParameter.ActualValue[i] = BoundsParameter.ActualValue[0, 0]; } else if (CurrentPositionParameter.ActualValue[i] > BoundsParameter.ActualValue[0,1]) { CurrentPositionParameter.ActualValue[i] = BoundsParameter.ActualValue[0, 1]; } } return base.Apply(); } public override bool CanChangeName { get { return false; } } } }