1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
7 | using HeuristicLab.Operators;
|
---|
8 | using HeuristicLab.Common;
|
---|
9 | using HeuristicLab.Parameters;
|
---|
10 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
11 | using HeuristicLab.Data;
|
---|
12 |
|
---|
13 | namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
|
---|
14 | [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")]
|
---|
15 | [StorableClass]
|
---|
16 | public class MultiPSOTopologyUpdater : SingleSuccessorOperator, ITopologyUpdater, ITopologyInitializer {
|
---|
17 | #region Parameters
|
---|
18 | public LookupParameter<IRandom> RandomParameter {
|
---|
19 | get { return (LookupParameter<IRandom>)Parameters["Random"]; }
|
---|
20 | }
|
---|
21 | public ValueLookupParameter<IntValue> NrOfConnectionsParameter {
|
---|
22 | get { return (ValueLookupParameter<IntValue>)Parameters["NrOfConnections"]; }
|
---|
23 | }
|
---|
24 | public LookupParameter<IntValue> SwarmSizeParameter {
|
---|
25 | get { return (LookupParameter<IntValue>)Parameters["SwarmSize"]; }
|
---|
26 | }
|
---|
27 | public ScopeTreeLookupParameter<IntegerVector> NeighborsParameter {
|
---|
28 | get { return (ScopeTreeLookupParameter<IntegerVector>)Parameters["Neighbors"]; }
|
---|
29 | }
|
---|
30 | public LookupParameter<IntValue> CurrentIterationParameter {
|
---|
31 | get { return (LookupParameter<IntValue>)Parameters["CurrentIteration"]; }
|
---|
32 | }
|
---|
33 | public ValueLookupParameter<IntValue> RegroupingPeriodParameter {
|
---|
34 | get { return (ValueLookupParameter<IntValue>)Parameters["RegroupingPeriod"]; }
|
---|
35 | }
|
---|
36 | #endregion
|
---|
37 |
|
---|
38 | #region Parameter Values
|
---|
39 | public IRandom Random {
|
---|
40 | get { return RandomParameter.ActualValue; }
|
---|
41 | }
|
---|
42 | public int NrOfConnections {
|
---|
43 | get { return NrOfConnectionsParameter.ActualValue.Value; }
|
---|
44 | }
|
---|
45 | public int SwarmSize {
|
---|
46 | get { return SwarmSizeParameter.ActualValue.Value; }
|
---|
47 | }
|
---|
48 | public ItemArray<IntegerVector> Neighbors {
|
---|
49 | get { return NeighborsParameter.ActualValue; }
|
---|
50 | set { NeighborsParameter.ActualValue = value; }
|
---|
51 | }
|
---|
52 | public int CurrentIteration {
|
---|
53 | get { return CurrentIterationParameter.ActualValue.Value; }
|
---|
54 | }
|
---|
55 | public int RegroupingPeriod {
|
---|
56 | get { return RegroupingPeriodParameter.ActualValue.Value; }
|
---|
57 | }
|
---|
58 | #endregion
|
---|
59 |
|
---|
60 | public MultiPSOTopologyUpdater()
|
---|
61 | : base() {
|
---|
62 | Parameters.Add(new LookupParameter<IRandom>("Random", "A random number generator."));
|
---|
63 | Parameters.Add(new ValueLookupParameter<IntValue>("NrOfConnections", "Nr of connected neighbors.", new IntValue(3)));
|
---|
64 | Parameters.Add(new LookupParameter<IntValue>("SwarmSize", "Number of particles in the swarm."));
|
---|
65 | Parameters.Add(new ScopeTreeLookupParameter<IntegerVector>("Neighbors", "The list of neighbors for each particle."));
|
---|
66 | Parameters.Add(new LookupParameter<IntValue>("CurrentIteration", "The current iteration of the algorithm."));
|
---|
67 | Parameters.Add(new ValueLookupParameter<IntValue>("RegroupingPeriod", "Update interval (=iterations) for regrouping of neighborhoods.", new IntValue(5)));
|
---|
68 | }
|
---|
69 |
|
---|
70 | // Splits the swarm into non-overlapping sub swarms
|
---|
71 | public override IOperation Apply() {
|
---|
72 | if (CurrentIteration % RegroupingPeriod == 0) {
|
---|
73 | ItemArray<IntegerVector> neighbors = new ItemArray<IntegerVector>(SwarmSize);
|
---|
74 | Dictionary<int, List<int>> neighborsPerParticle = new Dictionary<int, List<int>>();
|
---|
75 | for (int i = 0; i < SwarmSize; i++) {
|
---|
76 | neighborsPerParticle.Add(i, new List<int>());
|
---|
77 | }
|
---|
78 |
|
---|
79 | // partition swarm into groups
|
---|
80 | Dictionary<int, List<int>> groups = new Dictionary<int, List<int>>();
|
---|
81 | int groupId = 0;
|
---|
82 | var numbers = Enumerable.Range(0, SwarmSize).ToList();
|
---|
83 | for (int i = 0; i < SwarmSize; i++) {
|
---|
84 | int nextParticle = numbers[Random.Next(0, numbers.Count)];
|
---|
85 | if (!groups.ContainsKey(groupId)) {
|
---|
86 | groups.Add(groupId, new List<int>());
|
---|
87 | }
|
---|
88 | groups[groupId].Add(nextParticle);
|
---|
89 | if (groups[groupId].Count - 1 == NrOfConnections) {
|
---|
90 | groupId++;
|
---|
91 | }
|
---|
92 | numbers.Remove(nextParticle);
|
---|
93 | }
|
---|
94 |
|
---|
95 | // add neighbors to each particle
|
---|
96 | foreach (List<int> group in groups.Values) {
|
---|
97 | foreach (int sib1 in group) {
|
---|
98 | foreach (int sib2 in group) {
|
---|
99 | if (sib1 != sib2 && !neighborsPerParticle[sib1].Contains(sib2)) {
|
---|
100 | neighborsPerParticle[sib1].Add(sib2);
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | for (int particle = 0; particle < neighborsPerParticle.Count; particle++) {
|
---|
107 | neighbors[particle] = new IntegerVector(neighborsPerParticle[particle].ToArray());
|
---|
108 | }
|
---|
109 | Neighbors = neighbors;
|
---|
110 | }
|
---|
111 | return base.Apply();
|
---|
112 | }
|
---|
113 |
|
---|
114 | [StorableConstructor]
|
---|
115 | protected MultiPSOTopologyUpdater(bool deserializing) : base(deserializing) { }
|
---|
116 |
|
---|
117 | #region Cloning
|
---|
118 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
119 | return new MultiPSOTopologyUpdater(this, cloner);
|
---|
120 | }
|
---|
121 |
|
---|
122 | protected MultiPSOTopologyUpdater(MultiPSOTopologyUpdater original, Cloner cloner)
|
---|
123 | : base(original, cloner) {
|
---|
124 | }
|
---|
125 | #endregion
|
---|
126 | }
|
---|
127 | }
|
---|