Changeset 15102
- Timestamp:
- 06/30/17 21:57:38 (7 years ago)
- Location:
- trunk/sources
- Files:
-
- 6 added
- 1 deleted
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/ParticleSwarmOptimization.cs
r15096 r15102 36 36 37 37 namespace HeuristicLab.Algorithms.ParticleSwarmOptimization { 38 [Item("Particle Swarm Optimization (PSO)", "A particle swarm optimization algorithm based on the description in Pedersen, M.E.H. (2010). PhD thesis. University of Southampton.")]38 [Item("Particle Swarm Optimization (PSO)", "A particle swarm optimization algorithm based on Standard PSO (SPSO) as described in Clerc, M. (2012). Standard particle swarm optimisation.")] 39 39 [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 300)] 40 40 [StorableClass] -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/HeuristicLab.Encodings.RealVectorEncoding-3.3.csproj
r15096 r15102 116 116 <Compile Include="Creators\NormalDistributedRealVectorCreator.cs" /> 117 117 <Compile Include="Interfaces\IRealVectorMultiNeighborhoodShakingOperator.cs" /> 118 <Compile Include="ParticleOperators\RealVectorNeighborhoodParticleUpdater.cs" /> 119 <Compile Include="ParticleOperators\RealVectorParticleCreator.cs" /> 120 <Compile Include="ParticleOperators\RealVectorParticleUpdater.cs" /> 121 <Compile Include="ParticleOperators\RealVectorSwarmUpdater.cs" /> 122 <Compile Include="ParticleOperators\RealVectorTotallyConnectedParticleUpdater.cs" /> 123 <Compile Include="ParticleOperators\SPSO2007VelocityInitializer.cs" /> 124 <Compile Include="ParticleOperators\SPSOVelocityInitializer.cs" /> 118 125 <Compile Include="ParticleOperators\SPSO2011ParticleUpdater.cs" /> 119 126 <Compile Include="ParticleOperators\SPSO2007ParticleUpdater.cs" /> 120 <Compile Include="ParticleOperators\ RealVectorParticleCreator.cs" />127 <Compile Include="ParticleOperators\SPSOParticleCreator.cs" /> 121 128 <Compile Include="Crossovers\BlendAlphaBetaCrossover.cs" /> 122 129 <Compile Include="Interfaces\IRealVectorManipulator.cs" /> … … 130 137 <Compile Include="Interfaces\IRealVectorSwarmUpdater.cs" /> 131 138 <Compile Include="Manipulators\SelfAdaptiveNormalAllPositionsManipulator.cs" /> 132 <Compile Include="ParticleOperators\RealVectorNeighborhoodParticleUpdater.cs" /> 133 <Compile Include="ParticleOperators\RealVectorParticleUpdater.cs" /> 134 <Compile Include="ParticleOperators\RealVectorSwarmUpdater.cs" /> 135 <Compile Include="ParticleOperators\RealVectorTotallyConnectedParticleUpdater.cs" /> 136 <Compile Include="ParticleOperators\RealVectorVelocityInitializer.cs" /> 139 <Compile Include="ParticleOperators\SPSOParticleUpdater.cs" /> 140 <Compile Include="ParticleOperators\SPSO2011VelocityInitializer.cs" /> 141 <Compile Include="ParticleOperators\SPSOSwarmUpdater.cs" /> 137 142 <Compile Include="Plugin.cs" /> 138 143 <Compile Include="RealVectorCreator.cs" /> -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorNeighborhoodParticleUpdater.cs
r15096 r15102 30 30 [StorableClass] 31 31 [NonDiscoverableType] 32 [Obsolete(" Replaced by SPSO2007ParticleUpdater")]32 [Obsolete("Use SPSO2011ParticleUpdater")] 33 33 internal sealed class RealVectorNeighborhoodParticleUpdater : RealVectorParticleUpdater { 34 34 … … 43 43 #endregion 44 44 45 private void UpdateVelocity() { 46 var velocity = VelocityParameter.ActualValue; 47 var position = RealVectorParameter.ActualValue; 48 var inertia = CurrentInertiaParameter.ActualValue.Value; 49 var personalBest = PersonalBestParameter.ActualValue; 50 var personalBestAttraction = PersonalBestAttractionParameter.ActualValue.Value; 51 var neighborBest = NeighborBestParameter.ActualValue; 52 var neighborBestAttraction = NeighborBestAttractionParameter.ActualValue.Value; 45 public override IOperation Apply() { 46 double inertia = Inertia.Value; 47 double personalBestAttraction = PersonalBestAttraction.Value; 48 double neighborBestAttraction = NeighborBestAttraction.Value; 53 49 54 var random = RandomParameter.ActualValue; 50 RealVector velocity = new RealVector(Velocity.Length); 51 RealVector position = new RealVector(RealVector.Length); 52 double r_p = Random.NextDouble(); 53 double r_g = Random.NextDouble(); 55 54 56 55 for (int i = 0; i < velocity.Length; i++) { 57 double r_p = random.NextDouble();58 double r_g = random.NextDouble();59 56 velocity[i] = 60 velocity[i] * inertia +61 ( personalBest[i] - position[i]) * personalBestAttraction * r_p +62 ( neighborBest[i] - position[i]) * neighborBestAttraction * r_g;57 Velocity[i] * inertia + 58 (PersonalBest[i] - RealVector[i]) * personalBestAttraction * r_p + 59 (BestPoint[i] - RealVector[i]) * neighborBestAttraction * r_g; 63 60 } 64 61 65 var maxVelocity = CurrentMaxVelocityParameter.ActualValue.Value; 66 var speed = Math.Sqrt(velocity.DotProduct(velocity)); 67 if (speed > maxVelocity) { 68 for (var i = 0; i < velocity.Length; i++) { 69 velocity[i] *= maxVelocity / speed; 70 } 71 } 72 } 73 74 private void UpdatePosition() { 75 var velocity = VelocityParameter.ActualValue; 76 var position = RealVectorParameter.ActualValue; 77 78 for (int i = 0; i < velocity.Length; i++) { 79 position[i] += velocity[i]; 80 } 81 82 var bounds = BoundsParameter.ActualValue; 83 for (int i = 0; i < position.Length; i++) { 84 double min = bounds[i % bounds.Rows, 0]; 85 double max = bounds[i % bounds.Rows, 1]; 86 if (position[i] < min) { 87 position[i] = min; 88 velocity[i] = -0.5 * velocity[i]; // SPSO 2011 89 } 90 if (position[i] > max) { 91 position[i] = max; 92 velocity[i] = -0.5 * velocity[i]; // SPSO 2011 93 } 94 } 95 } 96 97 public override IOperation Apply() { 98 UpdateVelocity(); 99 UpdatePosition(); 62 MoveParticle(velocity, position); 100 63 101 64 return base.Apply(); -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorParticleCreator.cs
r15091 r15102 20 20 #endregion 21 21 22 using System; 22 23 using HeuristicLab.Common; 23 24 using HeuristicLab.Core; … … 27 28 using HeuristicLab.Parameters; 28 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 30 using HeuristicLab.PluginInfrastructure; 29 31 30 32 namespace HeuristicLab.Encodings.RealVectorEncoding { 31 33 [Item("RealVectorParticleCreator", "Creates a particle with position, zero velocity vector and personal best.")] 32 34 [StorableClass] 33 public class RealVectorParticleCreator : AlgorithmOperator, IRealVectorParticleCreator, IStochasticOperator { 35 [NonDiscoverableType] 36 [Obsolete("Use SPSOParticleCreator")] 37 internal class RealVectorParticleCreator : AlgorithmOperator, IRealVectorParticleCreator { 34 38 35 39 #region Parameters 36 public ILookupParameter<I Random> RandomParameter {37 get { return (ILookupParameter<I Random>)Parameters["Random"]; }40 public ILookupParameter<IntValue> ProblemSizeParameter { 41 get { return (ILookupParameter<IntValue>)Parameters["ProblemSize"]; } 38 42 } 39 43 public IValueLookupParameter<DoubleMatrix> BoundsParameter { … … 53 57 } 54 58 #endregion 55 59 60 #region Parameter Values 61 protected int ProblemSize { 62 get { return ProblemSizeParameter.ActualValue.Value; } 63 } 64 protected RealVector Velocity { 65 set { VelocityParameter.ActualValue = value; } 66 } 67 #endregion 68 56 69 #region Construction & Cloning 57 70 [StorableConstructor] … … 60 73 public RealVectorParticleCreator() 61 74 : base() { 62 Parameters.Add(new LookupParameter<I Random>("Random", "The random number generator to use."));75 Parameters.Add(new LookupParameter<IntValue>("ProblemSize", "The dimension of the problem.")); 63 76 Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Bounds", "The lower and upper bounds in each dimension.")); 64 77 Parameters.Add(new LookupParameter<RealVector>("RealVector", "Particle's current solution")); … … 67 80 Parameters.Add(new LookupParameter<ISolutionCreator>("SolutionCreator", "The operator that creates the initial position.")); 68 81 69 Placeholder realVectorCreater = new Placeholder();82 UniformRandomRealVectorCreator realVectorCreater = new UniformRandomRealVectorCreator(); 70 83 Assigner personalBestPositionAssigner = new Assigner(); 71 RealVectorVelocityInitializer velocityInitializer = new RealVectorVelocityInitializer();72 84 73 85 OperatorGraph.InitialOperator = realVectorCreater; 74 86 75 realVectorCreater.OperatorParameter.ActualName = SolutionCreatorParameter.Name; 87 realVectorCreater.RealVectorParameter.ActualName = RealVectorParameter.Name; 88 realVectorCreater.LengthParameter.ActualName = ProblemSizeParameter.Name; 89 realVectorCreater.BoundsParameter.ActualName = BoundsParameter.Name; 76 90 realVectorCreater.Successor = personalBestPositionAssigner; 77 91 78 92 personalBestPositionAssigner.LeftSideParameter.ActualName = PersonalBestParameter.Name; 79 93 personalBestPositionAssigner.RightSideParameter.ActualName = RealVectorParameter.Name; 80 personalBestPositionAssigner.Successor = velocityInitializer; 81 82 velocityInitializer.BoundsParameter.ActualName = BoundsParameter.Name; 83 velocityInitializer.RandomParameter.ActualName = RandomParameter.Name; 84 velocityInitializer.RealVectorParameter.ActualName = RealVectorParameter.Name; 85 velocityInitializer.VelocityParameter.ActualName = VelocityParameter.Name; 86 velocityInitializer.Successor = null; 94 personalBestPositionAssigner.Successor = null; 87 95 } 88 96 public override IDeepCloneable Clone(Cloner cloner) { … … 90 98 } 91 99 #endregion 100 101 public override IOperation Apply() { 102 Velocity = new RealVector(ProblemSize); 103 return base.Apply(); 104 } 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 } 92 111 } 93 112 } -
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 } -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorSwarmUpdater.cs
r15096 r15102 21 21 22 22 using System; 23 using System.Collections.Generic;24 23 using System.Linq; 25 24 using HeuristicLab.Common; … … 36 35 [Item("RealVectorSwarmUpdater", "Updates personal best point and quality as well as global best point and quality.")] 37 36 [StorableClass] 38 public sealed class RealVectorSwarmUpdater : SingleSuccessorOperator, IRealVectorSwarmUpdater, ISingleObjectiveOperator { 37 [NonDiscoverableType] 38 [Obsolete("Use SPSOSwarmUpdater")] 39 internal sealed class RealVectorSwarmUpdater : SingleSuccessorOperator, IRealVectorSwarmUpdater, ISingleObjectiveOperator { 39 40 40 41 [Storable] … … 76 77 get { return (IScopeTreeLookupParameter<IntArray>)Parameters["Neighbors"]; } 77 78 } 78 public IValueLookupParameter<Double Value> MaxVelocityParameter {79 get { return (ValueLookupParameter<Double Value>)Parameters["MaxVelocity"]; }80 } 81 public ILookupParameter<Double Value> CurrentMaxVelocityParameter {82 get { return (ILookupParameter<Double Value>)Parameters["CurrentMaxVelocity"]; }79 public IValueLookupParameter<DoubleMatrix> VelocityBoundsParameter { 80 get { return (ValueLookupParameter<DoubleMatrix>)Parameters["VelocityBounds"]; } 81 } 82 public ILookupParameter<DoubleMatrix> CurrentVelocityBoundsParameter { 83 get { return (ILookupParameter<DoubleMatrix>)Parameters["CurrentVelocityBounds"]; } 83 84 } 84 85 public LookupParameter<ResultCollection> ResultsParameter { … … 86 87 } 87 88 88 #region Max Velocity Updating 89 public IConstrainedValueParameter<IDiscreteDoubleValueModifier> MaxVelocityScalingOperatorParameter { 90 get { return (IConstrainedValueParameter<IDiscreteDoubleValueModifier>)Parameters["MaxVelocityScalingOperator"]; } 91 } 92 public IValueLookupParameter<DoubleValue> FinalMaxVelocityParameter { 93 get { return (IValueLookupParameter<DoubleValue>)Parameters["FinalMaxVelocity"]; } 94 } 95 public ILookupParameter<IntValue> MaxVelocityIndexParameter { 96 get { return (ILookupParameter<IntValue>)Parameters["MaxVelocityIndex"]; } 97 } 98 public IValueLookupParameter<IntValue> MaxVelocityStartIndexParameter { 99 get { return (IValueLookupParameter<IntValue>)Parameters["MaxVelocityStartIndex"]; } 100 } 101 public IValueLookupParameter<IntValue> MaxVelocityEndIndexParameter { 102 get { return (IValueLookupParameter<IntValue>)Parameters["MaxVelocityEndIndex"]; } 89 #region Velocity Bounds Updating 90 public ILookupParameter<DoubleValue> VelocityBoundsScaleParameter { 91 get { return (ILookupParameter<DoubleValue>)Parameters["VelocityBoundsScale"]; } 92 } 93 public IConstrainedValueParameter<IDiscreteDoubleValueModifier> VelocityBoundsScalingOperatorParameter { 94 get { return (IConstrainedValueParameter<IDiscreteDoubleValueModifier>)Parameters["VelocityBoundsScalingOperator"]; } 95 } 96 public IValueLookupParameter<DoubleValue> VelocityBoundsStartValueParameter { 97 get { return (IValueLookupParameter<DoubleValue>)Parameters["VelocityBoundsStartValue"]; } 98 } 99 public IValueLookupParameter<DoubleValue> VelocityBoundsEndValueParameter { 100 get { return (IValueLookupParameter<DoubleValue>)Parameters["VelocityBoundsEndValue"]; } 101 } 102 public ILookupParameter<IntValue> VelocityBoundsIndexParameter { 103 get { return (ILookupParameter<IntValue>)Parameters["VelocityBoundsIndex"]; } 104 } 105 public IValueLookupParameter<IntValue> VelocityBoundsStartIndexParameter { 106 get { return (IValueLookupParameter<IntValue>)Parameters["VelocityBoundsStartIndex"]; } 107 } 108 public IValueLookupParameter<IntValue> VelocityBoundsEndIndexParameter { 109 get { return (IValueLookupParameter<IntValue>)Parameters["VelocityBoundsEndIndex"]; } 103 110 } 104 111 #endregion 105 112 106 113 #endregion 107 114 115 #region Parameter values 116 private DoubleValue SwarmBestQuality { 117 get { return SwarmBestQualityParameter.ActualValue; } 118 set { SwarmBestQualityParameter.ActualValue = value; } 119 } 120 private RealVector BestRealVector { 121 get { return BestRealVectorParameter.ActualValue; } 122 set { BestRealVectorParameter.ActualValue = value; } 123 } 124 private ItemArray<DoubleValue> Quality { 125 get { return QualityParameter.ActualValue; } 126 } 127 private ItemArray<DoubleValue> PersonalBestQuality { 128 get { return PersonalBestQualityParameter.ActualValue; } 129 set { PersonalBestQualityParameter.ActualValue = value; } 130 } 131 private ItemArray<DoubleValue> NeighborBestQuality { 132 get { return NeighborBestQualityParameter.ActualValue; } 133 set { NeighborBestQualityParameter.ActualValue = value; } 134 } 135 private ItemArray<RealVector> RealVector { 136 get { return RealVectorParameter.ActualValue; } 137 } 138 private ItemArray<RealVector> PersonalBest { 139 get { return PersonalBestParameter.ActualValue; } 140 set { PersonalBestParameter.ActualValue = value; } 141 } 142 private ItemArray<RealVector> NeighborBest { 143 get { return NeighborBestParameter.ActualValue; } 144 set { NeighborBestParameter.ActualValue = value; } 145 } 146 private bool Maximization { 147 get { return MaximizationParameter.ActualValue.Value; } 148 } 149 private ItemArray<IntArray> Neighbors { 150 get { return NeighborsParameter.ActualValue; } 151 } 152 private DoubleMatrix VelocityBounds { 153 get { return VelocityBoundsParameter.ActualValue; } 154 } 155 private DoubleMatrix CurrentVelocityBounds { 156 get { return CurrentVelocityBoundsParameter.ActualValue; } 157 set { CurrentVelocityBoundsParameter.ActualValue = value; } 158 } 159 private DoubleValue VelocityBoundsScale { 160 get { return VelocityBoundsScaleParameter.ActualValue; } 161 set { VelocityBoundsScaleParameter.ActualValue = value; } 162 } 163 private DoubleValue VelocityBoundsStartValue { 164 get { return VelocityBoundsStartValueParameter.ActualValue; } 165 } 166 public IDiscreteDoubleValueModifier VelocityBoundsScalingOperator { 167 get { return VelocityBoundsScalingOperatorParameter.Value; } 168 set { VelocityBoundsScalingOperatorParameter.Value = value; } 169 } 170 private ResultCollection Results { 171 get { return ResultsParameter.ActualValue; } 172 } 173 #endregion 174 108 175 #region Construction & Cloning 109 176 … … 126 193 Parameters.Add(new ScopeTreeLookupParameter<IntArray>("Neighbors", "The list of neighbors for each particle.")); 127 194 Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false.")); 128 Parameters.Add(new ValueLookupParameter<Double Value>("MaxVelocity", "Speed limit for each particle.", new DoubleValue(1000000)));129 Parameters.Add(new LookupParameter<Double Value>("CurrentMaxVelocity", "Current value of the speed limit."));195 Parameters.Add(new ValueLookupParameter<DoubleMatrix>("VelocityBounds", "Maximum velocity for each dimension.", new DoubleMatrix(new double[,] { { -1, 1 } }))); 196 Parameters.Add(new LookupParameter<DoubleMatrix>("CurrentVelocityBounds", "Current value of velocity bounds.")); 130 197 Parameters.Add(new LookupParameter<ResultCollection>("Results", "Results")); 131 198 132 199 #region Velocity Bounds Updating 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))); 135 Parameters.Add(new LookupParameter<IntValue>("MaxVelocityIndex", "The current index.", "Iterations")); 136 Parameters.Add(new ValueLookupParameter<IntValue>("MaxVelocityStartIndex", "The start index at which to start modifying 'Value'.", new IntValue(0))); 137 Parameters.Add(new ValueLookupParameter<IntValue>("MaxVelocityEndIndex", "The end index by which 'Value' should have reached 'EndValue'.", "MaxIterations")); 138 MaxVelocityStartIndexParameter.Hidden = true; 139 MaxVelocityEndIndexParameter.Hidden = true; 200 Parameters.Add(new LookupParameter<DoubleValue>("VelocityBoundsScale", "Scale parameter.")); 201 Parameters.Add(new OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>("VelocityBoundsScalingOperator", "Modifies the value")); 202 Parameters.Add(new ValueLookupParameter<DoubleValue>("VelocityBoundsStartValue", "The start value of 'Value'.", new DoubleValue(1))); 203 Parameters.Add(new ValueLookupParameter<DoubleValue>("VelocityBoundsEndValue", "The end value of 'Value'.", new DoubleValue(1E-10))); 204 Parameters.Add(new LookupParameter<IntValue>("VelocityBoundsIndex", "The current index.", "Iterations")); 205 Parameters.Add(new ValueLookupParameter<IntValue>("VelocityBoundsStartIndex", "The start index at which to start modifying 'Value'.", new IntValue(0))); 206 Parameters.Add(new ValueLookupParameter<IntValue>("VelocityBoundsEndIndex", "The end index by which 'Value' should have reached 'EndValue'.", "MaxIterations")); 207 VelocityBoundsStartIndexParameter.Hidden = true; 208 VelocityBoundsEndIndexParameter.Hidden = true; 140 209 #endregion 141 210 142 211 Initialize(); 212 RegisterEvents(); 143 213 } 144 214 … … 158 228 Parameters.Remove("BestQuality"); 159 229 } 160 } 230 RegisterEvents(); 231 } 232 233 private void RegisterEvents() { 234 VelocityBoundsStartValueParameter.ValueChanged += new EventHandler(VelocityBoundsStartValueParameter_ValueChanged); 235 VelocityBoundsStartValueParameter.Value.ValueChanged += new EventHandler(VelocityBoundsStartValueParameter_Value_ValueChanged); 236 } 237 238 void VelocityBoundsStartValueParameter_Value_ValueChanged(object sender, EventArgs e) { 239 UpdateVelocityBoundsParamater(); 240 } 241 242 void UpdateVelocityBoundsParamater() { 243 if (VelocityBoundsParameter.Value == null) { 244 VelocityBoundsParameter.Value = new DoubleMatrix(1, 2); 245 } else if (VelocityBoundsParameter.Value.Columns != 2) { 246 VelocityBoundsParameter.Value = new DoubleMatrix(VelocityBoundsParameter.Value.Rows, 2); 247 } 248 if (VelocityBoundsStartValueParameter.Value != null) { 249 DoubleMatrix matrix = VelocityBoundsParameter.Value; 250 for (int i = 0; i < matrix.Rows; i++) { 251 matrix[i, 0] = (-1) * VelocityBoundsStartValueParameter.Value.Value; 252 matrix[i, 1] = VelocityBoundsStartValueParameter.Value.Value; 253 } 254 } 255 } 256 257 void VelocityBoundsStartValueParameter_ValueChanged(object sender, EventArgs e) { 258 if (VelocityBoundsStartValueParameter.Value != null) { 259 VelocityBoundsStartValueParameter.Value.ValueChanged += new EventHandler(VelocityBoundsStartValueParameter_Value_ValueChanged); 260 } 261 UpdateVelocityBoundsParamater(); 262 } 263 161 264 private void Initialize() { 162 265 ResultsCollector = new ResultsCollector(); 163 ResultsCollector.CollectedValues.Add(Current MaxVelocityParameter);164 ResultsCollector.CollectedValues.Add( MaxVelocityParameter);266 ResultsCollector.CollectedValues.Add(CurrentVelocityBoundsParameter); 267 ResultsCollector.CollectedValues.Add(VelocityBoundsParameter); 165 268 166 269 foreach (IDiscreteDoubleValueModifier op in ApplicationManager.Manager.GetInstances<IDiscreteDoubleValueModifier>()) { 167 MaxVelocityScalingOperatorParameter.ValidValues.Add(op);168 op.ValueParameter.ActualName = CurrentMaxVelocityParameter.Name;169 op.StartValueParameter.ActualName = MaxVelocityParameter.Name;170 op.EndValueParameter.ActualName = FinalMaxVelocityParameter.Name;171 op.IndexParameter.ActualName = MaxVelocityIndexParameter.Name;172 op.StartIndexParameter.ActualName = MaxVelocityStartIndexParameter.Name;173 op.EndIndexParameter.ActualName = MaxVelocityEndIndexParameter.Name;174 } 175 MaxVelocityScalingOperatorParameter.Value = null;270 VelocityBoundsScalingOperatorParameter.ValidValues.Add(op); 271 op.ValueParameter.ActualName = VelocityBoundsScaleParameter.Name; 272 op.StartValueParameter.ActualName = VelocityBoundsStartValueParameter.Name; 273 op.EndValueParameter.ActualName = VelocityBoundsEndValueParameter.Name; 274 op.IndexParameter.ActualName = VelocityBoundsIndexParameter.Name; 275 op.StartIndexParameter.ActualName = VelocityBoundsStartIndexParameter.Name; 276 op.EndIndexParameter.ActualName = VelocityBoundsEndIndexParameter.Name; 277 } 278 VelocityBoundsScalingOperatorParameter.Value = null; 176 279 } 177 280 178 281 public override IOperation Apply() { 179 var max = MaximizationParameter.ActualValue.Value; 180 // Update of the personal bests 181 var points = RealVectorParameter.ActualValue; 182 var qualities = QualityParameter.ActualValue; 183 var particles = points.Select((p, i) => new { Particle = p, Index = i }) 184 .Zip(qualities, (p, q) => Tuple.Create(p.Index, p.Particle, q.Value)).ToList(); 185 UpdatePersonalBest(max, particles); 186 187 // SPSO: update of the neighbor bests from the personal bests 188 var personalBestPoints = PersonalBestParameter.ActualValue; 189 var personalBestQualities = PersonalBestQualityParameter.ActualValue; 190 particles = personalBestPoints.Select((p, i) => new { Particle = p, Index = i }) 191 .Zip(personalBestQualities, (p, q) => Tuple.Create(p.Index, p.Particle, q.Value)).ToList(); 192 UpdateNeighborBest(max, particles); 193 194 var next = new OperationCollection() { base.Apply() }; 195 next.Insert(0, ExecutionContext.CreateChildOperation(ResultsCollector)); 196 if (MaxVelocityScalingOperatorParameter.Value != null) { 197 next.Insert(0, ExecutionContext.CreateChildOperation(MaxVelocityScalingOperatorParameter.Value)); 198 } else CurrentMaxVelocityParameter.ActualValue = new DoubleValue(MaxVelocityParameter.ActualValue.Value); 199 return next; 200 } 201 202 private void UpdateNeighborBest(bool maximization, IList<Tuple<int, RealVector, double>> particles) { 203 var neighbors = NeighborsParameter.ActualValue; 204 if (neighbors.Length > 0) { 205 var neighborBest = new ItemArray<RealVector>(neighbors.Length); 206 var neighborBestQuality = new ItemArray<DoubleValue>(neighbors.Length); 207 for (int n = 0; n < neighbors.Length; n++) { 208 var pairs = particles.Where(x => x.Item1 == n || neighbors[n].Contains(x.Item1)); 209 var bestNeighbor = (maximization ? pairs.MaxItems(p => p.Item3) 210 : pairs.MinItems(p => p.Item3)).First(); 211 neighborBest[n] = bestNeighbor.Item2; 212 neighborBestQuality[n] = new DoubleValue(bestNeighbor.Item3); 282 UpdateGlobalBest(); 283 UpdateNeighborBest(); 284 UpdatePersonalBest(); 285 return UpdateVelocityBounds(); 286 } 287 288 private void UpdateGlobalBest() { 289 if (SwarmBestQuality == null) 290 SwarmBestQuality = new DoubleValue(); 291 SwarmBestQuality.Value = Maximization ? Quality.Max(v => v.Value) : Quality.Min(v => v.Value); 292 BestRealVector = (RealVector)RealVector[Quality.FindIndex(v => v.Value == SwarmBestQuality.Value)].Clone(); 293 } 294 295 private void UpdateNeighborBest() { 296 if (Neighbors.Length > 0) { 297 var neighborBest = new ItemArray<RealVector>(Neighbors.Length); 298 var neighborBestQuality = new ItemArray<DoubleValue>(Neighbors.Length); 299 for (int n = 0; n < Neighbors.Length; n++) { 300 var pairs = Quality.Zip(RealVector, (q, p) => new { Quality = q, Point = p }) 301 .Where((p, i) => i == n || Neighbors[n].Contains(i)); 302 var bestNeighbor = Maximization ? 303 pairs.OrderByDescending(p => p.Quality.Value).First() : 304 pairs.OrderBy(p => p.Quality.Value).First(); 305 neighborBest[n] = bestNeighbor.Point; 306 neighborBestQuality[n] = bestNeighbor.Quality; 213 307 } 214 NeighborBestParameter.ActualValue = neighborBest; 215 NeighborBestQualityParameter.ActualValue = neighborBestQuality; 216 } else { 217 // Neighbor best = Global best 218 var best = maximization ? particles.MaxItems(x => x.Item3).First() : particles.MinItems(x => x.Item3).First(); 219 NeighborBestParameter.ActualValue = new ItemArray<RealVector>(particles.Select(x => best.Item2)); 220 NeighborBestQualityParameter.ActualValue = new ItemArray<DoubleValue>(particles.Select(x => new DoubleValue(best.Item3))); 221 } 222 } 223 224 private void UpdatePersonalBest(bool maximization, IList<Tuple<int, RealVector, double>> particles) { 225 var personalBest = PersonalBestParameter.ActualValue; 226 var personalBestQuality = PersonalBestQualityParameter.ActualValue; 227 228 if (personalBestQuality.Length == 0) { 229 personalBestQuality = new ItemArray<DoubleValue>(particles.Select(x => new DoubleValue(x.Item3))); 230 PersonalBestQualityParameter.ActualValue = personalBestQuality; 231 } 232 foreach (var p in particles) { 233 if (maximization && p.Item3 > personalBestQuality[p.Item1].Value || 234 !maximization && p.Item3 < personalBestQuality[p.Item1].Value) { 235 personalBestQuality[p.Item1].Value = p.Item3; 236 personalBest[p.Item1] = (RealVector)p.Item2.Clone(); 308 NeighborBest = neighborBest; 309 NeighborBestQuality = neighborBestQuality; 310 } 311 } 312 313 private void UpdatePersonalBest() { 314 if (PersonalBestQuality.Length == 0) 315 PersonalBestQuality = (ItemArray<DoubleValue>)Quality.Clone(); 316 for (int i = 0; i < RealVector.Length; i++) { 317 if (Maximization && Quality[i].Value > PersonalBestQuality[i].Value || 318 !Maximization && Quality[i].Value < PersonalBestQuality[i].Value) { 319 PersonalBestQuality[i].Value = Quality[i].Value; 320 PersonalBest[i] = RealVector[i]; 237 321 } 238 322 } 239 PersonalBestParameter.ActualValue = personalBest; 323 } 324 325 private IOperation UpdateVelocityBounds() { 326 if (CurrentVelocityBounds == null) 327 CurrentVelocityBounds = (DoubleMatrix)VelocityBounds.Clone(); 328 329 if (VelocityBoundsScalingOperator == null) 330 return new OperationCollection() { 331 ExecutionContext.CreateChildOperation(ResultsCollector), 332 base.Apply() 333 }; 334 335 DoubleMatrix matrix = CurrentVelocityBounds; 336 if (VelocityBoundsScale == null && VelocityBoundsStartValue != null) { 337 VelocityBoundsScale = new DoubleValue(VelocityBoundsStartValue.Value); 338 } 339 for (int i = 0; i < matrix.Rows; i++) { 340 for (int j = 0; j < matrix.Columns; j++) { 341 if (matrix[i, j] >= 0) { 342 matrix[i, j] = VelocityBoundsScale.Value; 343 } else { 344 matrix[i, j] = (-1) * VelocityBoundsScale.Value; 345 } 346 } 347 } 348 349 return new OperationCollection() { 350 ExecutionContext.CreateChildOperation(ResultsCollector), 351 ExecutionContext.CreateChildOperation(VelocityBoundsScalingOperator), 352 base.Apply() 353 }; 240 354 } 241 355 } -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorTotallyConnectedParticleUpdater.cs
r15096 r15102 30 30 [StorableClass] 31 31 [NonDiscoverableType] 32 [Obsolete(" Replaced by SPSO2007ParticleUpdater")]32 [Obsolete("Use SPSO2011ParticleUpdater")] 33 33 internal sealed class RealVectorTotallyConnectedParticleUpdater : RealVectorParticleUpdater { 34 34 … … 43 43 #endregion 44 44 45 private void UpdateVelocity() { 46 var velocity = VelocityParameter.ActualValue; 47 var position = RealVectorParameter.ActualValue; 48 var inertia = CurrentInertiaParameter.ActualValue.Value; 49 var personalBest = PersonalBestParameter.ActualValue; 50 var personalBestAttraction = PersonalBestAttractionParameter.ActualValue.Value; 51 var neighborBest = NeighborBestParameter.ActualValue; 52 var neighborBestAttraction = NeighborBestAttractionParameter.ActualValue.Value; 45 public override IOperation Apply() { 46 double inertia = Inertia.Value; 47 double personalBestAttraction = PersonalBestAttraction.Value; 48 double neighborBestAttraction = NeighborBestAttraction.Value; 53 49 54 var random = RandomParameter.ActualValue; 50 RealVector velocity = new RealVector(Velocity.Length); 51 RealVector position = new RealVector(RealVector.Length); 52 double r_p = Random.NextDouble(); 53 double r_g = Random.NextDouble(); 55 54 56 55 for (int i = 0; i < velocity.Length; i++) { 57 double r_p = random.NextDouble();58 double r_g = random.NextDouble();59 56 velocity[i] = 60 velocity[i] * inertia +61 ( personalBest[i] - position[i]) * personalBestAttraction * r_p +62 ( neighborBest[i] - position[i]) * neighborBestAttraction * r_g;57 Velocity[i] * inertia + 58 (PersonalBest[i] - RealVector[i]) * personalBestAttraction * r_p + 59 (BestPoint[i] - RealVector[i]) * neighborBestAttraction * r_g; 63 60 } 64 61 65 var maxVelocity = CurrentMaxVelocityParameter.ActualValue.Value; 66 var speed = Math.Sqrt(velocity.DotProduct(velocity)); 67 if (speed > maxVelocity) { 68 for (var i = 0; i < velocity.Length; i++) { 69 velocity[i] *= maxVelocity / speed; 70 } 71 } 72 } 73 74 private void UpdatePosition() { 75 var velocity = VelocityParameter.ActualValue; 76 var position = RealVectorParameter.ActualValue; 77 78 for (int i = 0; i < velocity.Length; i++) { 79 position[i] += velocity[i]; 80 } 81 82 var bounds = BoundsParameter.ActualValue; 83 for (int i = 0; i < position.Length; i++) { 84 double min = bounds[i % bounds.Rows, 0]; 85 double max = bounds[i % bounds.Rows, 1]; 86 if (position[i] < min) { 87 position[i] = min; 88 velocity[i] = -0.5 * velocity[i]; // SPSO 2011 89 } 90 if (position[i] > max) { 91 position[i] = max; 92 velocity[i] = -0.5 * velocity[i]; // SPSO 2011 93 } 94 } 95 } 96 97 public override IOperation Apply() { 98 UpdateVelocity(); 99 UpdatePosition(); 62 MoveParticle(velocity, position); 100 63 101 64 return base.Apply(); -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/SPSO2007ParticleUpdater.cs
r15096 r15102 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, double maxVelocity, RealVector position, double inertia, RealVector personalBest, double personalBestAttraction, RealVector neighborBest, double neighborBestAttraction, double c = 1.193) { 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() * c; 47 double r_g = random.NextDouble() * c; 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 var maxBeyond = MaxBeyondBestParameter.ActualValue.Value; 94 95 UpdateVelocity(random, velocity, maxVelocity, position, inertia, personalBest, personalBestAttraction, neighborBest, neighborBestAttraction, maxBeyond); 94 96 UpdatePosition(bounds, velocity, position); 95 97 -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/SPSO2011ParticleUpdater.cs
r15096 r15102 29 29 [Item("SPSO 2011 Particle Updater", "Updates the particle's position according to the formulae described in SPSO 2011.")] 30 30 [StorableClass] 31 public sealed class SPSO2011ParticleUpdater : RealVectorParticleUpdater { 32 31 public sealed class SPSO2011ParticleUpdater : SPSOParticleUpdater { 33 32 #region Construction & Cloning 34 33 [StorableConstructor] … … 41 40 #endregion 42 41 43 public static void UpdateVelocity(IRandom random, RealVector velocity, double maxVelocity, RealVector position, double inertia, RealVector personalBest, double personalBestAttraction, RealVector neighborBest, double neighborBestAttraction ) {42 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) { 44 43 var gravity = new double[velocity.Length]; 45 44 var direct = new RealVector(velocity.Length); … … 47 46 48 47 for (int i = 0; i < velocity.Length; i++) { 49 var g_id = 1.193* ((personalBest[i] + neighborBest[i] - 2 * position[i]) / 3.0);48 var g_id = c * ((personalBest[i] + neighborBest[i] - 2 * position[i]) / 3.0); 50 49 gravity[i] = g_id + position[i]; 51 50 direct[i] = (random.NextDouble() - 0.5) * 2; … … 101 100 var neighborBest = NeighborBestParameter.ActualValue; 102 101 var neighborBestAttraction = NeighborBestAttractionParameter.ActualValue.Value; 103 104 UpdateVelocity(random, velocity, maxVelocity, position, inertia, personalBest, personalBestAttraction, neighborBest, neighborBestAttraction); 102 var maxBeyond = MaxBeyondBestParameter.ActualValue.Value; 103 104 UpdateVelocity(random, velocity, maxVelocity, position, inertia, personalBest, personalBestAttraction, neighborBest, neighborBestAttraction, maxBeyond); 105 105 UpdatePosition(bounds, velocity, position); 106 106
Note: See TracChangeset
for help on using the changeset viewer.