Changeset 5592 for trunk/sources
- Timestamp:
- 03/02/11 15:22:54 (14 years ago)
- Location:
- trunk/sources
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
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 } -
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.")); -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorNeighborhoodParticleUpdater.cs
r5568 r5592 30 30 public sealed class RealVectorNeighborhoodParticleUpdater : RealVectorParticleUpdater, ILocalParticleUpdater { 31 31 32 #region Construction & Cloning 32 33 [StorableConstructor] 33 34 private RealVectorNeighborhoodParticleUpdater(bool deserializing) : base(deserializing) { } 34 35 private RealVectorNeighborhoodParticleUpdater(RealVectorNeighborhoodParticleUpdater original, Cloner cloner) : base(original, cloner) { } 35 36 public RealVectorNeighborhoodParticleUpdater() : base() { } 36 37 37 public override IDeepCloneable Clone(Cloner cloner) { 38 38 return new RealVectorNeighborhoodParticleUpdater(this, cloner); 39 39 } 40 #endregion 40 41 41 42 public override IOperation Apply() { 43 double inertia = Inertia.Value; 44 double personalBestAttraction = PersonalBestAttraction.Value; 45 double neighborBestAttraction = NeighborBestAttraction.Value; 46 42 47 RealVector velocity = new RealVector(Velocity.Length); 43 48 RealVector position = new RealVector(RealVector.Length); 44 49 double r_p = Random.NextDouble(); 45 50 double r_g = Random.NextDouble(); 46 double omega = Inertia.Value; 47 double phi_p = PersonalBestAttraction.Value; 48 double phi_g = NeighborBestAttraction.Value; 51 49 52 for (int i = 0; i < velocity.Length; i++) { 50 53 velocity[i] = 51 Velocity[i] * omega +52 (PersonalBest[i] - RealVector[i]) * p hi_p* r_p +53 (BestPoint[i] - RealVector[i]) * phi_g* r_g;54 Velocity[i] * inertia + 55 (PersonalBest[i] - RealVector[i]) * personalBestAttraction * r_p + 56 (BestPoint[i] - RealVector[i]) * neighborBestAttraction * r_g; 54 57 } 58 55 59 BoundsChecker.Apply(velocity, VelocityBounds); 56 60 for (int i = 0; i < velocity.Length; i++) { … … 58 62 } 59 63 BoundsChecker.Apply(position, Bounds); 64 60 65 RealVector = position; 61 66 Velocity = velocity; -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorParticleCreator.cs
r5560 r5592 20 20 #endregion 21 21 22 using System; 23 using HeuristicLab.Operators; 22 using HeuristicLab.Common; 24 23 using HeuristicLab.Core; 25 24 using HeuristicLab.Data; 25 using HeuristicLab.Operators; 26 26 using HeuristicLab.Parameters; 27 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 using HeuristicLab.Common;29 using HeuristicLab.Optimization;30 28 31 29 namespace HeuristicLab.Encodings.RealVectorEncoding { 32 30 [Item("RealVectorParticleCreator", "Creates a particle with position, zero velocity vector and personal best.")] 33 31 [StorableClass] 34 public class RealVectorParticleCreator : SingleSuccessorOperator, IRealVectorParticleCreator { 35 #region IRealVectorParticleCreator Members 32 public class RealVectorParticleCreator : AlgorithmOperator, IRealVectorParticleCreator { 33 34 #region Parameters 36 35 public ILookupParameter<IntValue> ProblemSizeParameter { 37 get { return (ILookupParameter<IntValue>)Parameters["ProblemSize"]; }36 get { return (ILookupParameter<IntValue>)Parameters["ProblemSize"]; } 38 37 } 39 38 public IValueLookupParameter<DoubleMatrix> BoundsParameter { 39 get { return (IValueLookupParameter<DoubleMatrix>)Parameters["Bounds"]; } 40 } 40 41 public ILookupParameter<RealVector> RealVectorParameter { 41 42 get { return (ILookupParameter<RealVector>)Parameters["RealVector"]; } 42 43 } 43 44 44 public ILookupParameter<RealVector> PersonalBestParameter { 45 45 get { return (ILookupParameter<RealVector>)Parameters["PersonalBest"]; } 46 46 } 47 48 public IValueLookupParameter<DoubleMatrix> BoundsParameter {49 get { return (IValueLookupParameter<DoubleMatrix>)Parameters["Bounds"]; }50 }51 52 47 public ILookupParameter<RealVector> VelocityParameter { 53 48 get { return (ILookupParameter<RealVector>)Parameters["Velocity"]; } 54 49 } 55 56 50 #endregion 57 51 58 public RealVectorParticleCreator() : base() { 52 #region Parameter Values 53 protected int ProblemSize { 54 get { return ProblemSizeParameter.ActualValue.Value; } 55 } 56 protected RealVector Velocity { 57 set { VelocityParameter.ActualValue = value; } 58 } 59 #endregion 60 61 #region Construction & Cloning 62 [StorableConstructor] 63 protected RealVectorParticleCreator(bool deserializing) : base(deserializing) { } 64 protected RealVectorParticleCreator(RealVectorParticleCreator original, Cloner cloner) : base(original, cloner) { } 65 public RealVectorParticleCreator() 66 : base() { 59 67 Parameters.Add(new LookupParameter<IntValue>("ProblemSize", "The dimension of the problem.")); 60 68 Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Bounds", "The lower and upper bounds in each dimension.")); 61 69 Parameters.Add(new LookupParameter<RealVector>("RealVector", "Particle's current solution")); 62 70 Parameters.Add(new LookupParameter<RealVector>("PersonalBest", "Particle's personal best solution.")); 63 Parameters.Add(new LookupParameter<RealVector>("Velocity", "Particle's current velocity.")); 64 } 71 Parameters.Add(new LookupParameter<RealVector>("Velocity", "Particle's current velocity.")); 65 72 66 public override IOperation Apply() { 67 VelocityParameter.ActualValue = new RealVector(ProblemSizeParameter.ActualValue.Value); 68 UniformRandomRealVectorCreator realVectorCreater = new UniformRandomRealVectorCreator(); 73 UniformRandomRealVectorCreator realVectorCreater = new UniformRandomRealVectorCreator(); 69 74 Assigner personalBestPositionAssigner = new Assigner(); 70 75 71 this.Name = "Particle Creator";76 OperatorGraph.InitialOperator = realVectorCreater; 72 77 73 //realVectorCreater.Name = "(SolutionCreator)"; 74 realVectorCreater.RealVectorParameter.ActualName = "RealVector"; 78 realVectorCreater.RealVectorParameter.ActualName = RealVectorParameter.Name; 75 79 realVectorCreater.LengthParameter.ActualName = ProblemSizeParameter.Name; 76 realVectorCreater.BoundsParameter.ActualName = BoundsParameter.Name; 80 realVectorCreater.BoundsParameter.ActualName = BoundsParameter.Name; 77 81 realVectorCreater.Successor = personalBestPositionAssigner; 78 82 79 personalBestPositionAssigner.LeftSideParameter.ActualName = "PersonalBest"; 80 personalBestPositionAssigner.RightSideParameter.ActualName = "RealVector"; 81 personalBestPositionAssigner.Successor = null; 82 83 OperationCollection next = new OperationCollection(); 84 next.Add(ExecutionContext.CreateChildOperation(realVectorCreater)); 85 next.Add(base.Apply()); 86 return next; 83 personalBestPositionAssigner.LeftSideParameter.ActualName = PersonalBestParameter.Name; 84 personalBestPositionAssigner.RightSideParameter.ActualName = RealVectorParameter.Name; 85 personalBestPositionAssigner.Successor = null; 87 86 } 88 89 87 public override IDeepCloneable Clone(Cloner cloner) { 90 88 return new RealVectorParticleCreator(this, cloner); 91 89 } 90 #endregion 92 91 93 protected RealVectorParticleCreator(RealVectorParticleCreator original, Cloner cloner) 94 : base(original, cloner) { 92 public override IOperation Apply() { 93 Velocity = new RealVector(ProblemSize); 94 return base.Apply(); 95 95 } 96 96 97 [StorableConstructor] 98 protected RealVectorParticleCreator(bool deserializing) : base(deserializing) { } 97 99 98 } 100 99 } -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorParticleUpdater.cs
r5568 r5592 28 28 29 29 namespace HeuristicLab.Encodings.RealVectorEncoding { 30 30 31 [Item("RealVectorParticleUpdater", "Updates a certain particle taking the current position and velocity into account, as well as the best point and the best point in a local neighborhood.")] 31 32 [StorableClass] 32 33 public abstract class RealVectorParticleUpdater : SingleSuccessorOperator, IRealVectorParticleUpdater { 34 33 35 public override bool CanChangeName { 34 36 get { return false; } … … 110 112 111 113 #region Construction & Cloning 112 113 114 [StorableConstructor] 114 115 protected RealVectorParticleUpdater(bool deserializing) : base(deserializing) { } 115 116 protected RealVectorParticleUpdater(RealVectorParticleUpdater original, Cloner cloner) : base(original, cloner) { } 116 117 117 public RealVectorParticleUpdater() 118 118 : base() { … … 129 129 Parameters.Add(new LookupParameter<DoubleValue>("NeighborBestAttraction", "The weight for the global best position.")); 130 130 } 131 132 131 #endregion 133 132 } -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorSwarmUpdater.cs
r5581 r5592 33 33 [StorableClass] 34 34 public sealed class RealVectorSwarmUpdater : SingleSuccessorOperator, IRealVectorSwarmUpdater { 35 35 36 public override bool CanChangeName { 36 37 get { return false; } … … 79 80 private DoubleValue BestQuality { 80 81 get { return BestQualityParameter.ActualValue; } 82 set { BestQualityParameter.ActualValue = value; } 81 83 } 82 84 private RealVector BestPoint { … … 149 151 150 152 public override IOperation Apply() { 151 InitializeBestPoint(); 152 UpdateNeighbors(); 153 UpdateSwarm(); 154 if (VelocityBoundsUpdater != null) { 155 var ops = new OperationCollection(); 156 ops.Add(ExecutionContext.CreateChildOperation(VelocityBoundsUpdater)); 157 ops.Add(ExecutionContext.CreateOperation(Successor)); 158 return ops; 159 } else { 160 return base.Apply(); 161 } 162 } 163 164 private void InitializeBestPoint() { 153 UpdateGlobalBest(); 154 UpdateNeighborBest(); 155 UpdatePersonalBest(); 156 return UpdateVelocityBounds(); 157 } 158 159 private void UpdateGlobalBest() { 165 160 if (BestQuality == null) 166 BestQuality Parameter.ActualValue= new DoubleValue();161 BestQuality = new DoubleValue(); 167 162 BestQuality.Value = Maximization ? Quality.Max(v => v.Value) : Quality.Min(v => v.Value); 168 int bestIndex = Quality.FindIndex(v => v.Value == BestQuality.Value); 169 BestPoint = (RealVector)RealVector[bestIndex].Clone(); 170 } 171 172 private void UpdateNeighbors() { 173 if (Neighbors != null & Neighbors.Length > 0) { 174 if (this.NeighborBest == null || NeighborBest.Length != Neighbors.Length) 175 NeighborBest = new ItemArray<RealVector>(Neighbors.Length); 163 BestPoint = (RealVector)RealVector[Quality.FindIndex(v => v.Value == BestQuality.Value)].Clone(); 164 } 165 166 private void UpdateNeighborBest() { 167 if (Neighbors.Length > 0) { 168 var neighborBest = new ItemArray<RealVector>(Neighbors.Length); 169 var neighborBestQuality = new ItemArray<DoubleValue>(Neighbors.Length); 176 170 for (int n = 0; n < Neighbors.Length; n++) { 177 171 var pairs = Quality.Zip(RealVector, (q, p) => new { Quality = q, Point = p }) 178 172 .Where((p, i) => i == n || Neighbors[n].Contains(i)); 179 NeighborBest[n] = Maximization ? 180 pairs.OrderByDescending(p => p.Quality.Value).First().Point : 181 pairs.OrderBy(p => p.Quality.Value).First().Point; 173 var bestNeighbor = Maximization ? 174 pairs.OrderByDescending(p => p.Quality.Value).First() : 175 pairs.OrderBy(p => p.Quality.Value).First(); 176 neighborBest[n] = bestNeighbor.Point; 177 neighborBestQuality[n] = bestNeighbor.Quality; 182 178 } 183 NeighborBestParameter.ActualValue = NeighborBest; 179 NeighborBest = neighborBest; 180 NeighborBestQuality = neighborBestQuality; 184 181 } 185 182 } 186 183 187 private void Update Swarm() {188 if (PersonalBestQuality.Length == 0) {184 private void UpdatePersonalBest() { 185 if (PersonalBestQuality.Length == 0) 189 186 PersonalBestQuality = (ItemArray<DoubleValue>)Quality.Clone(); 190 if (VelocityBounds == null)191 VelocityBounds = new DoubleMatrix(new double[,] { { -1, 1 } });192 }193 187 for (int i = 0; i < RealVector.Length; i++) { 194 188 if (Maximization && Quality[i].Value > PersonalBestQuality[i].Value || … … 198 192 } 199 193 } 200 if (Neighbors.Length > 0) { 201 var neighborBestQuality = NeighborBestQuality; 202 var neighborBest = NeighborBest; 203 if (NeighborBestQuality.Length == 0) { 204 neighborBestQuality = (ItemArray<DoubleValue>)Quality.Clone(); 205 neighborBest = (ItemArray<RealVector>)RealVector.Clone(); 206 } 207 for (int i = 0; i < RealVector.Length; i++) { 208 if (Maximization && PersonalBestQuality[i].Value > neighborBestQuality[i].Value || 209 !Maximization && PersonalBestQuality[i].Value < neighborBestQuality[i].Value) { 210 neighborBestQuality[i].Value = PersonalBestQuality[i].Value; 211 neighborBest[i] = PersonalBest[i]; 212 } 213 } 214 NeighborBestQuality = neighborBestQuality; 215 NeighborBest = neighborBest; 216 } 194 } 195 196 private IOperation UpdateVelocityBounds() { 197 if (VelocityBounds == null) 198 VelocityBounds = new DoubleMatrix(new double[,] { { -1, 1 } }); 199 return VelocityBoundsUpdater == null ? 200 base.Apply() : 201 new OperationCollection() { 202 ExecutionContext.CreateChildOperation(VelocityBoundsUpdater), 203 ExecutionContext.CreateOperation(Successor) 204 }; 217 205 } 218 206 } -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorTotallyConnectedParticleUpdater.cs
r5568 r5592 31 31 32 32 #region Construction & Cloning 33 34 33 [StorableConstructor] 35 34 private RealVectorTotallyConnectedParticleUpdater(bool deserializing) : base(deserializing) { } 36 35 private RealVectorTotallyConnectedParticleUpdater(RealVectorTotallyConnectedParticleUpdater original, Cloner cloner) : base(original, cloner) { } 37 36 public RealVectorTotallyConnectedParticleUpdater() : base() { } 38 39 37 public override IDeepCloneable Clone(Cloner cloner) { 40 38 return new RealVectorTotallyConnectedParticleUpdater(this, cloner); 41 39 } 42 43 40 #endregion 44 41 45 42 public override IOperation Apply() { 46 base.Apply(); 43 double inertia = Inertia.Value; 44 double personalBestAttraction = PersonalBestAttraction.Value; 45 double neighborBestAttraction = NeighborBestAttraction.Value; 46 47 47 RealVector velocity = new RealVector(Velocity.Length); 48 48 RealVector position = new RealVector(RealVector.Length); 49 49 double r_p = Random.NextDouble(); 50 50 double r_g = Random.NextDouble(); 51 double omega = Inertia.Value; 52 double phi_p = PersonalBestAttraction.Value; 53 double phi_g = NeighborBestAttraction.Value; 51 54 52 for (int i = 0; i < velocity.Length; i++) { 55 53 velocity[i] = 56 Velocity[i] * omega +57 (PersonalBest[i] - RealVector[i]) * p hi_p* r_p +58 (BestPoint[i] - RealVector[i]) * phi_g* r_g;54 Velocity[i] * inertia + 55 (PersonalBest[i] - RealVector[i]) * personalBestAttraction * r_p + 56 (BestPoint[i] - RealVector[i]) * neighborBestAttraction * r_g; 59 57 } 58 60 59 BoundsChecker.Apply(velocity, VelocityBounds); 61 60 for (int i = 0; i < velocity.Length; i++) { … … 63 62 } 64 63 BoundsChecker.Apply(position, Bounds); 64 65 65 RealVector = position; 66 66 Velocity = velocity;
Note: See TracChangeset
for help on using the changeset viewer.