Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/MultiPSOTopologyUpdater.cs @ 5560

Last change on this file since 5560 was 5560, checked in by mkofler, 14 years ago

#852: Code refactoring. Created new interfaces and moved operators to respective projects as suggested in A. Beham's review. Work in progress.

File size: 6.2 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.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 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 {
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<IntArray> NeighborsParameter {
51      get { return (IScopeTreeLookupParameter<IntArray>)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<IntArray> 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   
87    public MultiPSOTopologyUpdater()
88      : base() {
89      Parameters.Add(new LookupParameter<IRandom>("Random", "A random number generator."));
90      Parameters.Add(new ValueLookupParameter<IntValue>("NrOfConnections", "Nr of connected neighbors.", new IntValue(3)));
91      Parameters.Add(new LookupParameter<IntValue>("SwarmSize", "Number of particles in the swarm."));
92      Parameters.Add(new ScopeTreeLookupParameter<IntArray>("Neighbors", "The list of neighbors for each particle."));
93      Parameters.Add(new LookupParameter<IntValue>("CurrentIteration", "The current iteration of the algorithm."));
94      Parameters.Add(new ValueLookupParameter<IntValue>("RegroupingPeriod", "Update interval (=iterations) for regrouping of neighborhoods.", new IntValue(5)));
95    }
96
97    public override IDeepCloneable Clone(Cloner cloner) {
98      return new MultiPSOTopologyUpdater(this, cloner);
99    }
100
101    // Splits the swarm into non-overlapping sub swarms
102    public override IOperation Apply() {
103      if (CurrentIteration > 0 && CurrentIteration % RegroupingPeriod == 0) {
104        ItemArray<IntArray> neighbors = new ItemArray<IntArray>(SwarmSize);
105        Dictionary<int, List<int>> neighborsPerParticle = new Dictionary<int, List<int>>();
106        for (int i = 0; i < SwarmSize; i++) {
107          neighborsPerParticle.Add(i, new List<int>());
108        }
109
110        // partition swarm into groups
111        Dictionary<int, List<int>> groups = new Dictionary<int, List<int>>();
112        int groupId = 0;
113        var numbers = Enumerable.Range(0, SwarmSize).ToList();
114        for (int i = 0; i < SwarmSize; i++) {
115          int nextParticle = numbers[Random.Next(0, numbers.Count)];
116          if (!groups.ContainsKey(groupId)) {
117            groups.Add(groupId, new List<int>());
118          }
119          groups[groupId].Add(nextParticle);
120          if (groups[groupId].Count - 1 == NrOfConnections) {
121            groupId++;
122          }
123          numbers.Remove(nextParticle);
124        }
125
126        // add neighbors to each particle
127        foreach (List<int> group in groups.Values) {
128          foreach (int sib1 in group) {
129            foreach (int sib2 in group) {
130              if (sib1 != sib2 && !neighborsPerParticle[sib1].Contains(sib2)) {
131                neighborsPerParticle[sib1].Add(sib2);
132              }
133            }
134          }
135        }
136
137        for (int particle = 0; particle < neighborsPerParticle.Count; particle++) {
138          neighbors[particle] = new IntArray(neighborsPerParticle[particle].ToArray());
139        }
140        Neighbors = neighbors;
141      }
142      return base.Apply();
143    }
144  }
145}
Note: See TracBrowser for help on using the repository browser.