Changeset 15277 for stable/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/SPSO2011ParticleUpdater.cs
- Timestamp:
- 07/21/17 11:18:40 (7 years ago)
- Location:
- stable
- Files:
-
- 2 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
stable
- Property svn:mergeinfo changed
/trunk/sources merged: 15071,15076,15091-15093,15096,15102,15114,15181,15201,15214,15223-15224,15228
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Encodings.RealVectorEncoding
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Encodings.RealVectorEncoding merged: 15071,15091,15096,15102,15114,15181,15201,15214
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/SPSO2011ParticleUpdater.cs
r15096 r15277 25 25 using HeuristicLab.Data; 26 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 using HeuristicLab.Random; 27 28 28 29 namespace HeuristicLab.Encodings.RealVectorEncoding { 29 30 [Item("SPSO 2011 Particle Updater", "Updates the particle's position according to the formulae described in SPSO 2011.")] 30 31 [StorableClass] 31 public sealed class SPSO2011ParticleUpdater : RealVectorParticleUpdater { 32 32 public sealed class SPSO2011ParticleUpdater : SPSOParticleUpdater { 33 33 #region Construction & Cloning 34 34 [StorableConstructor] … … 41 41 #endregion 42 42 43 public static void UpdateVelocity(IRandom random, RealVector velocity, double maxVelocity, RealVector position, double inertia, RealVector personalBest, double personalBestAttraction, RealVector neighborBest, double neighborBestAttraction) {43 public static void UpdateVelocity(IRandom random, RealVector velocity, RealVector position, RealVector personalBest, RealVector neighborBest, double inertia = 0.721, double personalBestAttraction = 1.193, double neighborBestAttraction = 1.193, double maxVelocity = double.MaxValue) { 44 44 var gravity = new double[velocity.Length]; 45 var direct = new RealVector(velocity.Length);45 var direction = new RealVector(velocity.Length); 46 46 var radius = 0.0; 47 47 48 var nd = new NormalDistributedRandom(random, 0, 1); 49 48 50 for (int i = 0; i < velocity.Length; i++) { 49 var g_id = 1.193 * ((personalBest[i] + neighborBest[i] - 2 * position[i]) / 3.0); 51 var g_id = (personalBestAttraction * personalBest[i] 52 + neighborBestAttraction * neighborBest[i] 53 - position[i] * (neighborBestAttraction + personalBestAttraction)) / 3.0; 54 // center of the hyper-sphere 50 55 gravity[i] = g_id + position[i]; 51 direct[i] = (random.NextDouble() - 0.5) * 2; 56 // a random direction vector uniform over the surface of hyper-sphere, see http://mathworld.wolfram.com/HyperspherePointPicking.html 57 direction[i] = nd.NextDouble(); 52 58 radius += g_id * g_id; 53 59 } 54 60 61 // randomly choose a radius within the hyper-sphere 55 62 radius = random.NextDouble() * Math.Sqrt(radius); 56 63 57 var unitscale = Math.Sqrt(direct.DotProduct(direct)); 64 // unitscale is used to rescale the random direction vector to unit length, resp. length of the radius 65 var unitscale = Math.Sqrt(direction.DotProduct(direction)); 58 66 if (unitscale > 0) { 59 67 for (var i = 0; i < velocity.Length; i++) { 60 velocity[i] = velocity[i] * inertia + gravity[i] + direct[i] * radius / unitscale - position[i]; 68 var sampledPos = gravity[i] + direction[i] * radius / unitscale; 69 velocity[i] = velocity[i] * inertia + sampledPos - position[i]; 61 70 } 62 71 } … … 101 110 var neighborBest = NeighborBestParameter.ActualValue; 102 111 var neighborBestAttraction = NeighborBestAttractionParameter.ActualValue.Value; 103 104 UpdateVelocity(random, velocity, maxVelocity, position, inertia, personalBest, personalBestAttraction, neighborBest, neighborBestAttraction);112 113 UpdateVelocity(random, velocity, position, personalBest, neighborBest, inertia, personalBestAttraction, neighborBestAttraction, maxVelocity); 105 114 UpdatePosition(bounds, velocity, position); 106 115
Note: See TracChangeset
for help on using the changeset viewer.