Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/29/17 15:26:16 (7 years ago)
Author:
abeham
Message:

#2797:

  • Updated PSO to make it more compatible with SPSO 2011
  • Removed truncation of velocity vector and instead rescaled it given the maximum velocity
  • Added non-zero initial velocity according to SPSO 2011
  • Removed complicated bouncing code due to box constraints and instead implemented as described in SPSO 2011
  • Calculating neighbor best has been changed to use personal best
  • Avoiding local and global particle update and instead relying on neighborbest
  • More randomization during velocity update by using a separate random numbers per dimension
  • Reusing problem specific solution creator in RealVectorParticleCreator instead of always using UniformRandomRealVectorCreator
File:
1 edited

Legend:

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

    r14185 r15091  
    5151      get { return (ILookupParameter<RealVector>)Parameters["NeighborBest"]; }
    5252    }
    53     public LookupParameter<RealVector> BestRealVectorParameter {
    54       get { return (LookupParameter<RealVector>)Parameters["BestRealVector"]; }
    55     }
    5653    public ILookupParameter<RealVector> RealVectorParameter {
    5754      get { return (ILookupParameter<RealVector>)Parameters["RealVector"]; }
     
    6057      get { return (ILookupParameter<DoubleMatrix>)Parameters["Bounds"]; }
    6158    }
    62     public ILookupParameter<DoubleMatrix> CurrentVelocityBoundsParameter {
    63       get { return (ILookupParameter<DoubleMatrix>)Parameters["CurrentVelocityBounds"]; }
     59    public ILookupParameter<DoubleValue> CurrentMaxVelocityParameter {
     60      get { return (ILookupParameter<DoubleValue>)Parameters["CurrentMaxVelocity"]; }
    6461    }
    6562    public ILookupParameter<DoubleValue> InertiaParameter {
     
    7370    }
    7471    #endregion
    75 
    76     #region Parameter Values
    77     protected IRandom Random {
    78       get { return RandomParameter.ActualValue; }
    79     }
    80     protected RealVector Velocity {
    81       get { return VelocityParameter.ActualValue; }
    82       set { VelocityParameter.ActualValue = value; }
    83     }
    84     protected RealVector PersonalBest {
    85       get { return PersonalBestParameter.ActualValue; }
    86     }
    87     protected RealVector BestPoint {
    88       get { return BestRealVectorParameter.ActualValue; }
    89     }
    90     protected RealVector RealVector {
    91       get { return RealVectorParameter.ActualValue; }
    92       set { RealVectorParameter.ActualValue = value; }
    93     }
    94     protected RealVector NeighborBest {
    95       get { return NeighborBestParameter.ActualValue; }
    96     }
    97     protected DoubleMatrix Bounds {
    98       get { return BoundsParameter.ActualValue; }
    99     }
    100     protected DoubleMatrix CurrentVelocityBounds {
    101       get { return CurrentVelocityBoundsParameter.ActualValue; }
    102     }
    103     protected DoubleValue Inertia {
    104       get { return InertiaParameter.ActualValue; }
    105     }
    106     protected DoubleValue PersonalBestAttraction {
    107       get { return PersonalBestAttractionParameter.ActualValue; }
    108     }
    109     protected DoubleValue NeighborBestAttraction {
    110       get { return NeighborBestAttractionParameter.ActualValue; }
    111     }
    112     #endregion
    113 
     72   
    11473    #region Construction & Cloning
    11574    [StorableConstructor]
     
    12281      Parameters.Add(new LookupParameter<RealVector>("Velocity", "Particle's current velocity."));
    12382      Parameters.Add(new LookupParameter<RealVector>("PersonalBest", "Particle's personal best solution."));
    124       Parameters.Add(new LookupParameter<RealVector>("BestRealVector", "Global best position."));
    12583      Parameters.Add(new LookupParameter<RealVector>("NeighborBest", "Best neighboring solution."));
    12684      Parameters.Add(new LookupParameter<DoubleMatrix>("Bounds", "The lower and upper bounds for each dimension of the position vector for the current problem."));
    127       Parameters.Add(new LookupParameter<DoubleMatrix>("CurrentVelocityBounds", "Upper and lower bounds for the particle's velocity vector."));
     85      Parameters.Add(new LookupParameter<DoubleValue>("CurrentMaxVelocity", "Maximum for the particle's velocity vector."));
    12886      Parameters.Add(new LookupParameter<DoubleValue>("CurrentInertia", "The weight for the particle's velocity vector."));
    12987      Parameters.Add(new LookupParameter<DoubleValue>("PersonalBestAttraction", "The weight for the particle's personal best position."));
     
    13290    #endregion
    13391
    134     protected void MoveParticle(RealVector velocity, RealVector position) {
    135       BoundsChecker.Apply(velocity, CurrentVelocityBounds);
     92    protected void UpdateVelocity() {
     93      var velocity = VelocityParameter.ActualValue;
     94      var position = RealVectorParameter.ActualValue;
     95      var inertia = InertiaParameter.ActualValue.Value;
     96      var personalBest = PersonalBestParameter.ActualValue;
     97      var personalBestAttraction = PersonalBestAttractionParameter.ActualValue.Value;
     98      var neighborBest = NeighborBestParameter.ActualValue;
     99      var neighborBestAttraction = NeighborBestAttractionParameter.ActualValue.Value;
     100
     101      var random = RandomParameter.ActualValue;
     102
    136103      for (int i = 0; i < velocity.Length; i++) {
    137         position[i] = RealVector[i] + velocity[i];
     104        double r_p = random.NextDouble();
     105        double r_g = random.NextDouble();
     106        velocity[i] =
     107          velocity[i] * inertia +
     108          (personalBest[i] - position[i]) * personalBestAttraction * r_p +
     109          (neighborBest[i] - position[i]) * neighborBestAttraction * r_g;
    138110      }
     111
     112      var maxVelocity = CurrentMaxVelocityParameter.ActualValue.Value;
     113      var speed = Math.Sqrt(velocity.DotProduct(velocity));
     114      if (speed > maxVelocity) {
     115        for (var i = 0; i < velocity.Length; i++) {
     116          velocity[i] *= maxVelocity / speed;
     117        }
     118      }
     119    }
     120
     121    protected void UpdatePosition() {
     122      var velocity = VelocityParameter.ActualValue;
     123      var position = RealVectorParameter.ActualValue;
     124
     125      for (int i = 0; i < velocity.Length; i++) {
     126        position[i] += velocity[i];
     127      }
     128
     129      var bounds = BoundsParameter.ActualValue;
    139130      for (int i = 0; i < position.Length; i++) {
    140         double min = Bounds[i % Bounds.Rows, 0];
    141         double max = Bounds[i % Bounds.Rows, 1];
     131        double min = bounds[i % bounds.Rows, 0];
     132        double max = bounds[i % bounds.Rows, 1];
    142133        if (position[i] < min) {
    143           int reflectionCount = (int)Math.Truncate((min - position[i]) / (max - min)) + 1;
    144           double reflection = (min - position[i]) % (max - min);
    145           if (IsOdd(reflectionCount)) {
    146             position[i] = min + reflection;
    147             velocity[i] = -velocity[i];
    148 
    149           } else {
    150             position[i] = max - reflection;
    151           }
     134          position[i] = min;
     135          velocity[i] = -0.5 * velocity[i]; // SPSO 2011
    152136        }
    153137        if (position[i] > max) {
    154           int reflectionCount = (int)Math.Truncate((position[i] - max) / (max - min)) + 1;
    155           double reflection = (position[i] - max) % (max - min);
    156           if (IsOdd(reflectionCount)) {
    157             position[i] = max - reflection;
    158             velocity[i] = -velocity[i];
    159           } else {
    160             position[i] = min + reflection;
    161           }
     138          position[i] = max;
     139          velocity[i] = -0.5 * velocity[i]; // SPSO 2011
    162140        }
    163141      }
    164 
    165       RealVector = position;
    166       Velocity = velocity;
    167     }
    168 
    169     private static bool IsOdd(int number) {
    170       return number % 2 == 1;
    171142    }
    172143  }
Note: See TracChangeset for help on using the changeset viewer.