Changeset 5033 for branches/PSO/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/ParticleUpdater.cs
- Timestamp:
- 12/06/10 14:45:27 (13 years ago)
- Location:
- branches/PSO/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PSO/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3
- Property svn:ignore
-
old new 3 3 obj 4 4 *.user 5 *.suo
-
- Property svn:ignore
-
branches/PSO/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/ParticleUpdater.cs
r4068 r5033 25 25 using HeuristicLab.Operators; 26 26 using HeuristicLab.Parameters; 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 using HeuristicLab.Common; 27 29 28 30 namespace HeuristicLab.Algorithms.ParticleSwarmOptimization { 29 public class ParticleUpdater : SingleSuccessorOperator { // ParticleUpdater 31 public class ParticleUpdater : SingleSuccessorOperator { 32 30 33 #region Parameter properties 31 32 public ILookupParameter<RealVector> VelocityParameter { 33 get { return (ILookupParameter<RealVector>)Parameters["Velocity"]; } 34 public LookupParameter<IRandom> RandomParameter { 35 get { return (LookupParameter<IRandom>)Parameters["Random"]; } 34 36 } 35 36 public IParameter RandomParameter { 37 get { return (IParameter)Parameters["Random"]; } 37 public LookupParameter<RealVector> PointParameter { 38 get { return (LookupParameter<RealVector>)Parameters["Point"]; } 38 39 } 39 40 public ILookupParameter<RealVector> CurrentPositionParameter { 41 get { return (ILookupParameter<RealVector>)Parameters["CurrentPosition"]; } 40 public LookupParameter<RealVector> VelocityParameter { 41 get { return (LookupParameter<RealVector>)Parameters["Velocity"]; } 42 42 } 43 44 public ILookupParameter<RealVector> BestLocalParameter { 45 get { return (ILookupParameter<RealVector>)Parameters["BestLocal"]; } 43 public LookupParameter<RealVector> PersonalBestPointParameter { 44 get { return (LookupParameter<RealVector>)Parameters["PersonalBestPoint"]; } 46 45 } 47 48 public ILookupParameter<RealVector> BestGlobalParameter { 49 get { return (ILookupParameter<RealVector>)Parameters["BestGlobal"]; } 46 public LookupParameter<RealVector> BestPointParameter { 47 get { return (LookupParameter<RealVector>)Parameters["BestPoint"]; } 50 48 } 51 52 public ILookupParameter<DoubleMatrix> BoundsParameter { 53 get { return (ILookupParameter<DoubleMatrix>)Parameters["Bounds"]; } 49 public LookupParameter<DoubleMatrix> BoundsParameter { 50 get { return (LookupParameter<DoubleMatrix>)Parameters["Bounds"]; } 51 } 52 public LookupParameter<DoubleMatrix> VelocityBoundsParameter { 53 get { return (LookupParameter<DoubleMatrix>)Parameters["VelocityBounds"]; } 54 } 55 public LookupParameter<DoubleValue> OmegaParameter { 56 get { return (LookupParameter<DoubleValue>)Parameters["Omega"]; } 57 } 58 public LookupParameter<DoubleValue> Phi_PParameter { 59 get { return (LookupParameter<DoubleValue>)Parameters["Phi_P"]; } 60 } 61 public LookupParameter<DoubleValue> Phi_GParameter { 62 get { return (LookupParameter<DoubleValue>)Parameters["Phi_G"]; } 54 63 } 55 64 #endregion 56 65 66 #region Parameter Values 67 public IRandom Random { 68 get { return RandomParameter.ActualValue; } 69 } 70 public RealVector Point { 71 get { return PointParameter.ActualValue; } 72 set { PointParameter.ActualValue = value; } 73 } 74 public RealVector Velocity { 75 get { return VelocityParameter.ActualValue; } 76 set { VelocityParameter.ActualValue = value; } 77 } 78 public RealVector PersonalBestPoint { 79 get { return PersonalBestPointParameter.ActualValue; } 80 } 81 public RealVector BestPoint { 82 get { return BestPointParameter.ActualValue; } 83 } 84 public DoubleMatrix Bounds { 85 get { return BoundsParameter.ActualValue; } 86 } 87 public DoubleMatrix VelocityBounds { 88 get { return VelocityBoundsParameter.ActualValue; } 89 } 90 public double Omega { 91 get { return OmegaParameter.ActualValue.Value; } 92 } 93 public double Phi_P { 94 get { return Phi_PParameter.ActualValue.Value; } 95 } 96 public double Phi_G { 97 get { return Phi_GParameter.ActualValue.Value; } 98 } 99 #endregion 100 101 #region Construction & Cloning 102 103 [StorableConstructor] 104 protected ParticleUpdater(bool deserializing) : base(deserializing) { } 105 protected ParticleUpdater(ParticleUpdater original, Cloner cloner) 106 : base(original, cloner) { 107 } 108 57 109 public ParticleUpdater() 58 110 : base() { 59 Parameters.Add(new LookupParameter<IRandom>("Random", "Random number generator (to generate alpha an beta).")); 60 Parameters.Add(new LookupParameter<RealVector>("Velocity", "The velocity vector to update.")); 61 Parameters.Add(new LookupParameter<RealVector>("CurrentPosition", "Current position")); 62 Parameters.Add(new LookupParameter<RealVector>("BestLocal", "Best local position")); 63 Parameters.Add(new LookupParameter<RealVector>("BestGlobal", "Best global position")); 64 Parameters.Add(new LookupParameter<DoubleMatrix>("Bounds", "The lower and upper bounds for each dimension of the position vector.")); 111 Parameters.Add(new LookupParameter<IRandom>("Random", "Random number generator.")); 112 Parameters.Add(new LookupParameter<RealVector>("Point", "Particle's current position")); 113 Parameters.Add(new LookupParameter<RealVector>("Velocity", "Particle's current velocity.")); 114 Parameters.Add(new LookupParameter<RealVector>("PersonalBestPoint", "Particle's personal best position")); 115 Parameters.Add(new LookupParameter<RealVector>("BestPoint", "Global best position")); 116 Parameters.Add(new LookupParameter<DoubleMatrix>("Bounds", "The lower and upper bounds for each dimension of the position vector for the current problem.")); 117 Parameters.Add(new LookupParameter<DoubleMatrix>("VelocityBounds", "Upper and lower bounds for the particle's velocity vector.")); 118 Parameters.Add(new LookupParameter<DoubleValue>("Omega", "The weight for the particle's velocity vector.")); 119 Parameters.Add(new LookupParameter<DoubleValue>("Phi_P", "The weight for the particle's personal best position.")); 120 Parameters.Add(new LookupParameter<DoubleValue>("Phi_G", "The weight for the global best position.")); 65 121 } 66 122 123 public override IDeepCloneable Clone(Cloner cloner) { 124 return new ParticleUpdater(this, cloner); 125 } 126 127 #endregion 128 67 129 public override IOperation Apply() { 68 double alpha = ((IRandom)RandomParameter.ActualValue).NextDouble(); 69 double beta = ((IRandom)RandomParameter.ActualValue).NextDouble(); 70 RealVector velocity = (RealVector)VelocityParameter.ActualValue; 130 RealVector velocity = new RealVector(Velocity.Length); 131 RealVector position = new RealVector(Point.Length); 132 double r_p = Random.NextDouble(); 133 double r_g = Random.NextDouble(); 71 134 for (int i = 0; i < velocity.Length; i++) { 72 velocity[i] = velocity[i] + alpha * (BestLocalParameter.ActualValue[i] - CurrentPositionParameter.ActualValue[i]) + beta * (BestGlobalParameter.ActualValue[i] - CurrentPositionParameter.ActualValue[i]); 135 velocity[i] = 136 Velocity[i] * Omega + 137 (PersonalBestPoint[i] - Point[i]) * Phi_P * r_p + 138 (BestPoint[i] - Point[i]) * Phi_G * r_g; 73 139 } 74 VelocityParameter.ActualValue = velocity; 75 for (int i = 0; i < CurrentPositionParameter.ActualValue.Length; i++) { 76 CurrentPositionParameter.ActualValue[i] = CurrentPositionParameter.ActualValue[i] + VelocityParameter.ActualValue[i]; 77 if (CurrentPositionParameter.ActualValue[i] < BoundsParameter.ActualValue[0, 0]) { 78 CurrentPositionParameter.ActualValue[i] = BoundsParameter.ActualValue[0, 0]; 79 } else if (CurrentPositionParameter.ActualValue[i] > BoundsParameter.ActualValue[0, 1]) { 80 CurrentPositionParameter.ActualValue[i] = BoundsParameter.ActualValue[0, 1]; 81 } 140 BoundsChecker.Apply(velocity, VelocityBounds); 141 for (int i = 0; i < velocity.Length; i++) { 142 position[i] = Point[i] + velocity[i]; 82 143 } 144 BoundsChecker.Apply(position, Bounds); 145 Point = position; 146 Velocity = velocity; 147 83 148 return base.Apply(); 84 149 }
Note: See TracChangeset
for help on using the changeset viewer.