- Timestamp:
- 07/21/17 11:18:40 (7 years ago)
- Location:
- stable
- Files:
-
- 7 edited
- 8 copied
Legend:
- Unmodified
- Added
- Removed
-
stable
- Property svn:mergeinfo changed
/trunk/sources merged: 15071,15076,15091-15093,15096,15102,15114,15181,15201,15214,15223-15224,15228
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Encodings.RealVectorEncoding
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Encodings.RealVectorEncoding merged: 15071,15091,15096,15102,15114,15181,15201,15214
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorNeighborhoodParticleUpdater.cs
r14186 r15277 20 20 #endregion 21 21 22 using System; 22 23 using HeuristicLab.Common; 23 24 using HeuristicLab.Core; 24 using HeuristicLab.Optimization;25 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 26 using HeuristicLab.PluginInfrastructure; 26 27 27 28 namespace HeuristicLab.Encodings.RealVectorEncoding { 28 29 [Item("Neighborhood Particle Updater", "Updates the particle's position using (among other things) the best neighbor's position. Point = Point + Velocity*Inertia + (PersonalBestPoint-Point)*Phi_P*r_p + (BestNeighborPoint-Point)*Phi_G*r_g.")] 29 30 [StorableClass] 30 public sealed class RealVectorNeighborhoodParticleUpdater : RealVectorParticleUpdater, ILocalParticleUpdater { 31 [NonDiscoverableType] 32 [Obsolete("Use SPSO2011ParticleUpdater")] 33 internal sealed class RealVectorNeighborhoodParticleUpdater : RealVectorParticleUpdater { 31 34 32 35 #region Construction & Cloning -
stable/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorParticleCreator.cs
r14186 r15277 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; 27 using HeuristicLab.Optimization; 26 28 using HeuristicLab.Parameters; 27 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 30 using HeuristicLab.PluginInfrastructure; 28 31 29 32 namespace HeuristicLab.Encodings.RealVectorEncoding { 30 33 [Item("RealVectorParticleCreator", "Creates a particle with position, zero velocity vector and personal best.")] 31 34 [StorableClass] 32 public class RealVectorParticleCreator : AlgorithmOperator, IRealVectorParticleCreator { 35 [NonDiscoverableType] 36 [Obsolete("Use SPSOParticleCreator")] 37 internal class RealVectorParticleCreator : AlgorithmOperator, IRealVectorParticleCreator { 33 38 34 39 #region Parameters … … 47 52 public ILookupParameter<RealVector> VelocityParameter { 48 53 get { return (ILookupParameter<RealVector>)Parameters["Velocity"]; } 54 } 55 public ILookupParameter<ISolutionCreator> SolutionCreatorParameter { 56 get { return (ILookupParameter<ISolutionCreator>)Parameters["SolutionCreator"]; } 49 57 } 50 58 #endregion … … 70 78 Parameters.Add(new LookupParameter<RealVector>("PersonalBest", "Particle's personal best solution.")); 71 79 Parameters.Add(new LookupParameter<RealVector>("Velocity", "Particle's current velocity.")); 80 Parameters.Add(new LookupParameter<ISolutionCreator>("SolutionCreator", "The operator that creates the initial position.")); 72 81 73 82 UniformRandomRealVectorCreator realVectorCreater = new UniformRandomRealVectorCreator(); … … 94 103 return base.Apply(); 95 104 } 96 97 105 106 [StorableHook(HookType.AfterDeserialization)] 107 private void AfterDeserialization() { 108 if (!Parameters.ContainsKey("SolutionCreator")) 109 Parameters.Add(new LookupParameter<ISolutionCreator>("SolutionCreator", "The operator that creates the initial position.")); 110 } 98 111 } 99 112 } -
stable/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorParticleUpdater.cs
r14186 r15277 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 { … … 62 65 public ILookupParameter<DoubleMatrix> CurrentVelocityBoundsParameter { 63 66 get { return (ILookupParameter<DoubleMatrix>)Parameters["CurrentVelocityBounds"]; } 67 } 68 public ILookupParameter<DoubleValue> CurrentMaxVelocityParameter { 69 get { return (ILookupParameter<DoubleValue>)Parameters["CurrentMaxVelocity"]; } 64 70 } 65 71 public ILookupParameter<DoubleValue> InertiaParameter { … … 126 132 Parameters.Add(new LookupParameter<DoubleMatrix>("Bounds", "The lower and upper bounds for each dimension of the position vector for the current problem.")); 127 133 Parameters.Add(new LookupParameter<DoubleMatrix>("CurrentVelocityBounds", "Upper and lower bounds for the particle's velocity vector.")); 134 Parameters.Add(new LookupParameter<DoubleValue>("CurrentMaxVelocity", "Maximum for the particle's velocity vector.")); 128 135 Parameters.Add(new LookupParameter<DoubleValue>("CurrentInertia", "The weight for the particle's velocity vector.")); 129 136 Parameters.Add(new LookupParameter<DoubleValue>("PersonalBestAttraction", "The weight for the particle's personal best position.")); … … 131 138 } 132 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 } 133 146 134 147 protected void MoveParticle(RealVector velocity, RealVector position) { -
stable/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorSwarmUpdater.cs
r14186 r15277 35 35 [Item("RealVectorSwarmUpdater", "Updates personal best point and quality as well as global best point and quality.")] 36 36 [StorableClass] 37 public sealed class RealVectorSwarmUpdater : SingleSuccessorOperator, IRealVectorSwarmUpdater, ISingleObjectiveOperator { 37 [NonDiscoverableType] 38 [Obsolete("Use SPSOSwarmUpdater")] 39 internal sealed class RealVectorSwarmUpdater : SingleSuccessorOperator, IRealVectorSwarmUpdater, ISingleObjectiveOperator { 38 40 39 41 [Storable] -
stable/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorTotallyConnectedParticleUpdater.cs
r14186 r15277 20 20 #endregion 21 21 22 using System; 22 23 using HeuristicLab.Common; 23 24 using HeuristicLab.Core; 24 using HeuristicLab.Optimization;25 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 26 using HeuristicLab.PluginInfrastructure; 26 27 27 28 namespace HeuristicLab.Encodings.RealVectorEncoding { 28 29 [Item("Totally Connected Particle Updater", "Updates the particle's position using (among other things) the global best position. Use together with the empty topology initialzer. Point = Point + Velocity*Inertia + (PersonalBestPoint-Point)*Phi_P*r_p + (BestPoint-Point)*Phi_G*r_g")] 29 30 [StorableClass] 30 public sealed class RealVectorTotallyConnectedParticleUpdater : RealVectorParticleUpdater, IGlobalParticleUpdater { 31 [NonDiscoverableType] 32 [Obsolete("Use SPSO2011ParticleUpdater")] 33 internal sealed class RealVectorTotallyConnectedParticleUpdater : RealVectorParticleUpdater { 31 34 32 35 #region Construction & Cloning -
stable/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/SPSO2007ParticleUpdater.cs
r15096 r15277 24 24 using HeuristicLab.Core; 25 25 using HeuristicLab.Data; 26 using HeuristicLab.Parameters; 26 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 28 … … 29 30 [Item("SPSO 2007 Particle Updater", "Updates the particle's position according to the formulae described in SPSO 2007.")] 30 31 [StorableClass] 31 public sealed class SPSO2007ParticleUpdater : RealVectorParticleUpdater {32 public sealed class SPSO2007ParticleUpdater : SPSOParticleUpdater { 32 33 33 34 #region Construction & Cloning … … 41 42 #endregion 42 43 43 public static void UpdateVelocity(IRandom random, RealVector velocity, double maxVelocity, RealVector position, double inertia, RealVector personalBest, double personalBestAttraction, RealVector neighborBest, double neighborBestAttraction) {44 public static void UpdateVelocity(IRandom random, RealVector velocity, RealVector position, RealVector personalBest, RealVector neighborBest, double inertia = 0.721, double personalBestAttraction = 1.193, double neighborBestAttraction = 1.193, double maxVelocity = double.MaxValue) { 44 45 for (int i = 0; i < velocity.Length; i++) { 45 double r_p = random.NextDouble() * 1.193;46 double r_g = random.NextDouble() * 1.193;46 double r_p = random.NextDouble(); 47 double r_g = random.NextDouble(); 47 48 velocity[i] = 48 49 velocity[i] * inertia + … … 69 70 if (position[i] < min) { 70 71 position[i] = min; 71 velocity[i] = 0; 72 velocity[i] = 0; // SPSO 2007 72 73 } 73 74 if (position[i] > max) { 74 75 position[i] = max; 75 velocity[i] = 0; 76 velocity[i] = 0; // SPSO 2007 76 77 } 77 78 } … … 90 91 var neighborBest = NeighborBestParameter.ActualValue; 91 92 var neighborBestAttraction = NeighborBestAttractionParameter.ActualValue.Value; 92 93 UpdateVelocity(random, velocity, maxVelocity, position, inertia, personalBest, personalBestAttraction, neighborBest, neighborBestAttraction);93 94 UpdateVelocity(random, velocity, position, personalBest, neighborBest, inertia, personalBestAttraction, neighborBestAttraction, maxVelocity); 94 95 UpdatePosition(bounds, velocity, position); 95 96 -
stable/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/SPSO2011ParticleUpdater.cs
r15096 r15277 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 { 29 30 [Item("SPSO 2011 Particle Updater", "Updates the particle's position according to the formulae described in SPSO 2011.")] 30 31 [StorableClass] 31 public sealed class SPSO2011ParticleUpdater : RealVectorParticleUpdater { 32 32 public sealed class SPSO2011ParticleUpdater : SPSOParticleUpdater { 33 33 #region Construction & Cloning 34 34 [StorableConstructor] … … 41 41 #endregion 42 42 43 public static void UpdateVelocity(IRandom random, RealVector velocity, double maxVelocity, RealVector position, double inertia, RealVector personalBest, double personalBestAttraction, RealVector neighborBest, double neighborBestAttraction) {43 public static void UpdateVelocity(IRandom random, RealVector velocity, RealVector position, RealVector personalBest, RealVector neighborBest, double inertia = 0.721, double personalBestAttraction = 1.193, double neighborBestAttraction = 1.193, double maxVelocity = double.MaxValue) { 44 44 var gravity = new double[velocity.Length]; 45 var direct = new RealVector(velocity.Length);45 var direction = new RealVector(velocity.Length); 46 46 var radius = 0.0; 47 47 48 var nd = new NormalDistributedRandom(random, 0, 1); 49 48 50 for (int i = 0; i < velocity.Length; i++) { 49 var g_id = 1.193 * ((personalBest[i] + neighborBest[i] - 2 * position[i]) / 3.0); 51 var g_id = (personalBestAttraction * personalBest[i] 52 + neighborBestAttraction * neighborBest[i] 53 - position[i] * (neighborBestAttraction + personalBestAttraction)) / 3.0; 54 // center of the hyper-sphere 50 55 gravity[i] = g_id + position[i]; 51 direct[i] = (random.NextDouble() - 0.5) * 2; 56 // a random direction vector uniform over the surface of hyper-sphere, see http://mathworld.wolfram.com/HyperspherePointPicking.html 57 direction[i] = nd.NextDouble(); 52 58 radius += g_id * g_id; 53 59 } 54 60 61 // randomly choose a radius within the hyper-sphere 55 62 radius = random.NextDouble() * Math.Sqrt(radius); 56 63 57 var unitscale = Math.Sqrt(direct.DotProduct(direct)); 64 // unitscale is used to rescale the random direction vector to unit length, resp. length of the radius 65 var unitscale = Math.Sqrt(direction.DotProduct(direction)); 58 66 if (unitscale > 0) { 59 67 for (var i = 0; i < velocity.Length; i++) { 60 velocity[i] = velocity[i] * inertia + gravity[i] + direct[i] * radius / unitscale - position[i]; 68 var sampledPos = gravity[i] + direction[i] * radius / unitscale; 69 velocity[i] = velocity[i] * inertia + sampledPos - position[i]; 61 70 } 62 71 } … … 101 110 var neighborBest = NeighborBestParameter.ActualValue; 102 111 var neighborBestAttraction = NeighborBestAttractionParameter.ActualValue.Value; 103 104 UpdateVelocity(random, velocity, maxVelocity, position, inertia, personalBest, personalBestAttraction, neighborBest, neighborBestAttraction);112 113 UpdateVelocity(random, velocity, position, personalBest, neighborBest, inertia, personalBestAttraction, neighborBestAttraction, maxVelocity); 105 114 UpdatePosition(bounds, velocity, position); 106 115 -
stable/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/SPSOParticleUpdater.cs
r15102 r15277 71 71 get { return (ILookupParameter<DoubleValue>)Parameters["NeighborBestAttraction"]; } 72 72 } 73 74 public IValueLookupParameter<DoubleValue> MaxBeyondBestParameter {75 get { return (IValueLookupParameter<DoubleValue>)Parameters["MaxBeyondBest"]; }76 }77 73 #endregion 78 74 … … 93 89 Parameters.Add(new LookupParameter<DoubleValue>("PersonalBestAttraction", "The weight for the particle's personal best position.")); 94 90 Parameters.Add(new LookupParameter<DoubleValue>("NeighborBestAttraction", "The weight for the global best position.")); 95 Parameters.Add(new ValueLookupParameter<DoubleValue>("MaxBeyondBest", "A factor of how much the velocity update may maximally aim beyond the personal and neighbor best.", new DoubleValue(1.193)));96 91 } 97 92 #endregion -
stable/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/SPSOSwarmUpdater.cs
r15102 r15277 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.