Changeset 15181 for trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/SPSOSwarmUpdater.cs
- Timestamp:
- 07/10/17 17:26:43 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/SPSOSwarmUpdater.cs
r15102 r15181 34 34 35 35 namespace HeuristicLab.Encodings.RealVectorEncoding { 36 [Item("Swarm Updater (SPSO)", "Updates personal best point and quality as well as globalbest point and quality.")]36 [Item("Swarm Updater (SPSO)", "Updates personal best point and quality as well as neighbor best point and quality.")] 37 37 [StorableClass] 38 38 public sealed class SPSOSwarmUpdater : SingleSuccessorOperator, IRealVectorSwarmUpdater, ISingleObjectiveOperator { … … 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(1000000)));128 Parameters.Add(new ValueLookupParameter<DoubleValue>("MaxVelocity", "The maximum velocity for each particle and initial velocity if scaling is used.", new DoubleValue(double.MaxValue))); 129 129 Parameters.Add(new LookupParameter<DoubleValue>("CurrentMaxVelocity", "Current value of the speed limit.")); 130 130 Parameters.Add(new LookupParameter<ResultCollection>("Results", "Results")); … … 132 132 #region Max Velocity Updating 133 133 Parameters.Add(new OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>("MaxVelocityScalingOperator", "Modifies the value")); 134 Parameters.Add(new ValueLookupParameter<DoubleValue>("FinalMaxVelocity", "The value of maximum velocity if PSO has reached maximum iterations.", new DoubleValue(1E-10)));134 Parameters.Add(new ValueLookupParameter<DoubleValue>("FinalMaxVelocity", "The value of maximum velocity if scaling is used and PSO has reached maximum iterations.", new DoubleValue(1E-10))); 135 135 Parameters.Add(new LookupParameter<IntValue>("MaxVelocityIndex", "The current index.", "Iterations")); 136 136 Parameters.Add(new ValueLookupParameter<IntValue>("MaxVelocityStartIndex", "The start index at which to start modifying 'Value'.", new IntValue(0))); … … 195 195 var neighborBest = new ItemArray<RealVector>(neighbors.Length); 196 196 var neighborBestQuality = new ItemArray<DoubleValue>(neighbors.Length); 197 double overallBest = double.NaN; 198 RealVector overallBestVector = null; 197 199 for (int n = 0; n < neighbors.Length; n++) { 198 var pairs = particles.Where(x => x.Item1 == n ||neighbors[n].Contains(x.Item1));199 var bestNeighbor = (maximization ? pairs.MaxItems(p => p.Item3)200 : pairs.MinItems(p => p.Item3)).First();200 var neighborhood = particles.Where(x => neighbors[n].Contains(x.Item1)); 201 var bestNeighbor = (maximization ? neighborhood.MaxItems(p => p.Item3) 202 : neighborhood.MinItems(p => p.Item3)).First(); 201 203 neighborBest[n] = bestNeighbor.Item2; 202 204 neighborBestQuality[n] = new DoubleValue(bestNeighbor.Item3); 205 if (double.IsNaN(overallBest) || maximization && bestNeighbor.Item3 > overallBest 206 || !maximization && bestNeighbor.Item3 < overallBest) { 207 overallBest = bestNeighbor.Item3; 208 overallBestVector = bestNeighbor.Item2; 209 } 203 210 } 204 211 NeighborBestParameter.ActualValue = neighborBest; 205 212 NeighborBestQualityParameter.ActualValue = neighborBestQuality; 213 SwarmBestQualityParameter.ActualValue = new DoubleValue(overallBest); 214 BestRealVectorParameter.ActualValue = overallBestVector; 206 215 } else { 207 216 // Neighbor best = Global best 208 217 var best = maximization ? particles.MaxItems(x => x.Item3).First() : particles.MinItems(x => x.Item3).First(); 209 NeighborBestParameter.ActualValue = new ItemArray<RealVector>(particles.Select(x => best.Item2)); 210 NeighborBestQualityParameter.ActualValue = new ItemArray<DoubleValue>(particles.Select(x => new DoubleValue(best.Item3))); 218 NeighborBestParameter.ActualValue = new ItemArray<RealVector>(Enumerable.Repeat(best.Item2, particles.Count)); 219 NeighborBestQualityParameter.ActualValue = new ItemArray<DoubleValue>(Enumerable.Repeat(new DoubleValue(best.Item3), particles.Count)); 220 SwarmBestQualityParameter.ActualValue = new DoubleValue(best.Item3); 221 BestRealVectorParameter.ActualValue = best.Item2; 211 222 } 212 223 } … … 224 235 !maximization && p.Item3 < personalBestQuality[p.Item1].Value) { 225 236 personalBestQuality[p.Item1].Value = p.Item3; 226 personalBest[p.Item1] = (RealVector)p.Item2.Clone();237 personalBest[p.Item1] = new RealVector(p.Item2); 227 238 } 228 239 }
Note: See TracChangeset
for help on using the changeset viewer.