Changeset 15181 for trunk/sources/HeuristicLab.Encodings.RealVectorEncoding
- Timestamp:
- 07/10/17 17:26:43 (8 years ago)
- Location:
- trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/SPSO2011ParticleUpdater.cs ¶
r15102 r15181 25 25 using HeuristicLab.Data; 26 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 using HeuristicLab.Random; 27 28 28 29 namespace HeuristicLab.Encodings.RealVectorEncoding { … … 42 43 public static void UpdateVelocity(IRandom random, RealVector velocity, double maxVelocity, RealVector position, double inertia, RealVector personalBest, double personalBestAttraction, RealVector neighborBest, double neighborBestAttraction, double c = 1.193) { 43 44 var gravity = new double[velocity.Length]; 44 var direct = new RealVector(velocity.Length);45 var direction = new RealVector(velocity.Length); 45 46 var radius = 0.0; 47 48 var nd = new NormalDistributedRandom(random, 0, 1); 46 49 47 50 for (int i = 0; i < velocity.Length; i++) { 48 51 var g_id = c * ((personalBest[i] + neighborBest[i] - 2 * position[i]) / 3.0); 52 // center of the hyper-sphere 49 53 gravity[i] = g_id + position[i]; 50 direct[i] = (random.NextDouble() - 0.5) * 2; 54 // a random direction vector uniform over the surface of hyper-sphere, see http://mathworld.wolfram.com/HyperspherePointPicking.html 55 direction[i] = nd.NextDouble(); 51 56 radius += g_id * g_id; 52 57 } 53 58 59 // randomly choose a radius within the hyper-sphere 54 60 radius = random.NextDouble() * Math.Sqrt(radius); 55 61 56 var unitscale = Math.Sqrt(direct.DotProduct(direct)); 62 // unitscale is used to rescale the random direction vector to unit length, resp. length of the radius 63 var unitscale = Math.Sqrt(direction.DotProduct(direction)); 57 64 if (unitscale > 0) { 58 65 for (var i = 0; i < velocity.Length; i++) { 59 velocity[i] = velocity[i] * inertia + gravity[i] + direct[i] * radius / unitscale - position[i]; 66 var sampledPos = gravity[i] + direction[i] * radius / unitscale; 67 velocity[i] = velocity[i] * inertia + sampledPos - position[i]; 60 68 } 61 69 } -
TabularUnified 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 } -
TabularUnified trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/RealVector.cs ¶
r15114 r15181 45 45 array[i] = elements[i]; 46 46 } 47 public RealVector(RealVector other) 48 : this(other.Length) { 49 Array.Copy(other.array, array, other.Length); 50 } 47 51 48 52 public override IDeepCloneable Clone(Cloner cloner) {
Note: See TracChangeset
for help on using the changeset viewer.