Changeset 15102 for trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorNeighborhoodParticleUpdater.cs
- Timestamp:
- 06/30/17 21:57:38 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorNeighborhoodParticleUpdater.cs
r15096 r15102 30 30 [StorableClass] 31 31 [NonDiscoverableType] 32 [Obsolete(" Replaced by SPSO2007ParticleUpdater")]32 [Obsolete("Use SPSO2011ParticleUpdater")] 33 33 internal sealed class RealVectorNeighborhoodParticleUpdater : RealVectorParticleUpdater { 34 34 … … 43 43 #endregion 44 44 45 private void UpdateVelocity() { 46 var velocity = VelocityParameter.ActualValue; 47 var position = RealVectorParameter.ActualValue; 48 var inertia = CurrentInertiaParameter.ActualValue.Value; 49 var personalBest = PersonalBestParameter.ActualValue; 50 var personalBestAttraction = PersonalBestAttractionParameter.ActualValue.Value; 51 var neighborBest = NeighborBestParameter.ActualValue; 52 var neighborBestAttraction = NeighborBestAttractionParameter.ActualValue.Value; 45 public override IOperation Apply() { 46 double inertia = Inertia.Value; 47 double personalBestAttraction = PersonalBestAttraction.Value; 48 double neighborBestAttraction = NeighborBestAttraction.Value; 53 49 54 var random = RandomParameter.ActualValue; 50 RealVector velocity = new RealVector(Velocity.Length); 51 RealVector position = new RealVector(RealVector.Length); 52 double r_p = Random.NextDouble(); 53 double r_g = Random.NextDouble(); 55 54 56 55 for (int i = 0; i < velocity.Length; i++) { 57 double r_p = random.NextDouble();58 double r_g = random.NextDouble();59 56 velocity[i] = 60 velocity[i] * inertia +61 ( personalBest[i] - position[i]) * personalBestAttraction * r_p +62 ( neighborBest[i] - position[i]) * neighborBestAttraction * r_g;57 Velocity[i] * inertia + 58 (PersonalBest[i] - RealVector[i]) * personalBestAttraction * r_p + 59 (BestPoint[i] - RealVector[i]) * neighborBestAttraction * r_g; 63 60 } 64 61 65 var maxVelocity = CurrentMaxVelocityParameter.ActualValue.Value; 66 var speed = Math.Sqrt(velocity.DotProduct(velocity)); 67 if (speed > maxVelocity) { 68 for (var i = 0; i < velocity.Length; i++) { 69 velocity[i] *= maxVelocity / speed; 70 } 71 } 72 } 73 74 private void UpdatePosition() { 75 var velocity = VelocityParameter.ActualValue; 76 var position = RealVectorParameter.ActualValue; 77 78 for (int i = 0; i < velocity.Length; i++) { 79 position[i] += velocity[i]; 80 } 81 82 var bounds = BoundsParameter.ActualValue; 83 for (int i = 0; i < position.Length; i++) { 84 double min = bounds[i % bounds.Rows, 0]; 85 double max = bounds[i % bounds.Rows, 1]; 86 if (position[i] < min) { 87 position[i] = min; 88 velocity[i] = -0.5 * velocity[i]; // SPSO 2011 89 } 90 if (position[i] > max) { 91 position[i] = max; 92 velocity[i] = -0.5 * velocity[i]; // SPSO 2011 93 } 94 } 95 } 96 97 public override IOperation Apply() { 98 UpdateVelocity(); 99 UpdatePosition(); 62 MoveParticle(velocity, position); 100 63 101 64 return base.Apply();
Note: See TracChangeset
for help on using the changeset viewer.