- Timestamp:
- 03/02/11 15:22:54 (14 years ago)
- Location:
- trunk/sources/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified trunk/sources/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/MultiPSOTopologyUpdater.cs ¶
r5560 r5592 31 31 32 32 namespace 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.")]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 34 [StorableClass] 35 35 public sealed class MultiPSOTopologyUpdater : SingleSuccessorOperator, ITopologyUpdater { 36 36 37 public override bool CanChangeName { 37 38 get { return false; } … … 42 43 get { return (ILookupParameter<IRandom>)Parameters["Random"]; } 43 44 } 44 public IValueLookupParameter<IntValue> NrOf ConnectionsParameter {45 get { return (IValueLookupParameter<IntValue>)Parameters["NrOf Connections"]; }45 public IValueLookupParameter<IntValue> NrOfSwarmsParameter { 46 get { return (IValueLookupParameter<IntValue>)Parameters["NrOfSwarms"]; } 46 47 } 47 48 public ILookupParameter<IntValue> SwarmSizeParameter { … … 63 64 get { return RandomParameter.ActualValue; } 64 65 } 65 private int NrOf Connections {66 get { return NrOf ConnectionsParameter.ActualValue.Value; }66 private int NrOfSwarms { 67 get { return NrOfSwarmsParameter.ActualValue.Value; } 67 68 } 68 69 private int SwarmSize { … … 81 82 #endregion 82 83 84 #region Construction & Cloning 83 85 [StorableConstructor] 84 86 private MultiPSOTopologyUpdater(bool deserializing) : base(deserializing) { } 85 87 private MultiPSOTopologyUpdater(MultiPSOTopologyUpdater original, Cloner cloner) : base(original, cloner) { } 86 87 88 public MultiPSOTopologyUpdater() 88 89 : base() { 89 90 Parameters.Add(new LookupParameter<IRandom>("Random", "A random number generator.")); 90 Parameters.Add(new ValueLookupParameter<IntValue>("NrOf Connections", "Nr of connected neighbors.", new IntValue(3)));91 Parameters.Add(new ValueLookupParameter<IntValue>("NrOfSwarms", "Nr of connected sub-swarms.", new IntValue(3))); 91 92 Parameters.Add(new LookupParameter<IntValue>("SwarmSize", "Number of particles in the swarm.")); 92 93 Parameters.Add(new ScopeTreeLookupParameter<IntArray>("Neighbors", "The list of neighbors for each particle.")); … … 94 95 Parameters.Add(new ValueLookupParameter<IntValue>("RegroupingPeriod", "Update interval (=iterations) for regrouping of neighborhoods.", new IntValue(5))); 95 96 } 96 97 97 public override IDeepCloneable Clone(Cloner cloner) { 98 98 return new MultiPSOTopologyUpdater(this, cloner); 99 99 } 100 #endregion 100 101 101 // Splits the swarm into non-overlapping sub swarms102 102 public override IOperation Apply() { 103 103 if (CurrentIteration > 0 && CurrentIteration % RegroupingPeriod == 0) { 104 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>()); 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; 108 112 } 109 113 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); 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); 124 119 } 125 120 126 // add neighbors to each particle127 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 121 Neighbors = neighbors; 141 122 } 142 123 return base.Apply(); 143 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 } 144 133 } 145 134 } -
TabularUnified trunk/sources/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/ParticleSwarmOptimizationMainLoop.cs ¶
r5581 r5592 33 33 [StorableClass] 34 34 public class ParticleSwarmOptimizationMainLoop : AlgorithmOperator { 35 35 36 #region Parameter Properties 36 37 public IValueLookupParameter<IRandom> RandomParameter { … … 96 97 #region Create parameters 97 98 Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator.")); 98 Parameters.Add(new ValueLookupParameter<IntValue>("SwarmSize", "Size of the particle swarm." , new IntValue(10)));99 Parameters.Add(new ValueLookupParameter<IntValue>("MaxIterations", "Maximal number of iterations." , new IntValue(1000)));99 Parameters.Add(new ValueLookupParameter<IntValue>("SwarmSize", "Size of the particle swarm.")); 100 Parameters.Add(new ValueLookupParameter<IntValue>("MaxIterations", "Maximal number of iterations.")); 100 101 101 102 Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
Note: See TracChangeset
for help on using the changeset viewer.