- Timestamp:
- 06/29/17 23:04:03 (7 years ago)
- Location:
- trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators
- Files:
-
- 2 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorNeighborhoodParticleUpdater.cs
r15091 r15096 20 20 #endregion 21 21 22 using System; 22 23 using HeuristicLab.Common; 23 24 using HeuristicLab.Core; 24 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 26 using HeuristicLab.PluginInfrastructure; 25 27 26 28 namespace HeuristicLab.Encodings.RealVectorEncoding { 27 29 [Item("Neighborhood Particle Updater", "Updates the particle's position using (among other things) the best neighbor's position. Point = Point + Velocity*Inertia + (PersonalBestPoint-Point)*Phi_P*r_p + (BestNeighborPoint-Point)*Phi_G*r_g.")] 28 30 [StorableClass] 29 public sealed class RealVectorNeighborhoodParticleUpdater : RealVectorParticleUpdater { 31 [NonDiscoverableType] 32 [Obsolete("Replaced by SPSO2007ParticleUpdater")] 33 internal sealed class RealVectorNeighborhoodParticleUpdater : RealVectorParticleUpdater { 30 34 31 35 #region Construction & Cloning … … 39 43 #endregion 40 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; 53 54 var random = RandomParameter.ActualValue; 55 56 for (int i = 0; i < velocity.Length; i++) { 57 double r_p = random.NextDouble(); 58 double r_g = random.NextDouble(); 59 velocity[i] = 60 velocity[i] * inertia + 61 (personalBest[i] - position[i]) * personalBestAttraction * r_p + 62 (neighborBest[i] - position[i]) * neighborBestAttraction * r_g; 63 } 64 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 41 97 public override IOperation Apply() { 42 98 UpdateVelocity(); -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorParticleUpdater.cs
r15091 r15096 19 19 */ 20 20 #endregion 21 22 using System; 21 23 22 using HeuristicLab.Common; 24 23 using HeuristicLab.Core; 25 24 using HeuristicLab.Data; 26 25 using HeuristicLab.Operators; 26 using HeuristicLab.Optimization; 27 27 using HeuristicLab.Parameters; 28 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 60 60 get { return (ILookupParameter<DoubleValue>)Parameters["CurrentMaxVelocity"]; } 61 61 } 62 public ILookupParameter<DoubleValue> InertiaParameter {62 public ILookupParameter<DoubleValue> CurrentInertiaParameter { 63 63 get { return (ILookupParameter<DoubleValue>)Parameters["CurrentInertia"]; } 64 64 } 65 ILookupParameter<DoubleValue> IParticleUpdater.InertiaParameter { get { return CurrentInertiaParameter; } } 66 65 67 public ILookupParameter<DoubleValue> PersonalBestAttractionParameter { 66 68 get { return (ILookupParameter<DoubleValue>)Parameters["PersonalBestAttraction"]; } … … 89 91 } 90 92 #endregion 91 92 protected void UpdateVelocity() {93 var velocity = VelocityParameter.ActualValue;94 var position = RealVectorParameter.ActualValue;95 var inertia = InertiaParameter.ActualValue.Value;96 var personalBest = PersonalBestParameter.ActualValue;97 var personalBestAttraction = PersonalBestAttractionParameter.ActualValue.Value;98 var neighborBest = NeighborBestParameter.ActualValue;99 var neighborBestAttraction = NeighborBestAttractionParameter.ActualValue.Value;100 101 var random = RandomParameter.ActualValue;102 103 for (int i = 0; i < velocity.Length; i++) {104 double r_p = random.NextDouble();105 double r_g = random.NextDouble();106 velocity[i] =107 velocity[i] * inertia +108 (personalBest[i] - position[i]) * personalBestAttraction * r_p +109 (neighborBest[i] - position[i]) * neighborBestAttraction * r_g;110 }111 112 var maxVelocity = CurrentMaxVelocityParameter.ActualValue.Value;113 var speed = Math.Sqrt(velocity.DotProduct(velocity));114 if (speed > maxVelocity) {115 for (var i = 0; i < velocity.Length; i++) {116 velocity[i] *= maxVelocity / speed;117 }118 }119 }120 121 protected void UpdatePosition() {122 var velocity = VelocityParameter.ActualValue;123 var position = RealVectorParameter.ActualValue;124 125 for (int i = 0; i < velocity.Length; i++) {126 position[i] += velocity[i];127 }128 129 var bounds = BoundsParameter.ActualValue;130 for (int i = 0; i < position.Length; i++) {131 double min = bounds[i % bounds.Rows, 0];132 double max = bounds[i % bounds.Rows, 1];133 if (position[i] < min) {134 position[i] = min;135 velocity[i] = -0.5 * velocity[i]; // SPSO 2011136 }137 if (position[i] > max) {138 position[i] = max;139 velocity[i] = -0.5 * velocity[i]; // SPSO 2011140 }141 }142 }143 93 } 144 94 } -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorSwarmUpdater.cs
r15091 r15096 126 126 Parameters.Add(new ScopeTreeLookupParameter<IntArray>("Neighbors", "The list of neighbors for each particle.")); 127 127 Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false.")); 128 Parameters.Add(new ValueLookupParameter<DoubleValue>("MaxVelocity", "Speed limit for each particle.", new DoubleValue(1 )));128 Parameters.Add(new ValueLookupParameter<DoubleValue>("MaxVelocity", "Speed limit for each particle.", new DoubleValue(1000000))); 129 129 Parameters.Add(new LookupParameter<DoubleValue>("CurrentMaxVelocity", "Current value of the speed limit.")); 130 130 Parameters.Add(new LookupParameter<ResultCollection>("Results", "Results")); -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorTotallyConnectedParticleUpdater.cs
r15091 r15096 23 23 using HeuristicLab.Common; 24 24 using HeuristicLab.Core; 25 using HeuristicLab.Optimization;26 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 26 using HeuristicLab.PluginInfrastructure; … … 31 30 [StorableClass] 32 31 [NonDiscoverableType] 33 [Obsolete(" Same as the RealVectorNeighborhoodParticleUpdate")]32 [Obsolete("Replaced by SPSO2007ParticleUpdater")] 34 33 internal sealed class RealVectorTotallyConnectedParticleUpdater : RealVectorParticleUpdater { 35 34 … … 44 43 #endregion 45 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; 53 54 var random = RandomParameter.ActualValue; 55 56 for (int i = 0; i < velocity.Length; i++) { 57 double r_p = random.NextDouble(); 58 double r_g = random.NextDouble(); 59 velocity[i] = 60 velocity[i] * inertia + 61 (personalBest[i] - position[i]) * personalBestAttraction * r_p + 62 (neighborBest[i] - position[i]) * neighborBestAttraction * r_g; 63 } 64 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 46 97 public override IOperation Apply() { 47 98 UpdateVelocity();
Note: See TracChangeset
for help on using the changeset viewer.