Changeset 5592 for trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorSwarmUpdater.cs
- Timestamp:
- 03/02/11 15:22:54 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorSwarmUpdater.cs
r5581 r5592 33 33 [StorableClass] 34 34 public sealed class RealVectorSwarmUpdater : SingleSuccessorOperator, IRealVectorSwarmUpdater { 35 35 36 public override bool CanChangeName { 36 37 get { return false; } … … 79 80 private DoubleValue BestQuality { 80 81 get { return BestQualityParameter.ActualValue; } 82 set { BestQualityParameter.ActualValue = value; } 81 83 } 82 84 private RealVector BestPoint { … … 149 151 150 152 public override IOperation Apply() { 151 InitializeBestPoint(); 152 UpdateNeighbors(); 153 UpdateSwarm(); 154 if (VelocityBoundsUpdater != null) { 155 var ops = new OperationCollection(); 156 ops.Add(ExecutionContext.CreateChildOperation(VelocityBoundsUpdater)); 157 ops.Add(ExecutionContext.CreateOperation(Successor)); 158 return ops; 159 } else { 160 return base.Apply(); 161 } 162 } 163 164 private void InitializeBestPoint() { 153 UpdateGlobalBest(); 154 UpdateNeighborBest(); 155 UpdatePersonalBest(); 156 return UpdateVelocityBounds(); 157 } 158 159 private void UpdateGlobalBest() { 165 160 if (BestQuality == null) 166 BestQuality Parameter.ActualValue= new DoubleValue();161 BestQuality = new DoubleValue(); 167 162 BestQuality.Value = Maximization ? Quality.Max(v => v.Value) : Quality.Min(v => v.Value); 168 int bestIndex = Quality.FindIndex(v => v.Value == BestQuality.Value); 169 BestPoint = (RealVector)RealVector[bestIndex].Clone(); 170 } 171 172 private void UpdateNeighbors() { 173 if (Neighbors != null & Neighbors.Length > 0) { 174 if (this.NeighborBest == null || NeighborBest.Length != Neighbors.Length) 175 NeighborBest = new ItemArray<RealVector>(Neighbors.Length); 163 BestPoint = (RealVector)RealVector[Quality.FindIndex(v => v.Value == BestQuality.Value)].Clone(); 164 } 165 166 private void UpdateNeighborBest() { 167 if (Neighbors.Length > 0) { 168 var neighborBest = new ItemArray<RealVector>(Neighbors.Length); 169 var neighborBestQuality = new ItemArray<DoubleValue>(Neighbors.Length); 176 170 for (int n = 0; n < Neighbors.Length; n++) { 177 171 var pairs = Quality.Zip(RealVector, (q, p) => new { Quality = q, Point = p }) 178 172 .Where((p, i) => i == n || Neighbors[n].Contains(i)); 179 NeighborBest[n] = Maximization ? 180 pairs.OrderByDescending(p => p.Quality.Value).First().Point : 181 pairs.OrderBy(p => p.Quality.Value).First().Point; 173 var bestNeighbor = Maximization ? 174 pairs.OrderByDescending(p => p.Quality.Value).First() : 175 pairs.OrderBy(p => p.Quality.Value).First(); 176 neighborBest[n] = bestNeighbor.Point; 177 neighborBestQuality[n] = bestNeighbor.Quality; 182 178 } 183 NeighborBestParameter.ActualValue = NeighborBest; 179 NeighborBest = neighborBest; 180 NeighborBestQuality = neighborBestQuality; 184 181 } 185 182 } 186 183 187 private void Update Swarm() {188 if (PersonalBestQuality.Length == 0) {184 private void UpdatePersonalBest() { 185 if (PersonalBestQuality.Length == 0) 189 186 PersonalBestQuality = (ItemArray<DoubleValue>)Quality.Clone(); 190 if (VelocityBounds == null)191 VelocityBounds = new DoubleMatrix(new double[,] { { -1, 1 } });192 }193 187 for (int i = 0; i < RealVector.Length; i++) { 194 188 if (Maximization && Quality[i].Value > PersonalBestQuality[i].Value || … … 198 192 } 199 193 } 200 if (Neighbors.Length > 0) { 201 var neighborBestQuality = NeighborBestQuality; 202 var neighborBest = NeighborBest; 203 if (NeighborBestQuality.Length == 0) { 204 neighborBestQuality = (ItemArray<DoubleValue>)Quality.Clone(); 205 neighborBest = (ItemArray<RealVector>)RealVector.Clone(); 206 } 207 for (int i = 0; i < RealVector.Length; i++) { 208 if (Maximization && PersonalBestQuality[i].Value > neighborBestQuality[i].Value || 209 !Maximization && PersonalBestQuality[i].Value < neighborBestQuality[i].Value) { 210 neighborBestQuality[i].Value = PersonalBestQuality[i].Value; 211 neighborBest[i] = PersonalBest[i]; 212 } 213 } 214 NeighborBestQuality = neighborBestQuality; 215 NeighborBest = neighborBest; 216 } 194 } 195 196 private IOperation UpdateVelocityBounds() { 197 if (VelocityBounds == null) 198 VelocityBounds = new DoubleMatrix(new double[,] { { -1, 1 } }); 199 return VelocityBoundsUpdater == null ? 200 base.Apply() : 201 new OperationCollection() { 202 ExecutionContext.CreateChildOperation(VelocityBoundsUpdater), 203 ExecutionContext.CreateOperation(Successor) 204 }; 217 205 } 218 206 }
Note: See TracChangeset
for help on using the changeset viewer.