Ignore:
Timestamp:
06/29/17 23:04:03 (3 months ago)
Author:
abeham
Message:

#2797:

  • Added SPSO 2007 and SPSO 2011 particle updaters
  • Unhide particle updater parameter
  • Changed default parameters of sample
  • Changed max velocity to very high value by default (no speed limit)
  • Adapted unit test
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorNeighborhoodParticleUpdater.cs

    r15091 r15096  
    2020#endregion
    2121
     22using System;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
    2425using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     26using HeuristicLab.PluginInfrastructure;
    2527
    2628namespace HeuristicLab.Encodings.RealVectorEncoding {
    2729  [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.")]
    2830  [StorableClass]
    29   public sealed class RealVectorNeighborhoodParticleUpdater : RealVectorParticleUpdater {
     31  [NonDiscoverableType]
     32  [Obsolete("Replaced by SPSO2007ParticleUpdater")]
     33  internal sealed class RealVectorNeighborhoodParticleUpdater : RealVectorParticleUpdater {
    3034
    3135    #region Construction & Cloning
     
    3943    #endregion
    4044
     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;
     53
     54      var random = RandomParameter.ActualValue;
     55
     56      for (int i = 0; i < velocity.Length; i++) {
     57        double r_p = random.NextDouble();
     58        double r_g = random.NextDouble();
     59        velocity[i] =
     60          velocity[i] * inertia +
     61          (personalBest[i] - position[i]) * personalBestAttraction * r_p +
     62          (neighborBest[i] - position[i]) * neighborBestAttraction * r_g;
     63      }
     64
     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
    4197    public override IOperation Apply() {
    4298      UpdateVelocity();
Note: See TracChangeset for help on using the changeset viewer.