Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/30/17 21:57:38 (7 years ago)
Author:
abeham
Message:

#2797:

  • Recreated backwards compatibility by readding old operators and renaming new operators to SPSO*
    • If a previously configured algorithm is run again, the same results should be obtained
  • Set all old operators to internal, NonDiscoverableType, and Obsolete (they are also not fixed, e.g. PersonalBest update remains flawed)
  • Added SPSO 2007 velocity initializer and let users choose in SPSOParticleCreator
  • Changed description of PSO
  • Updated sample
File:
1 edited

Legend:

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

    r15096 r15102  
    1919 */
    2020#endregion
    21  
     21
     22using System;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
    2425using HeuristicLab.Data;
    2526using HeuristicLab.Operators;
    26 using HeuristicLab.Optimization;
    2727using HeuristicLab.Parameters;
    2828using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     29using HeuristicLab.PluginInfrastructure;
    2930
    3031namespace HeuristicLab.Encodings.RealVectorEncoding {
     
    3233  [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.")]
    3334  [StorableClass]
    34   public abstract class RealVectorParticleUpdater : SingleSuccessorOperator, IRealVectorParticleUpdater {
     35  [NonDiscoverableType]
     36  [Obsolete("Use SPSO2011ParticleUpdater")]
     37  internal abstract class RealVectorParticleUpdater : SingleSuccessorOperator, IRealVectorParticleUpdater {
    3538
    3639    public override bool CanChangeName {
     
    5154      get { return (ILookupParameter<RealVector>)Parameters["NeighborBest"]; }
    5255    }
     56    public LookupParameter<RealVector> BestRealVectorParameter {
     57      get { return (LookupParameter<RealVector>)Parameters["BestRealVector"]; }
     58    }
    5359    public ILookupParameter<RealVector> RealVectorParameter {
    5460      get { return (ILookupParameter<RealVector>)Parameters["RealVector"]; }
     
    5763      get { return (ILookupParameter<DoubleMatrix>)Parameters["Bounds"]; }
    5864    }
     65    public ILookupParameter<DoubleMatrix> CurrentVelocityBoundsParameter {
     66      get { return (ILookupParameter<DoubleMatrix>)Parameters["CurrentVelocityBounds"]; }
     67    }
    5968    public ILookupParameter<DoubleValue> CurrentMaxVelocityParameter {
    6069      get { return (ILookupParameter<DoubleValue>)Parameters["CurrentMaxVelocity"]; }
    6170    }
    62     public ILookupParameter<DoubleValue> CurrentInertiaParameter {
     71    public ILookupParameter<DoubleValue> InertiaParameter {
    6372      get { return (ILookupParameter<DoubleValue>)Parameters["CurrentInertia"]; }
    6473    }
    65     ILookupParameter<DoubleValue> IParticleUpdater.InertiaParameter { get { return CurrentInertiaParameter; } }
    66 
    6774    public ILookupParameter<DoubleValue> PersonalBestAttractionParameter {
    6875      get { return (ILookupParameter<DoubleValue>)Parameters["PersonalBestAttraction"]; }
     
    7279    }
    7380    #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
    75120    #region Construction & Cloning
    76121    [StorableConstructor]
     
    83128      Parameters.Add(new LookupParameter<RealVector>("Velocity", "Particle's current velocity."));
    84129      Parameters.Add(new LookupParameter<RealVector>("PersonalBest", "Particle's personal best solution."));
     130      Parameters.Add(new LookupParameter<RealVector>("BestRealVector", "Global best position."));
    85131      Parameters.Add(new LookupParameter<RealVector>("NeighborBest", "Best neighboring solution."));
    86132      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."));
    87134      Parameters.Add(new LookupParameter<DoubleValue>("CurrentMaxVelocity", "Maximum for the particle's velocity vector."));
    88135      Parameters.Add(new LookupParameter<DoubleValue>("CurrentInertia", "The weight for the particle's velocity vector."));
     
    91138    }
    92139    #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    }
    93185  }
    94186}
Note: See TracChangeset for help on using the changeset viewer.