Changeset 15102 for trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorParticleUpdater.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/RealVectorParticleUpdater.cs
r15096 r15102 19 19 */ 20 20 #endregion 21 21 22 using System; 22 23 using HeuristicLab.Common; 23 24 using HeuristicLab.Core; 24 25 using HeuristicLab.Data; 25 26 using HeuristicLab.Operators; 26 using HeuristicLab.Optimization;27 27 using HeuristicLab.Parameters; 28 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 29 using HeuristicLab.PluginInfrastructure; 29 30 30 31 namespace HeuristicLab.Encodings.RealVectorEncoding { … … 32 33 [Item("RealVectorParticleUpdater", "Updates a certain particle taking the current position and velocity into account, as well as the best point and the best point in a local neighborhood.")] 33 34 [StorableClass] 34 public abstract class RealVectorParticleUpdater : SingleSuccessorOperator, IRealVectorParticleUpdater { 35 [NonDiscoverableType] 36 [Obsolete("Use SPSO2011ParticleUpdater")] 37 internal abstract class RealVectorParticleUpdater : SingleSuccessorOperator, IRealVectorParticleUpdater { 35 38 36 39 public override bool CanChangeName { … … 51 54 get { return (ILookupParameter<RealVector>)Parameters["NeighborBest"]; } 52 55 } 56 public LookupParameter<RealVector> BestRealVectorParameter { 57 get { return (LookupParameter<RealVector>)Parameters["BestRealVector"]; } 58 } 53 59 public ILookupParameter<RealVector> RealVectorParameter { 54 60 get { return (ILookupParameter<RealVector>)Parameters["RealVector"]; } … … 57 63 get { return (ILookupParameter<DoubleMatrix>)Parameters["Bounds"]; } 58 64 } 65 public ILookupParameter<DoubleMatrix> CurrentVelocityBoundsParameter { 66 get { return (ILookupParameter<DoubleMatrix>)Parameters["CurrentVelocityBounds"]; } 67 } 59 68 public ILookupParameter<DoubleValue> CurrentMaxVelocityParameter { 60 69 get { return (ILookupParameter<DoubleValue>)Parameters["CurrentMaxVelocity"]; } 61 70 } 62 public ILookupParameter<DoubleValue> CurrentInertiaParameter {71 public ILookupParameter<DoubleValue> InertiaParameter { 63 72 get { return (ILookupParameter<DoubleValue>)Parameters["CurrentInertia"]; } 64 73 } 65 ILookupParameter<DoubleValue> IParticleUpdater.InertiaParameter { get { return CurrentInertiaParameter; } }66 67 74 public ILookupParameter<DoubleValue> PersonalBestAttractionParameter { 68 75 get { return (ILookupParameter<DoubleValue>)Parameters["PersonalBestAttraction"]; } … … 72 79 } 73 80 #endregion 74 81 82 #region Parameter Values 83 protected IRandom Random { 84 get { return RandomParameter.ActualValue; } 85 } 86 protected RealVector Velocity { 87 get { return VelocityParameter.ActualValue; } 88 set { VelocityParameter.ActualValue = value; } 89 } 90 protected RealVector PersonalBest { 91 get { return PersonalBestParameter.ActualValue; } 92 } 93 protected RealVector BestPoint { 94 get { return BestRealVectorParameter.ActualValue; } 95 } 96 protected RealVector RealVector { 97 get { return RealVectorParameter.ActualValue; } 98 set { RealVectorParameter.ActualValue = value; } 99 } 100 protected RealVector NeighborBest { 101 get { return NeighborBestParameter.ActualValue; } 102 } 103 protected DoubleMatrix Bounds { 104 get { return BoundsParameter.ActualValue; } 105 } 106 protected DoubleMatrix CurrentVelocityBounds { 107 get { return CurrentVelocityBoundsParameter.ActualValue; } 108 } 109 protected DoubleValue Inertia { 110 get { return InertiaParameter.ActualValue; } 111 } 112 protected DoubleValue PersonalBestAttraction { 113 get { return PersonalBestAttractionParameter.ActualValue; } 114 } 115 protected DoubleValue NeighborBestAttraction { 116 get { return NeighborBestAttractionParameter.ActualValue; } 117 } 118 #endregion 119 75 120 #region Construction & Cloning 76 121 [StorableConstructor] … … 83 128 Parameters.Add(new LookupParameter<RealVector>("Velocity", "Particle's current velocity.")); 84 129 Parameters.Add(new LookupParameter<RealVector>("PersonalBest", "Particle's personal best solution.")); 130 Parameters.Add(new LookupParameter<RealVector>("BestRealVector", "Global best position.")); 85 131 Parameters.Add(new LookupParameter<RealVector>("NeighborBest", "Best neighboring solution.")); 86 132 Parameters.Add(new LookupParameter<DoubleMatrix>("Bounds", "The lower and upper bounds for each dimension of the position vector for the current problem.")); 133 Parameters.Add(new LookupParameter<DoubleMatrix>("CurrentVelocityBounds", "Upper and lower bounds for the particle's velocity vector.")); 87 134 Parameters.Add(new LookupParameter<DoubleValue>("CurrentMaxVelocity", "Maximum for the particle's velocity vector.")); 88 135 Parameters.Add(new LookupParameter<DoubleValue>("CurrentInertia", "The weight for the particle's velocity vector.")); … … 91 138 } 92 139 #endregion 140 141 [StorableHook(HookType.AfterDeserialization)] 142 private void AfterDeserialization() { 143 if (!Parameters.ContainsKey("CurrentMaxVelocity")) 144 Parameters.Add(new LookupParameter<DoubleValue>("CurrentMaxVelocity", "Maximum for the particle's velocity vector.")); 145 } 146 147 protected void MoveParticle(RealVector velocity, RealVector position) { 148 BoundsChecker.Apply(velocity, CurrentVelocityBounds); 149 for (int i = 0; i < velocity.Length; i++) { 150 position[i] = RealVector[i] + velocity[i]; 151 } 152 for (int i = 0; i < position.Length; i++) { 153 double min = Bounds[i % Bounds.Rows, 0]; 154 double max = Bounds[i % Bounds.Rows, 1]; 155 if (position[i] < min) { 156 int reflectionCount = (int)Math.Truncate((min - position[i]) / (max - min)) + 1; 157 double reflection = (min - position[i]) % (max - min); 158 if (IsOdd(reflectionCount)) { 159 position[i] = min + reflection; 160 velocity[i] = -velocity[i]; 161 162 } else { 163 position[i] = max - reflection; 164 } 165 } 166 if (position[i] > max) { 167 int reflectionCount = (int)Math.Truncate((position[i] - max) / (max - min)) + 1; 168 double reflection = (position[i] - max) % (max - min); 169 if (IsOdd(reflectionCount)) { 170 position[i] = max - reflection; 171 velocity[i] = -velocity[i]; 172 } else { 173 position[i] = min + reflection; 174 } 175 } 176 } 177 178 RealVector = position; 179 Velocity = velocity; 180 } 181 182 private static bool IsOdd(int number) { 183 return number % 2 == 1; 184 } 93 185 } 94 186 }
Note: See TracChangeset
for help on using the changeset viewer.