- Timestamp:
- 03/02/11 15:22:54 (14 years ago)
- Location:
- trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorNeighborhoodParticleUpdater.cs
r5568 r5592 30 30 public sealed class RealVectorNeighborhoodParticleUpdater : RealVectorParticleUpdater, ILocalParticleUpdater { 31 31 32 #region Construction & Cloning 32 33 [StorableConstructor] 33 34 private RealVectorNeighborhoodParticleUpdater(bool deserializing) : base(deserializing) { } 34 35 private RealVectorNeighborhoodParticleUpdater(RealVectorNeighborhoodParticleUpdater original, Cloner cloner) : base(original, cloner) { } 35 36 public RealVectorNeighborhoodParticleUpdater() : base() { } 36 37 37 public override IDeepCloneable Clone(Cloner cloner) { 38 38 return new RealVectorNeighborhoodParticleUpdater(this, cloner); 39 39 } 40 #endregion 40 41 41 42 public override IOperation Apply() { 43 double inertia = Inertia.Value; 44 double personalBestAttraction = PersonalBestAttraction.Value; 45 double neighborBestAttraction = NeighborBestAttraction.Value; 46 42 47 RealVector velocity = new RealVector(Velocity.Length); 43 48 RealVector position = new RealVector(RealVector.Length); 44 49 double r_p = Random.NextDouble(); 45 50 double r_g = Random.NextDouble(); 46 double omega = Inertia.Value; 47 double phi_p = PersonalBestAttraction.Value; 48 double phi_g = NeighborBestAttraction.Value; 51 49 52 for (int i = 0; i < velocity.Length; i++) { 50 53 velocity[i] = 51 Velocity[i] * omega +52 (PersonalBest[i] - RealVector[i]) * p hi_p* r_p +53 (BestPoint[i] - RealVector[i]) * phi_g* r_g;54 Velocity[i] * inertia + 55 (PersonalBest[i] - RealVector[i]) * personalBestAttraction * r_p + 56 (BestPoint[i] - RealVector[i]) * neighborBestAttraction * r_g; 54 57 } 58 55 59 BoundsChecker.Apply(velocity, VelocityBounds); 56 60 for (int i = 0; i < velocity.Length; i++) { … … 58 62 } 59 63 BoundsChecker.Apply(position, Bounds); 64 60 65 RealVector = position; 61 66 Velocity = velocity; -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorParticleCreator.cs
r5560 r5592 20 20 #endregion 21 21 22 using System; 23 using HeuristicLab.Operators; 22 using HeuristicLab.Common; 24 23 using HeuristicLab.Core; 25 24 using HeuristicLab.Data; 25 using HeuristicLab.Operators; 26 26 using HeuristicLab.Parameters; 27 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 using HeuristicLab.Common;29 using HeuristicLab.Optimization;30 28 31 29 namespace HeuristicLab.Encodings.RealVectorEncoding { 32 30 [Item("RealVectorParticleCreator", "Creates a particle with position, zero velocity vector and personal best.")] 33 31 [StorableClass] 34 public class RealVectorParticleCreator : SingleSuccessorOperator, IRealVectorParticleCreator { 35 #region IRealVectorParticleCreator Members 32 public class RealVectorParticleCreator : AlgorithmOperator, IRealVectorParticleCreator { 33 34 #region Parameters 36 35 public ILookupParameter<IntValue> ProblemSizeParameter { 37 get { return (ILookupParameter<IntValue>)Parameters["ProblemSize"]; }36 get { return (ILookupParameter<IntValue>)Parameters["ProblemSize"]; } 38 37 } 39 38 public IValueLookupParameter<DoubleMatrix> BoundsParameter { 39 get { return (IValueLookupParameter<DoubleMatrix>)Parameters["Bounds"]; } 40 } 40 41 public ILookupParameter<RealVector> RealVectorParameter { 41 42 get { return (ILookupParameter<RealVector>)Parameters["RealVector"]; } 42 43 } 43 44 44 public ILookupParameter<RealVector> PersonalBestParameter { 45 45 get { return (ILookupParameter<RealVector>)Parameters["PersonalBest"]; } 46 46 } 47 48 public IValueLookupParameter<DoubleMatrix> BoundsParameter {49 get { return (IValueLookupParameter<DoubleMatrix>)Parameters["Bounds"]; }50 }51 52 47 public ILookupParameter<RealVector> VelocityParameter { 53 48 get { return (ILookupParameter<RealVector>)Parameters["Velocity"]; } 54 49 } 55 56 50 #endregion 57 51 58 public RealVectorParticleCreator() : base() { 52 #region Parameter Values 53 protected int ProblemSize { 54 get { return ProblemSizeParameter.ActualValue.Value; } 55 } 56 protected RealVector Velocity { 57 set { VelocityParameter.ActualValue = value; } 58 } 59 #endregion 60 61 #region Construction & Cloning 62 [StorableConstructor] 63 protected RealVectorParticleCreator(bool deserializing) : base(deserializing) { } 64 protected RealVectorParticleCreator(RealVectorParticleCreator original, Cloner cloner) : base(original, cloner) { } 65 public RealVectorParticleCreator() 66 : base() { 59 67 Parameters.Add(new LookupParameter<IntValue>("ProblemSize", "The dimension of the problem.")); 60 68 Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Bounds", "The lower and upper bounds in each dimension.")); 61 69 Parameters.Add(new LookupParameter<RealVector>("RealVector", "Particle's current solution")); 62 70 Parameters.Add(new LookupParameter<RealVector>("PersonalBest", "Particle's personal best solution.")); 63 Parameters.Add(new LookupParameter<RealVector>("Velocity", "Particle's current velocity.")); 64 } 71 Parameters.Add(new LookupParameter<RealVector>("Velocity", "Particle's current velocity.")); 65 72 66 public override IOperation Apply() { 67 VelocityParameter.ActualValue = new RealVector(ProblemSizeParameter.ActualValue.Value); 68 UniformRandomRealVectorCreator realVectorCreater = new UniformRandomRealVectorCreator(); 73 UniformRandomRealVectorCreator realVectorCreater = new UniformRandomRealVectorCreator(); 69 74 Assigner personalBestPositionAssigner = new Assigner(); 70 75 71 this.Name = "Particle Creator";76 OperatorGraph.InitialOperator = realVectorCreater; 72 77 73 //realVectorCreater.Name = "(SolutionCreator)"; 74 realVectorCreater.RealVectorParameter.ActualName = "RealVector"; 78 realVectorCreater.RealVectorParameter.ActualName = RealVectorParameter.Name; 75 79 realVectorCreater.LengthParameter.ActualName = ProblemSizeParameter.Name; 76 realVectorCreater.BoundsParameter.ActualName = BoundsParameter.Name; 80 realVectorCreater.BoundsParameter.ActualName = BoundsParameter.Name; 77 81 realVectorCreater.Successor = personalBestPositionAssigner; 78 82 79 personalBestPositionAssigner.LeftSideParameter.ActualName = "PersonalBest"; 80 personalBestPositionAssigner.RightSideParameter.ActualName = "RealVector"; 81 personalBestPositionAssigner.Successor = null; 82 83 OperationCollection next = new OperationCollection(); 84 next.Add(ExecutionContext.CreateChildOperation(realVectorCreater)); 85 next.Add(base.Apply()); 86 return next; 83 personalBestPositionAssigner.LeftSideParameter.ActualName = PersonalBestParameter.Name; 84 personalBestPositionAssigner.RightSideParameter.ActualName = RealVectorParameter.Name; 85 personalBestPositionAssigner.Successor = null; 87 86 } 88 89 87 public override IDeepCloneable Clone(Cloner cloner) { 90 88 return new RealVectorParticleCreator(this, cloner); 91 89 } 90 #endregion 92 91 93 protected RealVectorParticleCreator(RealVectorParticleCreator original, Cloner cloner) 94 : base(original, cloner) { 92 public override IOperation Apply() { 93 Velocity = new RealVector(ProblemSize); 94 return base.Apply(); 95 95 } 96 96 97 [StorableConstructor] 98 protected RealVectorParticleCreator(bool deserializing) : base(deserializing) { } 97 99 98 } 100 99 } -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorParticleUpdater.cs
r5568 r5592 28 28 29 29 namespace HeuristicLab.Encodings.RealVectorEncoding { 30 30 31 [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.")] 31 32 [StorableClass] 32 33 public abstract class RealVectorParticleUpdater : SingleSuccessorOperator, IRealVectorParticleUpdater { 34 33 35 public override bool CanChangeName { 34 36 get { return false; } … … 110 112 111 113 #region Construction & Cloning 112 113 114 [StorableConstructor] 114 115 protected RealVectorParticleUpdater(bool deserializing) : base(deserializing) { } 115 116 protected RealVectorParticleUpdater(RealVectorParticleUpdater original, Cloner cloner) : base(original, cloner) { } 116 117 117 public RealVectorParticleUpdater() 118 118 : base() { … … 129 129 Parameters.Add(new LookupParameter<DoubleValue>("NeighborBestAttraction", "The weight for the global best position.")); 130 130 } 131 132 131 #endregion 133 132 } -
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 } -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorTotallyConnectedParticleUpdater.cs
r5568 r5592 31 31 32 32 #region Construction & Cloning 33 34 33 [StorableConstructor] 35 34 private RealVectorTotallyConnectedParticleUpdater(bool deserializing) : base(deserializing) { } 36 35 private RealVectorTotallyConnectedParticleUpdater(RealVectorTotallyConnectedParticleUpdater original, Cloner cloner) : base(original, cloner) { } 37 36 public RealVectorTotallyConnectedParticleUpdater() : base() { } 38 39 37 public override IDeepCloneable Clone(Cloner cloner) { 40 38 return new RealVectorTotallyConnectedParticleUpdater(this, cloner); 41 39 } 42 43 40 #endregion 44 41 45 42 public override IOperation Apply() { 46 base.Apply(); 43 double inertia = Inertia.Value; 44 double personalBestAttraction = PersonalBestAttraction.Value; 45 double neighborBestAttraction = NeighborBestAttraction.Value; 46 47 47 RealVector velocity = new RealVector(Velocity.Length); 48 48 RealVector position = new RealVector(RealVector.Length); 49 49 double r_p = Random.NextDouble(); 50 50 double r_g = Random.NextDouble(); 51 double omega = Inertia.Value; 52 double phi_p = PersonalBestAttraction.Value; 53 double phi_g = NeighborBestAttraction.Value; 51 54 52 for (int i = 0; i < velocity.Length; i++) { 55 53 velocity[i] = 56 Velocity[i] * omega +57 (PersonalBest[i] - RealVector[i]) * p hi_p* r_p +58 (BestPoint[i] - RealVector[i]) * phi_g* r_g;54 Velocity[i] * inertia + 55 (PersonalBest[i] - RealVector[i]) * personalBestAttraction * r_p + 56 (BestPoint[i] - RealVector[i]) * neighborBestAttraction * r_g; 59 57 } 58 60 59 BoundsChecker.Apply(velocity, VelocityBounds); 61 60 for (int i = 0; i < velocity.Length; i++) { … … 63 62 } 64 63 BoundsChecker.Apply(position, Bounds); 64 65 65 RealVector = position; 66 66 Velocity = velocity;
Note: See TracChangeset
for help on using the changeset viewer.