Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2877_HiveImprovements/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/MultiPSOTopologyUpdater.cs @ 16688

Last change on this file since 16688 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 5.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
22using System.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
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.")]
34  [StorableClass]
35  public sealed class MultiPSOTopologyUpdater : SingleSuccessorOperator, ITopologyUpdater, IStochasticOperator {
36
37    public override bool CanChangeName {
38      get { return false; }
39    }
40
41    #region Parameters
42    public ILookupParameter<IRandom> RandomParameter {
43      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
44    }
45    public IValueLookupParameter<IntValue> NrOfSwarmsParameter {
46      get { return (IValueLookupParameter<IntValue>)Parameters["NrOfSwarms"]; }
47    }
48    public ILookupParameter<IntValue> SwarmSizeParameter {
49      get { return (ILookupParameter<IntValue>)Parameters["SwarmSize"]; }
50    }
51    public IScopeTreeLookupParameter<IntArray> NeighborsParameter {
52      get { return (IScopeTreeLookupParameter<IntArray>)Parameters["Neighbors"]; }
53    }
54    public ILookupParameter<IntValue> CurrentIterationParameter {
55      get { return (ILookupParameter<IntValue>)Parameters["CurrentIteration"]; }
56    }
57    public IValueLookupParameter<IntValue> RegroupingPeriodParameter {
58      get { return (IValueLookupParameter<IntValue>)Parameters["RegroupingPeriod"]; }
59    }
60    #endregion
61
62    #region Parameter Values
63    private IRandom Random {
64      get { return RandomParameter.ActualValue; }
65    }
66    private int NrOfSwarms {
67      get { return NrOfSwarmsParameter.ActualValue.Value; }
68    }
69    private int SwarmSize {
70      get { return SwarmSizeParameter.ActualValue.Value; }
71    }
72    private ItemArray<IntArray> Neighbors {
73      get { return NeighborsParameter.ActualValue; }
74      set { NeighborsParameter.ActualValue = value; }
75    }
76    private int CurrentIteration {
77      get { return CurrentIterationParameter.ActualValue.Value; }
78    }
79    private int RegroupingPeriod {
80      get { return RegroupingPeriodParameter.ActualValue.Value; }
81    }
82    #endregion
83
84    #region Construction & Cloning
85    [StorableConstructor]
86    private MultiPSOTopologyUpdater(bool deserializing) : base(deserializing) { }
87    private MultiPSOTopologyUpdater(MultiPSOTopologyUpdater original, Cloner cloner) : base(original, cloner) { }
88    public MultiPSOTopologyUpdater()
89      : base() {
90      Parameters.Add(new LookupParameter<IRandom>("Random", "A random number generator."));
91      Parameters.Add(new ValueLookupParameter<IntValue>("NrOfSwarms", "Nr of connected sub-swarms.", new IntValue(3)));
92      Parameters.Add(new LookupParameter<IntValue>("SwarmSize", "Number of particles in the swarm."));
93      Parameters.Add(new ScopeTreeLookupParameter<IntArray>("Neighbors", "The list of neighbors for each particle."));
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    }
97    public override IDeepCloneable Clone(Cloner cloner) {
98      return new MultiPSOTopologyUpdater(this, cloner);
99    }
100    #endregion
101
102    public override IOperation Apply() {
103      if (CurrentIteration > 0 && CurrentIteration % RegroupingPeriod == 0) {
104        ItemArray<IntArray> neighbors = new ItemArray<IntArray>(SwarmSize);
105
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;
112        }
113
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);
119        }
120
121        Neighbors = neighbors;
122      }
123      return base.Apply();
124    }
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    }
133  }
134}
Note: See TracBrowser for help on using the repository browser.