#region License Information /* HeuristicLab * Copyright (C) 2002-2014 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.Collections.Generic; 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.Algorithms.ParticleSwarmOptimization { [Item("Multi PSO Topology Updater", "Splits swarm into NrOfSwarms non-overlapping sub-swarms. Swarms are re-grouped every regroupingPeriod iteration. The operator is implemented as described in Liang, J.J. and Suganthan, P.N 2005. Dynamic multi-swarm particle swarm optimizer. IEEE Swarm Intelligence Symposium, pp. 124-129.")] [StorableClass] public sealed class MultiPSOTopologyUpdater : SingleSuccessorOperator, ITopologyUpdater { public override bool CanChangeName { get { return false; } } #region Parameters public ILookupParameter RandomParameter { get { return (ILookupParameter)Parameters["Random"]; } } public IValueLookupParameter NrOfSwarmsParameter { get { return (IValueLookupParameter)Parameters["NrOfSwarms"]; } } public ILookupParameter SwarmSizeParameter { get { return (ILookupParameter)Parameters["SwarmSize"]; } } public IScopeTreeLookupParameter NeighborsParameter { get { return (IScopeTreeLookupParameter)Parameters["Neighbors"]; } } public ILookupParameter CurrentIterationParameter { get { return (ILookupParameter)Parameters["CurrentIteration"]; } } public IValueLookupParameter RegroupingPeriodParameter { get { return (IValueLookupParameter)Parameters["RegroupingPeriod"]; } } #endregion #region Parameter Values private IRandom Random { get { return RandomParameter.ActualValue; } } private int NrOfSwarms { get { return NrOfSwarmsParameter.ActualValue.Value; } } private int SwarmSize { get { return SwarmSizeParameter.ActualValue.Value; } } private ItemArray Neighbors { get { return NeighborsParameter.ActualValue; } set { NeighborsParameter.ActualValue = value; } } private int CurrentIteration { get { return CurrentIterationParameter.ActualValue.Value; } } private int RegroupingPeriod { get { return RegroupingPeriodParameter.ActualValue.Value; } } #endregion #region Construction & Cloning [StorableConstructor] private MultiPSOTopologyUpdater(bool deserializing) : base(deserializing) { } private MultiPSOTopologyUpdater(MultiPSOTopologyUpdater original, Cloner cloner) : base(original, cloner) { } public MultiPSOTopologyUpdater() : base() { Parameters.Add(new LookupParameter("Random", "A random number generator.")); Parameters.Add(new ValueLookupParameter("NrOfSwarms", "Nr of connected sub-swarms.", new IntValue(3))); Parameters.Add(new LookupParameter("SwarmSize", "Number of particles in the swarm.")); Parameters.Add(new ScopeTreeLookupParameter("Neighbors", "The list of neighbors for each particle.")); Parameters.Add(new LookupParameter("CurrentIteration", "The current iteration of the algorithm.")); Parameters.Add(new ValueLookupParameter("RegroupingPeriod", "Update interval (=iterations) for regrouping of neighborhoods.", new IntValue(5))); } public override IDeepCloneable Clone(Cloner cloner) { return new MultiPSOTopologyUpdater(this, cloner); } #endregion public override IOperation Apply() { if (CurrentIteration > 0 && CurrentIteration % RegroupingPeriod == 0) { ItemArray neighbors = new ItemArray(SwarmSize); var particles = Enumerable.Range(0, SwarmSize).ToList(); for (int i = SwarmSize-1; i>0; i--) { int j = Random.Next(i+1); int t = particles[j]; particles[j] = particles[i]; particles[i] = t; } for (int partitionNr = 0; partitionNr list, int start, int end, int excludedIndex) { return new IntArray(list .Skip(start) .Take(end-start) .Where((p, j) => start+j != excludedIndex) .ToArray()); } } }