Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2839_HiveProjectManagement/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/MultiPSOTopologyUpdater.cs @ 16605

Last change on this file since 16605 was 16057, checked in by jkarder, 6 years ago

#2839:

File size: 5.6 KB
RevLine 
[5435]1#region License Information
2/* HeuristicLab
[16057]3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[5435]4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
[5339]22using System.Collections.Generic;
23using System.Linq;
[5435]24using HeuristicLab.Common;
[5339]25using HeuristicLab.Core;
[5435]26using HeuristicLab.Data;
[5339]27using HeuristicLab.Operators;
[5560]28using HeuristicLab.Optimization;
[5339]29using HeuristicLab.Parameters;
[5435]30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[5339]31
32namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
[5592]33  [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.")]
[5339]34  [StorableClass]
[15181]35  public sealed class MultiPSOTopologyUpdater : SingleSuccessorOperator, ITopologyUpdater, IStochasticOperator {
[5592]36
[5435]37    public override bool CanChangeName {
38      get { return false; }
39    }
40
[5339]41    #region Parameters
[5410]42    public ILookupParameter<IRandom> RandomParameter {
43      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
[5339]44    }
[5592]45    public IValueLookupParameter<IntValue> NrOfSwarmsParameter {
46      get { return (IValueLookupParameter<IntValue>)Parameters["NrOfSwarms"]; }
[5339]47    }
[5410]48    public ILookupParameter<IntValue> SwarmSizeParameter {
49      get { return (ILookupParameter<IntValue>)Parameters["SwarmSize"]; }
[5339]50    }
[5560]51    public IScopeTreeLookupParameter<IntArray> NeighborsParameter {
52      get { return (IScopeTreeLookupParameter<IntArray>)Parameters["Neighbors"]; }
[5339]53    }
[5410]54    public ILookupParameter<IntValue> CurrentIterationParameter {
55      get { return (ILookupParameter<IntValue>)Parameters["CurrentIteration"]; }
[5339]56    }
[5410]57    public IValueLookupParameter<IntValue> RegroupingPeriodParameter {
58      get { return (IValueLookupParameter<IntValue>)Parameters["RegroupingPeriod"]; }
[5339]59    }
60    #endregion
61
62    #region Parameter Values
[5435]63    private IRandom Random {
[5339]64      get { return RandomParameter.ActualValue; }
65    }
[5592]66    private int NrOfSwarms {
67      get { return NrOfSwarmsParameter.ActualValue.Value; }
[5339]68    }
[5435]69    private int SwarmSize {
[5339]70      get { return SwarmSizeParameter.ActualValue.Value; }
71    }
[5560]72    private ItemArray<IntArray> Neighbors {
[5339]73      get { return NeighborsParameter.ActualValue; }
74      set { NeighborsParameter.ActualValue = value; }
75    }
[5435]76    private int CurrentIteration {
[5339]77      get { return CurrentIterationParameter.ActualValue.Value; }
78    }
[5435]79    private int RegroupingPeriod {
[5339]80      get { return RegroupingPeriodParameter.ActualValue.Value; }
81    }
82    #endregion
83
[5592]84    #region Construction & Cloning
[5435]85    [StorableConstructor]
86    private MultiPSOTopologyUpdater(bool deserializing) : base(deserializing) { }
87    private MultiPSOTopologyUpdater(MultiPSOTopologyUpdater original, Cloner cloner) : base(original, cloner) { }
[5339]88    public MultiPSOTopologyUpdater()
89      : base() {
90      Parameters.Add(new LookupParameter<IRandom>("Random", "A random number generator."));
[5592]91      Parameters.Add(new ValueLookupParameter<IntValue>("NrOfSwarms", "Nr of connected sub-swarms.", new IntValue(3)));
[5339]92      Parameters.Add(new LookupParameter<IntValue>("SwarmSize", "Number of particles in the swarm."));
[5560]93      Parameters.Add(new ScopeTreeLookupParameter<IntArray>("Neighbors", "The list of neighbors for each particle."));
[5339]94      Parameters.Add(new LookupParameter<IntValue>("CurrentIteration", "The current iteration of the algorithm."));
95      Parameters.Add(new ValueLookupParameter<IntValue>("RegroupingPeriod", "Update interval (=iterations) for regrouping of neighborhoods.", new IntValue(5)));
96    }
[5435]97    public override IDeepCloneable Clone(Cloner cloner) {
98      return new MultiPSOTopologyUpdater(this, cloner);
99    }
[5592]100    #endregion
[5435]101
[5339]102    public override IOperation Apply() {
[5560]103      if (CurrentIteration > 0 && CurrentIteration % RegroupingPeriod == 0) {
104        ItemArray<IntArray> neighbors = new ItemArray<IntArray>(SwarmSize);
[5339]105
[5592]106        var particles = Enumerable.Range(0, SwarmSize).ToList();
107        for (int i = SwarmSize-1; i>0; i--) {
108          int j = Random.Next(i+1);
109          int t = particles[j];
110          particles[j] = particles[i];
111          particles[i] = t;
[5339]112        }
113
[5592]114        for (int partitionNr = 0; partitionNr<NrOfSwarms; partitionNr++) {
115          int start = partitionNr*SwarmSize/NrOfSwarms;
116          int end = (partitionNr+1)*SwarmSize/NrOfSwarms;
117          for (int i = start; i<end; i++)
118            neighbors[particles[i]] = GetSegment(particles, start, end, i);
[5339]119        }
120
121        Neighbors = neighbors;
122      }
123      return base.Apply();
124    }
[5592]125
126    public static IntArray GetSegment(IEnumerable<int> list, int start, int end, int excludedIndex) {
127      return new IntArray(list
128        .Skip(start)
129        .Take(end-start)
130        .Where((p, j) => start+j != excludedIndex)
131        .ToArray());
132    }
[5339]133  }
134}
Note: See TracBrowser for help on using the repository browser.