Changeset 5561 for trunk/sources/HeuristicLab.Encodings.RealVectorEncoding
- Timestamp:
- 02/24/11 19:22:39 (14 years ago)
- Location:
- trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/Interfaces/IRealVectorSwarmUpdater.cs
r5560 r5561 25 25 namespace HeuristicLab.Encodings.RealVectorEncoding { 26 26 public interface IRealVectorSwarmUpdater : ISwarmUpdater, IRealVectorOperator { 27 I LookupParameter<RealVector> NeighborsBestParameter { get; }28 I LookupParameter<RealVector> PersonalBestParameter { get; }29 I LookupParameter<RealVector> RealVectorParameter { get; }27 IScopeTreeLookupParameter<RealVector> NeighborsBestParameter { get; } 28 IScopeTreeLookupParameter<RealVector> PersonalBestParameter { get; } 29 IScopeTreeLookupParameter<RealVector> RealVectorParameter { get; } 30 30 } 31 31 } -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorSwarmUpdater.cs
r5560 r5561 20 20 #endregion 21 21 22 using System.Linq; 22 23 using HeuristicLab.Common; 23 24 using HeuristicLab.Core; … … 36 37 37 38 #region Parameter properties 38 public I LookupParameter<DoubleValue> QualityParameter {39 get { return (I LookupParameter<DoubleValue>)Parameters["Quality"]; }40 } 41 public I LookupParameter<DoubleValue> PersonalBestQualityParameter {42 get { return (I LookupParameter<DoubleValue>)Parameters["PersonalBestQuality"]; }43 } 44 public I LookupParameter<DoubleValue> NeighborsBestQualityParameter {45 get { return (I LookupParameter<DoubleValue>)Parameters["NeighborsBestQuality"]; }46 } 47 public I LookupParameter<RealVector> RealVectorParameter {48 get { return (I LookupParameter<RealVector>)Parameters["RealVector"]; }49 } 50 public I LookupParameter<RealVector> PersonalBestParameter {51 get { return (I LookupParameter<RealVector>)Parameters["PersonalBest"]; }52 } 53 public I LookupParameter<RealVector> NeighborsBestParameter {54 get { return (I LookupParameter<RealVector>)Parameters["NeighborsBest"]; }39 public IScopeTreeLookupParameter<DoubleValue> QualityParameter { 40 get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; } 41 } 42 public IScopeTreeLookupParameter<DoubleValue> PersonalBestQualityParameter { 43 get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["PersonalBestQuality"]; } 44 } 45 public IScopeTreeLookupParameter<DoubleValue> NeighborsBestQualityParameter { 46 get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["NeighborsBestQuality"]; } 47 } 48 public IScopeTreeLookupParameter<RealVector> RealVectorParameter { 49 get { return (IScopeTreeLookupParameter<RealVector>)Parameters["RealVector"]; } 50 } 51 public IScopeTreeLookupParameter<RealVector> PersonalBestParameter { 52 get { return (IScopeTreeLookupParameter<RealVector>)Parameters["PersonalBest"]; } 53 } 54 public IScopeTreeLookupParameter<RealVector> NeighborsBestParameter { 55 get { return (IScopeTreeLookupParameter<RealVector>)Parameters["NeighborsBest"]; } 55 56 } 56 57 public IValueLookupParameter<BoolValue> MaximizationParameter { 57 58 get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; } 58 59 } 60 public ILookupParameter<DoubleValue> BestQualityParameter { 61 get { return (ILookupParameter<DoubleValue>)Parameters["BestQuality"]; } 62 } 63 public ILookupParameter<RealVector> BestPointParameter { 64 get { return (ILookupParameter<RealVector>)Parameters["BestPoint"]; } 65 } 66 public IScopeTreeLookupParameter<IntArray> NeighborsParameter { 67 get { return (IScopeTreeLookupParameter<IntArray>)Parameters["Neighbors"]; } 68 } 59 69 #endregion 60 70 61 71 #region Parameter values 62 private double Quality { 63 get { return QualityParameter.ActualValue.Value; } 64 } 65 private double PersonalBestQuality { 66 get { return PersonalBestQualityParameter.ActualValue.Value; } 67 set { PersonalBestQualityParameter.ActualValue = new DoubleValue(value); } 68 } 69 private double NeighborsBestQuality { 70 get { return NeighborsBestQualityParameter.ActualValue.Value; } 71 set { NeighborsBestQualityParameter.ActualValue = new DoubleValue(value); } 72 } 73 private RealVector RealVector { 72 private DoubleValue BestQuality { 73 get { return BestQualityParameter.ActualValue; } 74 } 75 private RealVector BestPoint { 76 get { return BestPointParameter.ActualValue; } 77 set { BestPointParameter.ActualValue = value; } 78 } 79 private ItemArray<DoubleValue> Quality { 80 get { return QualityParameter.ActualValue; } 81 } 82 private ItemArray<DoubleValue> PersonalBestQuality { 83 get { return PersonalBestQualityParameter.ActualValue; } 84 } 85 private ItemArray<DoubleValue> NeighborsBestQuality { 86 get { return NeighborsBestQualityParameter.ActualValue; } 87 } 88 private ItemArray<RealVector> RealVector { 74 89 get { return RealVectorParameter.ActualValue; } 75 90 } 76 private RealVectorPersonalBest {91 private ItemArray<RealVector> PersonalBest { 77 92 get { return PersonalBestParameter.ActualValue; } 78 93 set { PersonalBestParameter.ActualValue = value; } 79 94 } 80 private RealVectorNeighborsBest {95 private ItemArray<RealVector> NeighborsBest { 81 96 get { return NeighborsBestParameter.ActualValue; } 82 97 set { NeighborsBestParameter.ActualValue = value; } … … 84 99 private bool Maximization { 85 100 get { return MaximizationParameter.ActualValue.Value; } 101 } 102 private ItemArray<IntArray> Neighbors { 103 get { return NeighborsParameter.ActualValue; } 86 104 } 87 105 #endregion … … 94 112 public RealVectorSwarmUpdater() 95 113 : base() { 96 Parameters.Add(new LookupParameter<DoubleValue>("Quality", "Particle's quality")); 97 Parameters.Add(new LookupParameter<DoubleValue>("PersonalBestQuality", "Particle's personal best quality")); 98 Parameters.Add(new LookupParameter<DoubleValue>("NeighborsBestQuality", "Global best particle quality")); 99 Parameters.Add(new LookupParameter<RealVector>("RealVector", "Particle's position")); 100 Parameters.Add(new LookupParameter<RealVector>("PersonalBest", "Particle's personal best position")); 101 Parameters.Add(new LookupParameter<RealVector>("NeighborsBest", "Neighborhood (or global in case of totally connected neighborhood) best particle position")); 114 Parameters.Add(new LookupParameter<DoubleValue>("BestQuality", "Overall best quality.")); 115 Parameters.Add(new LookupParameter<RealVector>("BestPoint", "Global best particle position")); 116 Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "Particle's quality")); 117 Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("PersonalBestQuality", "Particle's personal best quality")); 118 Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("NeighborsBestQuality", "Global best particle quality")); 119 Parameters.Add(new ScopeTreeLookupParameter<RealVector>("RealVector", "Particle's position")); 120 Parameters.Add(new ScopeTreeLookupParameter<RealVector>("PersonalBest", "Particle's personal best position")); 121 Parameters.Add(new ScopeTreeLookupParameter<RealVector>("NeighborsBest", "Neighborhood (or global in case of totally connected neighborhood) best particle position")); 122 Parameters.Add(new ScopeTreeLookupParameter<IntArray>("Neighbors", "The list of neighbors for each particle.")); 102 123 Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false.")); 103 124 } … … 110 131 111 132 public override IOperation Apply() { 112 if (Maximization && Quality > PersonalBestQuality || 113 !Maximization && Quality < PersonalBestQuality) { 114 PersonalBestQuality = Quality; 115 PersonalBest = RealVector; 116 if (Maximization && PersonalBestQuality > NeighborsBestQuality || 117 !Maximization && PersonalBestQuality < NeighborsBestQuality) { 118 NeighborsBestQuality = PersonalBestQuality; 119 NeighborsBest = PersonalBest; 120 } 121 } 133 InitializeBestPoint(); 134 UpdateNeighbors(); 135 UpdateVelocityBounds(); 136 UpdateSwarm(); 122 137 return base.Apply(); 138 } 139 140 private void InitializeBestPoint() { 141 if (BestQuality == null) 142 BestQualityParameter.ActualValue = new DoubleValue(); 143 BestQuality.Value = Maximization ? Quality.Max(v => v.Value) : Quality.Min(v => v.Value); 144 int bestIndex = Quality.FindIndex(v => v.Value == BestQuality.Value); 145 BestPoint = (RealVector)RealVector[bestIndex].Clone(); 146 } 147 148 private void UpdateNeighbors() { 149 if (Neighbors != null & Neighbors.Length > 0) { 150 if (this.NeighborsBest == null || NeighborsBest.Length != Neighbors.Length) 151 NeighborsBest = new ItemArray<RealVector>(Neighbors.Length); 152 for (int n = 0; n < Neighbors.Length; n++) { 153 var pairs = Quality.Zip(RealVector, (q, p) => new { Quality = q, Point = p }) 154 .Where((p, i) => i == n || Neighbors[n].Contains(i)); 155 NeighborsBest[n] = Maximization ? 156 pairs.OrderByDescending(p => p.Quality.Value).First().Point : 157 pairs.OrderBy(p => p.Quality.Value).First().Point; 158 } 159 NeighborsBestParameter.ActualValue = NeighborsBest; 160 } 161 } 162 163 private void UpdateVelocityBounds() { 164 // ToDo: Add code 165 } 166 167 private void UpdateSwarm() { 168 if (PersonalBestQuality.Length == 0) { 169 PersonalBestQualityParameter.ActualValue = new ItemArray<DoubleValue>(RealVector.Length); 170 for (int i = 0; i < RealVector.Length; i++) { 171 if (Maximization) { 172 PersonalBestQuality[i] = new DoubleValue(); 173 } else { 174 PersonalBestQuality[i] = new DoubleValue(double.MaxValue); 175 } 176 } 177 } 178 if (NeighborsBestQuality.Length == 0) { 179 NeighborsBestQualityParameter.ActualValue = new ItemArray<DoubleValue>(RealVector.Length); 180 for (int i = 0; i < RealVector.Length; i++) { 181 if (Maximization) { 182 NeighborsBestQuality[i] = new DoubleValue(); 183 } else { 184 NeighborsBestQuality[i] = new DoubleValue(double.MaxValue); 185 } 186 } 187 } 188 if (NeighborsBest.Length == 0) { 189 NeighborsBestParameter.ActualValue = new ItemArray<RealVector>(RealVector.Length); 190 } 191 for (int i = 0; i < RealVector.Length; i++) { 192 if (Maximization && Quality[i].Value > PersonalBestQuality[i].Value || 193 !Maximization && Quality[i].Value < PersonalBestQuality[i].Value) { 194 PersonalBestQuality[i].Value = Quality[i].Value; 195 PersonalBest[i] = RealVector[i]; 196 if (Maximization && PersonalBestQuality[i].Value > NeighborsBestQuality[i].Value || 197 !Maximization && PersonalBestQuality[i].Value < NeighborsBestQuality[i].Value) { 198 NeighborsBestQuality[i].Value = PersonalBestQuality[i].Value; 199 NeighborsBest[i] = PersonalBest[i]; 200 } 201 } 202 } 123 203 } 124 204
Note: See TracChangeset
for help on using the changeset viewer.