Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/29/17 23:04:03 (7 years 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
Location:
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators
Files:
2 added
4 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();
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorParticleUpdater.cs

    r15091 r15096  
    1919 */
    2020#endregion
    21 
    22 using System;
     21 
    2322using HeuristicLab.Common;
    2423using HeuristicLab.Core;
    2524using HeuristicLab.Data;
    2625using HeuristicLab.Operators;
     26using HeuristicLab.Optimization;
    2727using HeuristicLab.Parameters;
    2828using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    6060      get { return (ILookupParameter<DoubleValue>)Parameters["CurrentMaxVelocity"]; }
    6161    }
    62     public ILookupParameter<DoubleValue> InertiaParameter {
     62    public ILookupParameter<DoubleValue> CurrentInertiaParameter {
    6363      get { return (ILookupParameter<DoubleValue>)Parameters["CurrentInertia"]; }
    6464    }
     65    ILookupParameter<DoubleValue> IParticleUpdater.InertiaParameter { get { return CurrentInertiaParameter; } }
     66
    6567    public ILookupParameter<DoubleValue> PersonalBestAttractionParameter {
    6668      get { return (ILookupParameter<DoubleValue>)Parameters["PersonalBestAttraction"]; }
     
    8991    }
    9092    #endregion
    91 
    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 
    103       for (int i = 0; i < velocity.Length; 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;
    110       }
    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;
    130       for (int i = 0; i < position.Length; i++) {
    131         double min = bounds[i % bounds.Rows, 0];
    132         double max = bounds[i % bounds.Rows, 1];
    133         if (position[i] < min) {
    134           position[i] = min;
    135           velocity[i] = -0.5 * velocity[i]; // SPSO 2011
    136         }
    137         if (position[i] > max) {
    138           position[i] = max;
    139           velocity[i] = -0.5 * velocity[i]; // SPSO 2011
    140         }
    141       }
    142     }
    14393  }
    14494}
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorSwarmUpdater.cs

    r15091 r15096  
    126126      Parameters.Add(new ScopeTreeLookupParameter<IntArray>("Neighbors", "The list of neighbors for each particle."));
    127127      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(1)));
     128      Parameters.Add(new ValueLookupParameter<DoubleValue>("MaxVelocity", "Speed limit for each particle.", new DoubleValue(1000000)));
    129129      Parameters.Add(new LookupParameter<DoubleValue>("CurrentMaxVelocity", "Current value of the speed limit."));
    130130      Parameters.Add(new LookupParameter<ResultCollection>("Results", "Results"));
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorTotallyConnectedParticleUpdater.cs

    r15091 r15096  
    2323using HeuristicLab.Common;
    2424using HeuristicLab.Core;
    25 using HeuristicLab.Optimization;
    2625using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2726using HeuristicLab.PluginInfrastructure;
     
    3130  [StorableClass]
    3231  [NonDiscoverableType]
    33   [Obsolete("Same as the RealVectorNeighborhoodParticleUpdate")]
     32  [Obsolete("Replaced by SPSO2007ParticleUpdater")]
    3433  internal sealed class RealVectorTotallyConnectedParticleUpdater : RealVectorParticleUpdater {
    3534
     
    4443    #endregion
    4544
     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
    4697    public override IOperation Apply() {
    4798      UpdateVelocity();
Note: See TracChangeset for help on using the changeset viewer.