Changeset 15181 for trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/SPSO2011ParticleUpdater.cs
- Timestamp:
- 07/10/17 17:26:43 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/SPSO2011ParticleUpdater.cs
r15102 r15181 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 { … … 42 43 public static void UpdateVelocity(IRandom random, RealVector velocity, double maxVelocity, RealVector position, double inertia, RealVector personalBest, double personalBestAttraction, RealVector neighborBest, double neighborBestAttraction, double c = 1.193) { 43 44 var gravity = new double[velocity.Length]; 44 var direct = new RealVector(velocity.Length);45 var direction = new RealVector(velocity.Length); 45 46 var radius = 0.0; 47 48 var nd = new NormalDistributedRandom(random, 0, 1); 46 49 47 50 for (int i = 0; i < velocity.Length; i++) { 48 51 var g_id = c * ((personalBest[i] + neighborBest[i] - 2 * position[i]) / 3.0); 52 // center of the hyper-sphere 49 53 gravity[i] = g_id + position[i]; 50 direct[i] = (random.NextDouble() - 0.5) * 2; 54 // a random direction vector uniform over the surface of hyper-sphere, see http://mathworld.wolfram.com/HyperspherePointPicking.html 55 direction[i] = nd.NextDouble(); 51 56 radius += g_id * g_id; 52 57 } 53 58 59 // randomly choose a radius within the hyper-sphere 54 60 radius = random.NextDouble() * Math.Sqrt(radius); 55 61 56 var unitscale = Math.Sqrt(direct.DotProduct(direct)); 62 // unitscale is used to rescale the random direction vector to unit length, resp. length of the radius 63 var unitscale = Math.Sqrt(direction.DotProduct(direction)); 57 64 if (unitscale > 0) { 58 65 for (var i = 0; i < velocity.Length; i++) { 59 velocity[i] = velocity[i] * inertia + gravity[i] + direct[i] * radius / unitscale - position[i]; 66 var sampledPos = gravity[i] + direction[i] * radius / unitscale; 67 velocity[i] = velocity[i] * inertia + sampledPos - position[i]; 60 68 } 61 69 }
Note: See TracChangeset
for help on using the changeset viewer.