Changeset 5568 for trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorSwarmUpdater.cs
- Timestamp:
- 02/28/11 17:25:28 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorSwarmUpdater.cs
r5566 r5568 25 25 using HeuristicLab.Data; 26 26 using HeuristicLab.Operators; 27 using HeuristicLab.Optimization; 27 28 using HeuristicLab.Parameters; 28 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 43 44 get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["PersonalBestQuality"]; } 44 45 } 45 public IScopeTreeLookupParameter<DoubleValue> Neighbor sBestQualityParameter {46 get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Neighbor sBestQuality"]; }46 public IScopeTreeLookupParameter<DoubleValue> NeighborBestQualityParameter { 47 get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["NeighborBestQuality"]; } 47 48 } 48 49 public IScopeTreeLookupParameter<RealVector> RealVectorParameter { … … 52 53 get { return (IScopeTreeLookupParameter<RealVector>)Parameters["PersonalBest"]; } 53 54 } 54 public IScopeTreeLookupParameter<RealVector> Neighbor sBestParameter {55 get { return (IScopeTreeLookupParameter<RealVector>)Parameters["Neighbor sBest"]; }56 } 57 public I ValueLookupParameter<BoolValue> MaximizationParameter {58 get { return (I ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }55 public IScopeTreeLookupParameter<RealVector> NeighborBestParameter { 56 get { return (IScopeTreeLookupParameter<RealVector>)Parameters["NeighborBest"]; } 57 } 58 public ILookupParameter<BoolValue> MaximizationParameter { 59 get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; } 59 60 } 60 61 public ILookupParameter<DoubleValue> BestQualityParameter { … … 66 67 public IScopeTreeLookupParameter<IntArray> NeighborsParameter { 67 68 get { return (IScopeTreeLookupParameter<IntArray>)Parameters["Neighbors"]; } 69 } 70 public ValueLookupParameter<IDiscreteDoubleMatrixModifier> VelocityBoundsUpdaterParameter { 71 get { return (ValueLookupParameter<IDiscreteDoubleMatrixModifier>)Parameters["VelocityBoundsUpdater"]; } 72 } 73 public LookupParameter<DoubleMatrix> VelocityBoundsParameter { 74 get { return (LookupParameter<DoubleMatrix>)Parameters["VelocityBounds"]; } 68 75 } 69 76 #endregion … … 82 89 private ItemArray<DoubleValue> PersonalBestQuality { 83 90 get { return PersonalBestQualityParameter.ActualValue; } 84 } 85 private ItemArray<DoubleValue> NeighborsBestQuality { 86 get { return NeighborsBestQualityParameter.ActualValue; } 91 set { PersonalBestQualityParameter.ActualValue = value; } 92 } 93 private ItemArray<DoubleValue> NeighborBestQuality { 94 get { return NeighborBestQualityParameter.ActualValue; } 95 set { NeighborBestQualityParameter.ActualValue = value; } 87 96 } 88 97 private ItemArray<RealVector> RealVector { … … 93 102 set { PersonalBestParameter.ActualValue = value; } 94 103 } 95 private ItemArray<RealVector> Neighbor sBest {96 get { return Neighbor sBestParameter.ActualValue; }97 set { Neighbor sBestParameter.ActualValue = value; }104 private ItemArray<RealVector> NeighborBest { 105 get { return NeighborBestParameter.ActualValue; } 106 set { NeighborBestParameter.ActualValue = value; } 98 107 } 99 108 private bool Maximization { … … 102 111 private ItemArray<IntArray> Neighbors { 103 112 get { return NeighborsParameter.ActualValue; } 113 } 114 private IDiscreteDoubleMatrixModifier VelocityBoundsUpdater { 115 get { return VelocityBoundsUpdaterParameter.ActualValue; } 116 } 117 private DoubleMatrix VelocityBounds { 118 get { return VelocityBoundsParameter.ActualValue; } 119 set { VelocityBoundsParameter.ActualValue = value; } 104 120 } 105 121 #endregion … … 116 132 Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "Particle's quality")); 117 133 Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("PersonalBestQuality", "Particle's personal best quality")); 118 Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Neighbor sBestQuality", "Global best particle quality"));134 Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("NeighborBestQuality", "Global best particle quality")); 119 135 Parameters.Add(new ScopeTreeLookupParameter<RealVector>("RealVector", "Particle's position")); 120 136 Parameters.Add(new ScopeTreeLookupParameter<RealVector>("PersonalBest", "Particle's personal best position")); 121 Parameters.Add(new ScopeTreeLookupParameter<RealVector>("Neighbor sBest", "Neighborhood (or global in case of totally connected neighborhood) best particle position"));137 Parameters.Add(new ScopeTreeLookupParameter<RealVector>("NeighborBest", "Neighborhood (or global in case of totally connected neighborhood) best particle position")); 122 138 Parameters.Add(new ScopeTreeLookupParameter<IntArray>("Neighbors", "The list of neighbors for each particle.")); 123 Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false.")); 139 Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false.")); 140 Parameters.Add(new ValueLookupParameter<IDiscreteDoubleMatrixModifier>("VelocityBoundsUpdater", "Modifies the velocity bounds in the course of optimization.")); 141 Parameters.Add(new LookupParameter<DoubleMatrix>("VelocityBounds", "Maximum velocity for each dimension.")); 124 142 } 125 143 … … 132 150 public override IOperation Apply() { 133 151 InitializeBestPoint(); 152 UpdateNeighbors(); 134 153 UpdateSwarm(); 135 UpdateNeighbors(); 136 UpdateVelocityBounds(); 137 return base.Apply(); 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 } 138 162 } 139 163 … … 148 172 private void UpdateNeighbors() { 149 173 if (Neighbors != null & Neighbors.Length > 0) { 150 if (this.Neighbor sBest == null || NeighborsBest.Length != Neighbors.Length)151 Neighbor sBest = new ItemArray<RealVector>(Neighbors.Length);174 if (this.NeighborBest == null || NeighborBest.Length != Neighbors.Length) 175 NeighborBest = new ItemArray<RealVector>(Neighbors.Length); 152 176 for (int n = 0; n < Neighbors.Length; n++) { 153 177 var pairs = Quality.Zip(RealVector, (q, p) => new { Quality = q, Point = p }) 154 178 .Where((p, i) => i == n || Neighbors[n].Contains(i)); 155 Neighbor sBest[n] = Maximization ?179 NeighborBest[n] = Maximization ? 156 180 pairs.OrderByDescending(p => p.Quality.Value).First().Point : 157 181 pairs.OrderBy(p => p.Quality.Value).First().Point; 158 182 } 159 NeighborsBestParameter.ActualValue = NeighborsBest; 160 } 161 } 162 163 private void UpdateVelocityBounds() { 164 // ToDo: Add code 183 NeighborBestParameter.ActualValue = NeighborBest; 184 } 165 185 } 166 186 167 187 private void UpdateSwarm() { 168 188 if (PersonalBestQuality.Length == 0) { 169 ItemArray<DoubleValue> personalBestQualities = new ItemArray<DoubleValue>(RealVector.Length); 170 for (int i = 0; i < RealVector.Length; i++) { 171 if (Maximization) { 172 personalBestQualities[i] = new DoubleValue(double.MinValue); 173 } else { 174 personalBestQualities[i] = new DoubleValue(double.MaxValue); 175 } 176 } 177 PersonalBestQualityParameter.ActualValue = personalBestQualities; 178 } 179 if (NeighborsBestQuality.Length == 0) { 180 if (NeighborsParameter != null && Neighbors.Length > 0) { 181 ItemArray<DoubleValue> neighborsBestQualities = new ItemArray<DoubleValue>(RealVector.Length); 182 for (int i = 0; i < RealVector.Length; i++) { 183 if (Maximization) { 184 neighborsBestQualities[i] = new DoubleValue(double.MinValue); 185 } else { 186 neighborsBestQualities[i] = new DoubleValue(double.MaxValue); 187 } 188 } 189 NeighborsBestQualityParameter.ActualValue = neighborsBestQualities; 190 } 191 } 192 ItemArray<RealVector> neighborsBest = new ItemArray<RealVector>(RealVector.Length); 189 PersonalBestQuality = (ItemArray<DoubleValue>)Quality.Clone(); 190 if (VelocityBounds == null) 191 VelocityBounds = new DoubleMatrix(new double[,] { { -1, 1 } }); 192 } 193 193 for (int i = 0; i < RealVector.Length; i++) { 194 194 if (Maximization && Quality[i].Value > PersonalBestQuality[i].Value || … … 196 196 PersonalBestQuality[i].Value = Quality[i].Value; 197 197 PersonalBest[i] = RealVector[i]; 198 if (Maximization && PersonalBestQuality[i].Value > NeighborsBestQuality[i].Value || 199 !Maximization && PersonalBestQuality[i].Value < NeighborsBestQuality[i].Value) { 200 NeighborsBestQuality[i].Value = PersonalBestQuality[i].Value; 201 neighborsBest[i] = PersonalBest[i]; 198 } 199 } 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]; 202 212 } 203 213 } 204 } 205 if (NeighborsParameter != null && Neighbors.Length > 0) { 206 NeighborsBestParameter.ActualValue = neighborsBest; 207 } 208 } 209 214 NeighborBestQuality = neighborBestQuality; 215 NeighborBest = neighborBest; 216 } 217 } 210 218 } 211 219 }
Note: See TracChangeset
for help on using the changeset viewer.