Changeset 15091 for trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorParticleUpdater.cs
- Timestamp:
- 06/29/17 15:26:16 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorParticleUpdater.cs
r14185 r15091 51 51 get { return (ILookupParameter<RealVector>)Parameters["NeighborBest"]; } 52 52 } 53 public LookupParameter<RealVector> BestRealVectorParameter {54 get { return (LookupParameter<RealVector>)Parameters["BestRealVector"]; }55 }56 53 public ILookupParameter<RealVector> RealVectorParameter { 57 54 get { return (ILookupParameter<RealVector>)Parameters["RealVector"]; } … … 60 57 get { return (ILookupParameter<DoubleMatrix>)Parameters["Bounds"]; } 61 58 } 62 public ILookupParameter<Double Matrix> CurrentVelocityBoundsParameter {63 get { return (ILookupParameter<Double Matrix>)Parameters["CurrentVelocityBounds"]; }59 public ILookupParameter<DoubleValue> CurrentMaxVelocityParameter { 60 get { return (ILookupParameter<DoubleValue>)Parameters["CurrentMaxVelocity"]; } 64 61 } 65 62 public ILookupParameter<DoubleValue> InertiaParameter { … … 73 70 } 74 71 #endregion 75 76 #region Parameter Values 77 protected IRandom Random { 78 get { return RandomParameter.ActualValue; } 79 } 80 protected RealVector Velocity { 81 get { return VelocityParameter.ActualValue; } 82 set { VelocityParameter.ActualValue = value; } 83 } 84 protected RealVector PersonalBest { 85 get { return PersonalBestParameter.ActualValue; } 86 } 87 protected RealVector BestPoint { 88 get { return BestRealVectorParameter.ActualValue; } 89 } 90 protected RealVector RealVector { 91 get { return RealVectorParameter.ActualValue; } 92 set { RealVectorParameter.ActualValue = value; } 93 } 94 protected RealVector NeighborBest { 95 get { return NeighborBestParameter.ActualValue; } 96 } 97 protected DoubleMatrix Bounds { 98 get { return BoundsParameter.ActualValue; } 99 } 100 protected DoubleMatrix CurrentVelocityBounds { 101 get { return CurrentVelocityBoundsParameter.ActualValue; } 102 } 103 protected DoubleValue Inertia { 104 get { return InertiaParameter.ActualValue; } 105 } 106 protected DoubleValue PersonalBestAttraction { 107 get { return PersonalBestAttractionParameter.ActualValue; } 108 } 109 protected DoubleValue NeighborBestAttraction { 110 get { return NeighborBestAttractionParameter.ActualValue; } 111 } 112 #endregion 113 72 114 73 #region Construction & Cloning 115 74 [StorableConstructor] … … 122 81 Parameters.Add(new LookupParameter<RealVector>("Velocity", "Particle's current velocity.")); 123 82 Parameters.Add(new LookupParameter<RealVector>("PersonalBest", "Particle's personal best solution.")); 124 Parameters.Add(new LookupParameter<RealVector>("BestRealVector", "Global best position."));125 83 Parameters.Add(new LookupParameter<RealVector>("NeighborBest", "Best neighboring solution.")); 126 84 Parameters.Add(new LookupParameter<DoubleMatrix>("Bounds", "The lower and upper bounds for each dimension of the position vector for the current problem.")); 127 Parameters.Add(new LookupParameter<Double Matrix>("CurrentVelocityBounds", "Upper and lower boundsfor the particle's velocity vector."));85 Parameters.Add(new LookupParameter<DoubleValue>("CurrentMaxVelocity", "Maximum for the particle's velocity vector.")); 128 86 Parameters.Add(new LookupParameter<DoubleValue>("CurrentInertia", "The weight for the particle's velocity vector.")); 129 87 Parameters.Add(new LookupParameter<DoubleValue>("PersonalBestAttraction", "The weight for the particle's personal best position.")); … … 132 90 #endregion 133 91 134 protected void MoveParticle(RealVector velocity, RealVector position) { 135 BoundsChecker.Apply(velocity, CurrentVelocityBounds); 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 136 103 for (int i = 0; i < velocity.Length; i++) { 137 position[i] = RealVector[i] + velocity[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; 138 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; 139 130 for (int i = 0; i < position.Length; i++) { 140 double min = Bounds[i % Bounds.Rows, 0];141 double max = Bounds[i % Bounds.Rows, 1];131 double min = bounds[i % bounds.Rows, 0]; 132 double max = bounds[i % bounds.Rows, 1]; 142 133 if (position[i] < min) { 143 int reflectionCount = (int)Math.Truncate((min - position[i]) / (max - min)) + 1; 144 double reflection = (min - position[i]) % (max - min); 145 if (IsOdd(reflectionCount)) { 146 position[i] = min + reflection; 147 velocity[i] = -velocity[i]; 148 149 } else { 150 position[i] = max - reflection; 151 } 134 position[i] = min; 135 velocity[i] = -0.5 * velocity[i]; // SPSO 2011 152 136 } 153 137 if (position[i] > max) { 154 int reflectionCount = (int)Math.Truncate((position[i] - max) / (max - min)) + 1; 155 double reflection = (position[i] - max) % (max - min); 156 if (IsOdd(reflectionCount)) { 157 position[i] = max - reflection; 158 velocity[i] = -velocity[i]; 159 } else { 160 position[i] = min + reflection; 161 } 138 position[i] = max; 139 velocity[i] = -0.5 * velocity[i]; // SPSO 2011 162 140 } 163 141 } 164 165 RealVector = position;166 Velocity = velocity;167 }168 169 private static bool IsOdd(int number) {170 return number % 2 == 1;171 142 } 172 143 }
Note: See TracChangeset
for help on using the changeset viewer.