Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/MultiPSOTopologyUpdater.cs @ 5484

Last change on this file since 5484 was 5435, checked in by abeham, 14 years ago

#852

  • Made public properties that redirect to ActualValue.Value private or protected
  • Sealed all of the specific operators
  • Removed files that are present in the repository, but are not included in the project
  • Removed .sln file (is this still needed?)
  • Added license headers to some files
  • Unified the pattern for writing the constructors similar to other files in the trunk
  • Corrected assembly and plugin version from 3.3.0.x to 3.3.2.x
  • Fixed the wiring in the VelocityBoundsModifier
File size: 6.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Encodings.IntegerVectorEncoding;
28using HeuristicLab.Operators;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
33  [Item("Multi PSO Topology Initializer/Updater", "Splits swarm into swarmsize / (nrOfConnections + 1) 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, ITopologyInitializer {
36    public override bool CanChangeName {
37      get { return false; }
38    }
39
40    #region Parameters
41    public ILookupParameter<IRandom> RandomParameter {
42      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
43    }
44    public IValueLookupParameter<IntValue> NrOfConnectionsParameter {
45      get { return (IValueLookupParameter<IntValue>)Parameters["NrOfConnections"]; }
46    }
47    public ILookupParameter<IntValue> SwarmSizeParameter {
48      get { return (ILookupParameter<IntValue>)Parameters["SwarmSize"]; }
49    }
50    public IScopeTreeLookupParameter<IntegerVector> NeighborsParameter {
51      get { return (IScopeTreeLookupParameter<IntegerVector>)Parameters["Neighbors"]; }
52    }
53    public ILookupParameter<IntValue> CurrentIterationParameter {
54      get { return (ILookupParameter<IntValue>)Parameters["CurrentIteration"]; }
55    }
56    public IValueLookupParameter<IntValue> RegroupingPeriodParameter {
57      get { return (IValueLookupParameter<IntValue>)Parameters["RegroupingPeriod"]; }
58    }
59    #endregion
60
61    #region Parameter Values
62    private IRandom Random {
63      get { return RandomParameter.ActualValue; }
64    }
65    private int NrOfConnections {
66      get { return NrOfConnectionsParameter.ActualValue.Value; }
67    }
68    private int SwarmSize {
69      get { return SwarmSizeParameter.ActualValue.Value; }
70    }
71    private ItemArray<IntegerVector> Neighbors {
72      get { return NeighborsParameter.ActualValue; }
73      set { NeighborsParameter.ActualValue = value; }
74    }
75    private int CurrentIteration {
76      get { return CurrentIterationParameter.ActualValue.Value; }
77    }
78    private int RegroupingPeriod {
79      get { return RegroupingPeriodParameter.ActualValue.Value; }
80    }
81    #endregion
82
83    [StorableConstructor]
84    private MultiPSOTopologyUpdater(bool deserializing) : base(deserializing) { }
85    private MultiPSOTopologyUpdater(MultiPSOTopologyUpdater original, Cloner cloner) : base(original, cloner) { }
86    public MultiPSOTopologyUpdater()
87      : base() {
88      Parameters.Add(new LookupParameter<IRandom>("Random", "A random number generator."));
89      Parameters.Add(new ValueLookupParameter<IntValue>("NrOfConnections", "Nr of connected neighbors.", new IntValue(3)));
90      Parameters.Add(new LookupParameter<IntValue>("SwarmSize", "Number of particles in the swarm."));
91      Parameters.Add(new ScopeTreeLookupParameter<IntegerVector>("Neighbors", "The list of neighbors for each particle."));
92      Parameters.Add(new LookupParameter<IntValue>("CurrentIteration", "The current iteration of the algorithm."));
93      Parameters.Add(new ValueLookupParameter<IntValue>("RegroupingPeriod", "Update interval (=iterations) for regrouping of neighborhoods.", new IntValue(5)));
94    }
95
96    public override IDeepCloneable Clone(Cloner cloner) {
97      return new MultiPSOTopologyUpdater(this, cloner);
98    }
99
100    // Splits the swarm into non-overlapping sub swarms
101    public override IOperation Apply() {
102      if (CurrentIteration % RegroupingPeriod == 0) {
103        ItemArray<IntegerVector> neighbors = new ItemArray<IntegerVector>(SwarmSize);
104        Dictionary<int, List<int>> neighborsPerParticle = new Dictionary<int, List<int>>();
105        for (int i = 0; i < SwarmSize; i++) {
106          neighborsPerParticle.Add(i, new List<int>());
107        }
108
109        // partition swarm into groups
110        Dictionary<int, List<int>> groups = new Dictionary<int, List<int>>();
111        int groupId = 0;
112        var numbers = Enumerable.Range(0, SwarmSize).ToList();
113        for (int i = 0; i < SwarmSize; i++) {
114          int nextParticle = numbers[Random.Next(0, numbers.Count)];
115          if (!groups.ContainsKey(groupId)) {
116            groups.Add(groupId, new List<int>());
117          }
118          groups[groupId].Add(nextParticle);
119          if (groups[groupId].Count - 1 == NrOfConnections) {
120            groupId++;
121          }
122          numbers.Remove(nextParticle);
123        }
124
125        // add neighbors to each particle
126        foreach (List<int> group in groups.Values) {
127          foreach (int sib1 in group) {
128            foreach (int sib2 in group) {
129              if (sib1 != sib2 && !neighborsPerParticle[sib1].Contains(sib2)) {
130                neighborsPerParticle[sib1].Add(sib2);
131              }
132            }
133          }
134        }
135
136        for (int particle = 0; particle < neighborsPerParticle.Count; particle++) {
137          neighbors[particle] = new IntegerVector(neighborsPerParticle[particle].ToArray());
138        }
139        Neighbors = neighbors;
140      }
141      return base.Apply();
142    }
143  }
144}
Note: See TracBrowser for help on using the repository browser.