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
Location:
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators
Files:
6 added
1 deleted
7 edited

Legend:

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

    r15096 r15102  
    3030  [StorableClass]
    3131  [NonDiscoverableType]
    32   [Obsolete("Replaced by SPSO2007ParticleUpdater")]
     32  [Obsolete("Use SPSO2011ParticleUpdater")]
    3333  internal sealed class RealVectorNeighborhoodParticleUpdater : RealVectorParticleUpdater {
    3434
     
    4343    #endregion
    4444
    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;
     45    public override IOperation Apply() {
     46      double inertia = Inertia.Value;
     47      double personalBestAttraction = PersonalBestAttraction.Value;
     48      double neighborBestAttraction = NeighborBestAttraction.Value;
    5349
    54       var random = RandomParameter.ActualValue;
     50      RealVector velocity = new RealVector(Velocity.Length);
     51      RealVector position = new RealVector(RealVector.Length);
     52      double r_p = Random.NextDouble();
     53      double r_g = Random.NextDouble();
    5554
    5655      for (int i = 0; i < velocity.Length; i++) {
    57         double r_p = random.NextDouble();
    58         double r_g = random.NextDouble();
    5956        velocity[i] =
    60           velocity[i] * inertia +
    61           (personalBest[i] - position[i]) * personalBestAttraction * r_p +
    62           (neighborBest[i] - position[i]) * neighborBestAttraction * r_g;
     57          Velocity[i] * inertia +
     58          (PersonalBest[i] - RealVector[i]) * personalBestAttraction * r_p +
     59          (BestPoint[i] - RealVector[i]) * neighborBestAttraction * r_g;
    6360      }
    6461
    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 
    97     public override IOperation Apply() {
    98       UpdateVelocity();
    99       UpdatePosition();
     62      MoveParticle(velocity, position);
    10063
    10164      return base.Apply();
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorParticleCreator.cs

    r15091 r15102  
    2020#endregion
    2121
     22using System;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
     
    2728using HeuristicLab.Parameters;
    2829using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     30using HeuristicLab.PluginInfrastructure;
    2931
    3032namespace HeuristicLab.Encodings.RealVectorEncoding {
    3133  [Item("RealVectorParticleCreator", "Creates a particle with position, zero velocity vector and personal best.")]
    3234  [StorableClass]
    33   public class RealVectorParticleCreator : AlgorithmOperator, IRealVectorParticleCreator, IStochasticOperator {
     35  [NonDiscoverableType]
     36  [Obsolete("Use SPSOParticleCreator")]
     37  internal class RealVectorParticleCreator : AlgorithmOperator, IRealVectorParticleCreator {
    3438
    3539    #region Parameters
    36     public ILookupParameter<IRandom> RandomParameter {
    37       get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
     40    public ILookupParameter<IntValue> ProblemSizeParameter {
     41      get { return (ILookupParameter<IntValue>)Parameters["ProblemSize"]; }
    3842    }
    3943    public IValueLookupParameter<DoubleMatrix> BoundsParameter {
     
    5357    }
    5458    #endregion
    55    
     59
     60    #region Parameter Values
     61    protected int ProblemSize {
     62      get { return ProblemSizeParameter.ActualValue.Value; }
     63    }
     64    protected RealVector Velocity {
     65      set { VelocityParameter.ActualValue = value; }
     66    }
     67    #endregion
     68
    5669    #region Construction & Cloning
    5770    [StorableConstructor]
     
    6073    public RealVectorParticleCreator()
    6174      : base() {
    62       Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
     75      Parameters.Add(new LookupParameter<IntValue>("ProblemSize", "The dimension of the problem."));
    6376      Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Bounds", "The lower and upper bounds in each dimension."));
    6477      Parameters.Add(new LookupParameter<RealVector>("RealVector", "Particle's current solution"));
     
    6780      Parameters.Add(new LookupParameter<ISolutionCreator>("SolutionCreator", "The operator that creates the initial position."));
    6881
    69       Placeholder realVectorCreater = new Placeholder();
     82      UniformRandomRealVectorCreator realVectorCreater = new UniformRandomRealVectorCreator();
    7083      Assigner personalBestPositionAssigner = new Assigner();
    71       RealVectorVelocityInitializer velocityInitializer = new RealVectorVelocityInitializer();
    7284
    7385      OperatorGraph.InitialOperator = realVectorCreater;
    7486
    75       realVectorCreater.OperatorParameter.ActualName = SolutionCreatorParameter.Name;
     87      realVectorCreater.RealVectorParameter.ActualName = RealVectorParameter.Name;
     88      realVectorCreater.LengthParameter.ActualName = ProblemSizeParameter.Name;
     89      realVectorCreater.BoundsParameter.ActualName = BoundsParameter.Name;
    7690      realVectorCreater.Successor = personalBestPositionAssigner;
    7791
    7892      personalBestPositionAssigner.LeftSideParameter.ActualName = PersonalBestParameter.Name;
    7993      personalBestPositionAssigner.RightSideParameter.ActualName = RealVectorParameter.Name;
    80       personalBestPositionAssigner.Successor = velocityInitializer;
    81 
    82       velocityInitializer.BoundsParameter.ActualName = BoundsParameter.Name;
    83       velocityInitializer.RandomParameter.ActualName = RandomParameter.Name;
    84       velocityInitializer.RealVectorParameter.ActualName = RealVectorParameter.Name;
    85       velocityInitializer.VelocityParameter.ActualName = VelocityParameter.Name;
    86       velocityInitializer.Successor = null;
     94      personalBestPositionAssigner.Successor = null;
    8795    }
    8896    public override IDeepCloneable Clone(Cloner cloner) {
     
    9098    }
    9199    #endregion
     100
     101    public override IOperation Apply() {
     102      Velocity = new RealVector(ProblemSize);
     103      return base.Apply();
     104    }
     105   
     106    [StorableHook(HookType.AfterDeserialization)]
     107    private void AfterDeserialization() {
     108      if (!Parameters.ContainsKey("SolutionCreator"))
     109        Parameters.Add(new LookupParameter<ISolutionCreator>("SolutionCreator", "The operator that creates the initial position."));
     110    }
    92111  }
    93112}
  • 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}
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorSwarmUpdater.cs

    r15096 r15102  
    2121
    2222using System;
    23 using System.Collections.Generic;
    2423using System.Linq;
    2524using HeuristicLab.Common;
     
    3635  [Item("RealVectorSwarmUpdater", "Updates personal best point and quality as well as global best point and quality.")]
    3736  [StorableClass]
    38   public sealed class RealVectorSwarmUpdater : SingleSuccessorOperator, IRealVectorSwarmUpdater, ISingleObjectiveOperator {
     37  [NonDiscoverableType]
     38  [Obsolete("Use SPSOSwarmUpdater")]
     39  internal sealed class RealVectorSwarmUpdater : SingleSuccessorOperator, IRealVectorSwarmUpdater, ISingleObjectiveOperator {
    3940
    4041    [Storable]
     
    7677      get { return (IScopeTreeLookupParameter<IntArray>)Parameters["Neighbors"]; }
    7778    }
    78     public IValueLookupParameter<DoubleValue> MaxVelocityParameter {
    79       get { return (ValueLookupParameter<DoubleValue>)Parameters["MaxVelocity"]; }
    80     }
    81     public ILookupParameter<DoubleValue> CurrentMaxVelocityParameter {
    82       get { return (ILookupParameter<DoubleValue>)Parameters["CurrentMaxVelocity"]; }
     79    public IValueLookupParameter<DoubleMatrix> VelocityBoundsParameter {
     80      get { return (ValueLookupParameter<DoubleMatrix>)Parameters["VelocityBounds"]; }
     81    }
     82    public ILookupParameter<DoubleMatrix> CurrentVelocityBoundsParameter {
     83      get { return (ILookupParameter<DoubleMatrix>)Parameters["CurrentVelocityBounds"]; }
    8384    }
    8485    public LookupParameter<ResultCollection> ResultsParameter {
     
    8687    }
    8788
    88     #region Max Velocity Updating
    89     public IConstrainedValueParameter<IDiscreteDoubleValueModifier> MaxVelocityScalingOperatorParameter {
    90       get { return (IConstrainedValueParameter<IDiscreteDoubleValueModifier>)Parameters["MaxVelocityScalingOperator"]; }
    91     }
    92     public IValueLookupParameter<DoubleValue> FinalMaxVelocityParameter {
    93       get { return (IValueLookupParameter<DoubleValue>)Parameters["FinalMaxVelocity"]; }
    94     }
    95     public ILookupParameter<IntValue> MaxVelocityIndexParameter {
    96       get { return (ILookupParameter<IntValue>)Parameters["MaxVelocityIndex"]; }
    97     }
    98     public IValueLookupParameter<IntValue> MaxVelocityStartIndexParameter {
    99       get { return (IValueLookupParameter<IntValue>)Parameters["MaxVelocityStartIndex"]; }
    100     }
    101     public IValueLookupParameter<IntValue> MaxVelocityEndIndexParameter {
    102       get { return (IValueLookupParameter<IntValue>)Parameters["MaxVelocityEndIndex"]; }
     89    #region Velocity Bounds Updating
     90    public ILookupParameter<DoubleValue> VelocityBoundsScaleParameter {
     91      get { return (ILookupParameter<DoubleValue>)Parameters["VelocityBoundsScale"]; }
     92    }
     93    public IConstrainedValueParameter<IDiscreteDoubleValueModifier> VelocityBoundsScalingOperatorParameter {
     94      get { return (IConstrainedValueParameter<IDiscreteDoubleValueModifier>)Parameters["VelocityBoundsScalingOperator"]; }
     95    }
     96    public IValueLookupParameter<DoubleValue> VelocityBoundsStartValueParameter {
     97      get { return (IValueLookupParameter<DoubleValue>)Parameters["VelocityBoundsStartValue"]; }
     98    }
     99    public IValueLookupParameter<DoubleValue> VelocityBoundsEndValueParameter {
     100      get { return (IValueLookupParameter<DoubleValue>)Parameters["VelocityBoundsEndValue"]; }
     101    }
     102    public ILookupParameter<IntValue> VelocityBoundsIndexParameter {
     103      get { return (ILookupParameter<IntValue>)Parameters["VelocityBoundsIndex"]; }
     104    }
     105    public IValueLookupParameter<IntValue> VelocityBoundsStartIndexParameter {
     106      get { return (IValueLookupParameter<IntValue>)Parameters["VelocityBoundsStartIndex"]; }
     107    }
     108    public IValueLookupParameter<IntValue> VelocityBoundsEndIndexParameter {
     109      get { return (IValueLookupParameter<IntValue>)Parameters["VelocityBoundsEndIndex"]; }
    103110    }
    104111    #endregion
    105112
    106113    #endregion
    107    
     114
     115    #region Parameter values
     116    private DoubleValue SwarmBestQuality {
     117      get { return SwarmBestQualityParameter.ActualValue; }
     118      set { SwarmBestQualityParameter.ActualValue = value; }
     119    }
     120    private RealVector BestRealVector {
     121      get { return BestRealVectorParameter.ActualValue; }
     122      set { BestRealVectorParameter.ActualValue = value; }
     123    }
     124    private ItemArray<DoubleValue> Quality {
     125      get { return QualityParameter.ActualValue; }
     126    }
     127    private ItemArray<DoubleValue> PersonalBestQuality {
     128      get { return PersonalBestQualityParameter.ActualValue; }
     129      set { PersonalBestQualityParameter.ActualValue = value; }
     130    }
     131    private ItemArray<DoubleValue> NeighborBestQuality {
     132      get { return NeighborBestQualityParameter.ActualValue; }
     133      set { NeighborBestQualityParameter.ActualValue = value; }
     134    }
     135    private ItemArray<RealVector> RealVector {
     136      get { return RealVectorParameter.ActualValue; }
     137    }
     138    private ItemArray<RealVector> PersonalBest {
     139      get { return PersonalBestParameter.ActualValue; }
     140      set { PersonalBestParameter.ActualValue = value; }
     141    }
     142    private ItemArray<RealVector> NeighborBest {
     143      get { return NeighborBestParameter.ActualValue; }
     144      set { NeighborBestParameter.ActualValue = value; }
     145    }
     146    private bool Maximization {
     147      get { return MaximizationParameter.ActualValue.Value; }
     148    }
     149    private ItemArray<IntArray> Neighbors {
     150      get { return NeighborsParameter.ActualValue; }
     151    }
     152    private DoubleMatrix VelocityBounds {
     153      get { return VelocityBoundsParameter.ActualValue; }
     154    }
     155    private DoubleMatrix CurrentVelocityBounds {
     156      get { return CurrentVelocityBoundsParameter.ActualValue; }
     157      set { CurrentVelocityBoundsParameter.ActualValue = value; }
     158    }
     159    private DoubleValue VelocityBoundsScale {
     160      get { return VelocityBoundsScaleParameter.ActualValue; }
     161      set { VelocityBoundsScaleParameter.ActualValue = value; }
     162    }
     163    private DoubleValue VelocityBoundsStartValue {
     164      get { return VelocityBoundsStartValueParameter.ActualValue; }
     165    }
     166    public IDiscreteDoubleValueModifier VelocityBoundsScalingOperator {
     167      get { return VelocityBoundsScalingOperatorParameter.Value; }
     168      set { VelocityBoundsScalingOperatorParameter.Value = value; }
     169    }
     170    private ResultCollection Results {
     171      get { return ResultsParameter.ActualValue; }
     172    }
     173    #endregion
     174
    108175    #region Construction & Cloning
    109176
     
    126193      Parameters.Add(new ScopeTreeLookupParameter<IntArray>("Neighbors", "The list of neighbors for each particle."));
    127194      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
    128       Parameters.Add(new ValueLookupParameter<DoubleValue>("MaxVelocity", "Speed limit for each particle.", new DoubleValue(1000000)));
    129       Parameters.Add(new LookupParameter<DoubleValue>("CurrentMaxVelocity", "Current value of the speed limit."));
     195      Parameters.Add(new ValueLookupParameter<DoubleMatrix>("VelocityBounds", "Maximum velocity for each dimension.", new DoubleMatrix(new double[,] { { -1, 1 } })));
     196      Parameters.Add(new LookupParameter<DoubleMatrix>("CurrentVelocityBounds", "Current value of velocity bounds."));
    130197      Parameters.Add(new LookupParameter<ResultCollection>("Results", "Results"));
    131198
    132199      #region Velocity Bounds Updating
    133       Parameters.Add(new OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>("MaxVelocityScalingOperator", "Modifies the value"));
    134       Parameters.Add(new ValueLookupParameter<DoubleValue>("FinalMaxVelocity", "The value of maximum velocity if PSO has reached maximum iterations.", new DoubleValue(1E-10)));
    135       Parameters.Add(new LookupParameter<IntValue>("MaxVelocityIndex", "The current index.", "Iterations"));
    136       Parameters.Add(new ValueLookupParameter<IntValue>("MaxVelocityStartIndex", "The start index at which to start modifying 'Value'.", new IntValue(0)));
    137       Parameters.Add(new ValueLookupParameter<IntValue>("MaxVelocityEndIndex", "The end index by which 'Value' should have reached 'EndValue'.", "MaxIterations"));
    138       MaxVelocityStartIndexParameter.Hidden = true;
    139       MaxVelocityEndIndexParameter.Hidden = true;
     200      Parameters.Add(new LookupParameter<DoubleValue>("VelocityBoundsScale", "Scale parameter."));
     201      Parameters.Add(new OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>("VelocityBoundsScalingOperator", "Modifies the value"));
     202      Parameters.Add(new ValueLookupParameter<DoubleValue>("VelocityBoundsStartValue", "The start value of 'Value'.", new DoubleValue(1)));
     203      Parameters.Add(new ValueLookupParameter<DoubleValue>("VelocityBoundsEndValue", "The end value of 'Value'.", new DoubleValue(1E-10)));
     204      Parameters.Add(new LookupParameter<IntValue>("VelocityBoundsIndex", "The current index.", "Iterations"));
     205      Parameters.Add(new ValueLookupParameter<IntValue>("VelocityBoundsStartIndex", "The start index at which to start modifying 'Value'.", new IntValue(0)));
     206      Parameters.Add(new ValueLookupParameter<IntValue>("VelocityBoundsEndIndex", "The end index by which 'Value' should have reached 'EndValue'.", "MaxIterations"));
     207      VelocityBoundsStartIndexParameter.Hidden = true;
     208      VelocityBoundsEndIndexParameter.Hidden = true;
    140209      #endregion
    141210
    142211      Initialize();
     212      RegisterEvents();
    143213    }
    144214
     
    158228        Parameters.Remove("BestQuality");
    159229      }
    160     }
     230      RegisterEvents();
     231    }
     232
     233    private void RegisterEvents() {
     234      VelocityBoundsStartValueParameter.ValueChanged += new EventHandler(VelocityBoundsStartValueParameter_ValueChanged);
     235      VelocityBoundsStartValueParameter.Value.ValueChanged += new EventHandler(VelocityBoundsStartValueParameter_Value_ValueChanged);
     236    }
     237
     238    void VelocityBoundsStartValueParameter_Value_ValueChanged(object sender, EventArgs e) {
     239      UpdateVelocityBoundsParamater();
     240    }
     241
     242    void UpdateVelocityBoundsParamater() {
     243      if (VelocityBoundsParameter.Value == null) {
     244        VelocityBoundsParameter.Value = new DoubleMatrix(1, 2);
     245      } else if (VelocityBoundsParameter.Value.Columns != 2) {
     246        VelocityBoundsParameter.Value = new DoubleMatrix(VelocityBoundsParameter.Value.Rows, 2);
     247      }
     248      if (VelocityBoundsStartValueParameter.Value != null) {
     249        DoubleMatrix matrix = VelocityBoundsParameter.Value;
     250        for (int i = 0; i < matrix.Rows; i++) {
     251          matrix[i, 0] = (-1) * VelocityBoundsStartValueParameter.Value.Value;
     252          matrix[i, 1] = VelocityBoundsStartValueParameter.Value.Value;
     253        }
     254      }
     255    }
     256
     257    void VelocityBoundsStartValueParameter_ValueChanged(object sender, EventArgs e) {
     258      if (VelocityBoundsStartValueParameter.Value != null) {
     259        VelocityBoundsStartValueParameter.Value.ValueChanged += new EventHandler(VelocityBoundsStartValueParameter_Value_ValueChanged);
     260      }
     261      UpdateVelocityBoundsParamater();
     262    }
     263
    161264    private void Initialize() {
    162265      ResultsCollector = new ResultsCollector();
    163       ResultsCollector.CollectedValues.Add(CurrentMaxVelocityParameter);
    164       ResultsCollector.CollectedValues.Add(MaxVelocityParameter);
     266      ResultsCollector.CollectedValues.Add(CurrentVelocityBoundsParameter);
     267      ResultsCollector.CollectedValues.Add(VelocityBoundsParameter);
    165268
    166269      foreach (IDiscreteDoubleValueModifier op in ApplicationManager.Manager.GetInstances<IDiscreteDoubleValueModifier>()) {
    167         MaxVelocityScalingOperatorParameter.ValidValues.Add(op);
    168         op.ValueParameter.ActualName = CurrentMaxVelocityParameter.Name;
    169         op.StartValueParameter.ActualName = MaxVelocityParameter.Name;
    170         op.EndValueParameter.ActualName = FinalMaxVelocityParameter.Name;
    171         op.IndexParameter.ActualName = MaxVelocityIndexParameter.Name;
    172         op.StartIndexParameter.ActualName = MaxVelocityStartIndexParameter.Name;
    173         op.EndIndexParameter.ActualName = MaxVelocityEndIndexParameter.Name;
    174       }
    175       MaxVelocityScalingOperatorParameter.Value = null;
     270        VelocityBoundsScalingOperatorParameter.ValidValues.Add(op);
     271        op.ValueParameter.ActualName = VelocityBoundsScaleParameter.Name;
     272        op.StartValueParameter.ActualName = VelocityBoundsStartValueParameter.Name;
     273        op.EndValueParameter.ActualName = VelocityBoundsEndValueParameter.Name;
     274        op.IndexParameter.ActualName = VelocityBoundsIndexParameter.Name;
     275        op.StartIndexParameter.ActualName = VelocityBoundsStartIndexParameter.Name;
     276        op.EndIndexParameter.ActualName = VelocityBoundsEndIndexParameter.Name;
     277      }
     278      VelocityBoundsScalingOperatorParameter.Value = null;
    176279    }
    177280
    178281    public override IOperation Apply() {
    179       var max = MaximizationParameter.ActualValue.Value;
    180       // Update of the personal bests
    181       var points = RealVectorParameter.ActualValue;
    182       var qualities = QualityParameter.ActualValue;
    183       var particles = points.Select((p, i) => new { Particle = p, Index = i })
    184         .Zip(qualities, (p, q) => Tuple.Create(p.Index, p.Particle, q.Value)).ToList();
    185       UpdatePersonalBest(max, particles);
    186 
    187       // SPSO: update of the neighbor bests from the personal bests
    188       var personalBestPoints = PersonalBestParameter.ActualValue;
    189       var personalBestQualities = PersonalBestQualityParameter.ActualValue;
    190       particles = personalBestPoints.Select((p, i) => new { Particle = p, Index = i })
    191         .Zip(personalBestQualities, (p, q) => Tuple.Create(p.Index, p.Particle, q.Value)).ToList();
    192       UpdateNeighborBest(max, particles);
    193 
    194       var next = new OperationCollection() { base.Apply() };
    195       next.Insert(0, ExecutionContext.CreateChildOperation(ResultsCollector));
    196       if (MaxVelocityScalingOperatorParameter.Value != null) {
    197         next.Insert(0, ExecutionContext.CreateChildOperation(MaxVelocityScalingOperatorParameter.Value));
    198       } else CurrentMaxVelocityParameter.ActualValue = new DoubleValue(MaxVelocityParameter.ActualValue.Value);
    199       return next;
    200     }
    201 
    202     private void UpdateNeighborBest(bool maximization, IList<Tuple<int, RealVector, double>> particles) {
    203       var neighbors = NeighborsParameter.ActualValue;
    204       if (neighbors.Length > 0) {
    205         var neighborBest = new ItemArray<RealVector>(neighbors.Length);
    206         var neighborBestQuality = new ItemArray<DoubleValue>(neighbors.Length);
    207         for (int n = 0; n < neighbors.Length; n++) {
    208           var pairs = particles.Where(x => x.Item1 == n || neighbors[n].Contains(x.Item1));
    209           var bestNeighbor = (maximization ? pairs.MaxItems(p => p.Item3)
    210                                            : pairs.MinItems(p => p.Item3)).First();
    211           neighborBest[n] = bestNeighbor.Item2;
    212           neighborBestQuality[n] = new DoubleValue(bestNeighbor.Item3);
     282      UpdateGlobalBest();
     283      UpdateNeighborBest();
     284      UpdatePersonalBest();
     285      return UpdateVelocityBounds();
     286    }
     287
     288    private void UpdateGlobalBest() {
     289      if (SwarmBestQuality == null)
     290        SwarmBestQuality = new DoubleValue();
     291      SwarmBestQuality.Value = Maximization ? Quality.Max(v => v.Value) : Quality.Min(v => v.Value);
     292      BestRealVector = (RealVector)RealVector[Quality.FindIndex(v => v.Value == SwarmBestQuality.Value)].Clone();
     293    }
     294
     295    private void UpdateNeighborBest() {
     296      if (Neighbors.Length > 0) {
     297        var neighborBest = new ItemArray<RealVector>(Neighbors.Length);
     298        var neighborBestQuality = new ItemArray<DoubleValue>(Neighbors.Length);
     299        for (int n = 0; n < Neighbors.Length; n++) {
     300          var pairs = Quality.Zip(RealVector, (q, p) => new { Quality = q, Point = p })
     301            .Where((p, i) => i == n || Neighbors[n].Contains(i));
     302          var bestNeighbor = Maximization ?
     303            pairs.OrderByDescending(p => p.Quality.Value).First() :
     304            pairs.OrderBy(p => p.Quality.Value).First();
     305          neighborBest[n] = bestNeighbor.Point;
     306          neighborBestQuality[n] = bestNeighbor.Quality;
    213307        }
    214         NeighborBestParameter.ActualValue = neighborBest;
    215         NeighborBestQualityParameter.ActualValue = neighborBestQuality;
    216       } else {
    217         // Neighbor best = Global best
    218         var best = maximization ? particles.MaxItems(x => x.Item3).First() : particles.MinItems(x => x.Item3).First();
    219         NeighborBestParameter.ActualValue = new ItemArray<RealVector>(particles.Select(x => best.Item2));
    220         NeighborBestQualityParameter.ActualValue = new ItemArray<DoubleValue>(particles.Select(x => new DoubleValue(best.Item3)));
    221       }
    222     }
    223 
    224     private void UpdatePersonalBest(bool maximization, IList<Tuple<int, RealVector, double>> particles) {
    225       var personalBest = PersonalBestParameter.ActualValue;
    226       var personalBestQuality = PersonalBestQualityParameter.ActualValue;
    227 
    228       if (personalBestQuality.Length == 0) {
    229         personalBestQuality = new ItemArray<DoubleValue>(particles.Select(x => new DoubleValue(x.Item3)));
    230         PersonalBestQualityParameter.ActualValue = personalBestQuality;
    231       }
    232       foreach (var p in particles) {
    233         if (maximization && p.Item3 > personalBestQuality[p.Item1].Value ||
    234           !maximization && p.Item3 < personalBestQuality[p.Item1].Value) {
    235           personalBestQuality[p.Item1].Value = p.Item3;
    236           personalBest[p.Item1] = (RealVector)p.Item2.Clone();
     308        NeighborBest = neighborBest;
     309        NeighborBestQuality = neighborBestQuality;
     310      }
     311    }
     312
     313    private void UpdatePersonalBest() {
     314      if (PersonalBestQuality.Length == 0)
     315        PersonalBestQuality = (ItemArray<DoubleValue>)Quality.Clone();
     316      for (int i = 0; i < RealVector.Length; i++) {
     317        if (Maximization && Quality[i].Value > PersonalBestQuality[i].Value ||
     318          !Maximization && Quality[i].Value < PersonalBestQuality[i].Value) {
     319          PersonalBestQuality[i].Value = Quality[i].Value;
     320          PersonalBest[i] = RealVector[i];
    237321        }
    238322      }
    239       PersonalBestParameter.ActualValue = personalBest;
     323    }
     324
     325    private IOperation UpdateVelocityBounds() {
     326      if (CurrentVelocityBounds == null)
     327        CurrentVelocityBounds = (DoubleMatrix)VelocityBounds.Clone();
     328
     329      if (VelocityBoundsScalingOperator == null)
     330        return new OperationCollection() {
     331          ExecutionContext.CreateChildOperation(ResultsCollector),       
     332          base.Apply()
     333        };
     334
     335      DoubleMatrix matrix = CurrentVelocityBounds;
     336      if (VelocityBoundsScale == null && VelocityBoundsStartValue != null) {
     337        VelocityBoundsScale = new DoubleValue(VelocityBoundsStartValue.Value);
     338      }
     339      for (int i = 0; i < matrix.Rows; i++) {
     340        for (int j = 0; j < matrix.Columns; j++) {
     341          if (matrix[i, j] >= 0) {
     342            matrix[i, j] = VelocityBoundsScale.Value;
     343          } else {
     344            matrix[i, j] = (-1) * VelocityBoundsScale.Value;
     345          }
     346        }
     347      }
     348
     349      return new OperationCollection() {
     350        ExecutionContext.CreateChildOperation(ResultsCollector),
     351        ExecutionContext.CreateChildOperation(VelocityBoundsScalingOperator),       
     352        base.Apply()
     353      };
    240354    }
    241355  }
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorTotallyConnectedParticleUpdater.cs

    r15096 r15102  
    3030  [StorableClass]
    3131  [NonDiscoverableType]
    32   [Obsolete("Replaced by SPSO2007ParticleUpdater")]
     32  [Obsolete("Use SPSO2011ParticleUpdater")]
    3333  internal sealed class RealVectorTotallyConnectedParticleUpdater : RealVectorParticleUpdater {
    3434
     
    4343    #endregion
    4444
    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;
     45    public override IOperation Apply() {
     46      double inertia = Inertia.Value;
     47      double personalBestAttraction = PersonalBestAttraction.Value;
     48      double neighborBestAttraction = NeighborBestAttraction.Value;
    5349
    54       var random = RandomParameter.ActualValue;
     50      RealVector velocity = new RealVector(Velocity.Length);
     51      RealVector position = new RealVector(RealVector.Length);
     52      double r_p = Random.NextDouble();
     53      double r_g = Random.NextDouble();
    5554
    5655      for (int i = 0; i < velocity.Length; i++) {
    57         double r_p = random.NextDouble();
    58         double r_g = random.NextDouble();
    5956        velocity[i] =
    60           velocity[i] * inertia +
    61           (personalBest[i] - position[i]) * personalBestAttraction * r_p +
    62           (neighborBest[i] - position[i]) * neighborBestAttraction * r_g;
     57          Velocity[i] * inertia +
     58          (PersonalBest[i] - RealVector[i]) * personalBestAttraction * r_p +
     59          (BestPoint[i] - RealVector[i]) * neighborBestAttraction * r_g;
    6360      }
    6461
    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 
    97     public override IOperation Apply() {
    98       UpdateVelocity();
    99       UpdatePosition();
     62      MoveParticle(velocity, position);
    10063
    10164      return base.Apply();
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/SPSO2007ParticleUpdater.cs

    r15096 r15102  
    2424using HeuristicLab.Core;
    2525using HeuristicLab.Data;
     26using HeuristicLab.Parameters;
    2627using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2728
     
    2930  [Item("SPSO 2007 Particle Updater", "Updates the particle's position according to the formulae described in SPSO 2007.")]
    3031  [StorableClass]
    31   public sealed class SPSO2007ParticleUpdater : RealVectorParticleUpdater {
     32  public sealed class SPSO2007ParticleUpdater : SPSOParticleUpdater {
    3233
    3334    #region Construction & Cloning
     
    4142    #endregion
    4243   
    43     public static void UpdateVelocity(IRandom random, RealVector velocity, double maxVelocity, RealVector position, double inertia, RealVector personalBest, double personalBestAttraction, RealVector neighborBest, double neighborBestAttraction) {
     44    public static void UpdateVelocity(IRandom random, RealVector velocity, double maxVelocity, RealVector position, double inertia, RealVector personalBest, double personalBestAttraction, RealVector neighborBest, double neighborBestAttraction, double c = 1.193) {
    4445      for (int i = 0; i < velocity.Length; i++) {
    45         double r_p = random.NextDouble() * 1.193;
    46         double r_g = random.NextDouble() * 1.193;
     46        double r_p = random.NextDouble() * c;
     47        double r_g = random.NextDouble() * c;
    4748        velocity[i] =
    4849          velocity[i] * inertia +
     
    6970        if (position[i] < min) {
    7071          position[i] = min;
    71           velocity[i] = 0;
     72          velocity[i] = 0; // SPSO 2007
    7273        }
    7374        if (position[i] > max) {
    7475          position[i] = max;
    75           velocity[i] = 0;
     76          velocity[i] = 0; // SPSO 2007
    7677        }
    7778      }
     
    9091      var neighborBest = NeighborBestParameter.ActualValue;
    9192      var neighborBestAttraction = NeighborBestAttractionParameter.ActualValue.Value;
    92      
    93       UpdateVelocity(random, velocity, maxVelocity, position, inertia, personalBest, personalBestAttraction, neighborBest, neighborBestAttraction);
     93      var maxBeyond = MaxBeyondBestParameter.ActualValue.Value;
     94
     95      UpdateVelocity(random, velocity, maxVelocity, position, inertia, personalBest, personalBestAttraction, neighborBest, neighborBestAttraction, maxBeyond);
    9496      UpdatePosition(bounds, velocity, position);
    9597
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/SPSO2011ParticleUpdater.cs

    r15096 r15102  
    2929  [Item("SPSO 2011 Particle Updater", "Updates the particle's position according to the formulae described in SPSO 2011.")]
    3030  [StorableClass]
    31   public sealed class SPSO2011ParticleUpdater : RealVectorParticleUpdater {
    32 
     31  public sealed class SPSO2011ParticleUpdater : SPSOParticleUpdater {
    3332    #region Construction & Cloning
    3433    [StorableConstructor]
     
    4140    #endregion
    4241   
    43     public static void UpdateVelocity(IRandom random, RealVector velocity, double maxVelocity, RealVector position, double inertia, RealVector personalBest, double personalBestAttraction, RealVector neighborBest, double neighborBestAttraction) {
     42    public static void UpdateVelocity(IRandom random, RealVector velocity, double maxVelocity, RealVector position, double inertia, RealVector personalBest, double personalBestAttraction, RealVector neighborBest, double neighborBestAttraction, double c = 1.193) {
    4443      var gravity = new double[velocity.Length];
    4544      var direct = new RealVector(velocity.Length);
     
    4746
    4847      for (int i = 0; i < velocity.Length; i++) {
    49         var g_id = 1.193 * ((personalBest[i] + neighborBest[i] - 2 * position[i]) / 3.0);
     48        var g_id = c * ((personalBest[i] + neighborBest[i] - 2 * position[i]) / 3.0);
    5049        gravity[i] = g_id + position[i];
    5150        direct[i] = (random.NextDouble() - 0.5) * 2;
     
    101100      var neighborBest = NeighborBestParameter.ActualValue;
    102101      var neighborBestAttraction = NeighborBestAttractionParameter.ActualValue.Value;
    103      
    104       UpdateVelocity(random, velocity, maxVelocity, position, inertia, personalBest, personalBestAttraction, neighborBest, neighborBestAttraction);
     102      var maxBeyond = MaxBeyondBestParameter.ActualValue.Value;
     103
     104      UpdateVelocity(random, velocity, maxVelocity, position, inertia, personalBest, personalBestAttraction, neighborBest, neighborBestAttraction, maxBeyond);
    105105      UpdatePosition(bounds, velocity, position);
    106106
Note: See TracChangeset for help on using the changeset viewer.