Free cookie consent management tool by TermsFeed Policy Generator

Changeset 15096 for trunk


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
Files:
2 added
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/ParticleSwarmOptimization.cs

    r15091 r15096  
    177177      Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
    178178      Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
    179       Parameters.Add(new ValueParameter<IntValue>("SwarmSize", "Size of the particle swarm.", new IntValue(10)));
     179      Parameters.Add(new ValueParameter<IntValue>("SwarmSize", "Size of the particle swarm.", new IntValue(20)));
    180180      Parameters.Add(new ValueParameter<IntValue>("MaxIterations", "Maximal number of iterations.", new IntValue(1000)));
    181181      Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each generation.", new MultiAnalyzer()));
    182       Parameters.Add(new ValueParameter<DoubleValue>("Inertia", "Inertia weight on a particle's movement (omega).", new DoubleValue(0.9)));
    183       Parameters.Add(new ValueParameter<DoubleValue>("PersonalBestAttraction", "Weight for particle's pull towards its personal best soution (phi_p).", new DoubleValue(0.05)));
    184       Parameters.Add(new ValueParameter<DoubleValue>("NeighborBestAttraction", "Weight for pull towards the neighborhood best solution or global best solution in case of a totally connected topology (phi_g).", new DoubleValue(0.5)));
     182      Parameters.Add(new ValueParameter<DoubleValue>("Inertia", "Inertia weight on a particle's movement (omega).", new DoubleValue(0.8)));
     183      Parameters.Add(new ValueParameter<DoubleValue>("PersonalBestAttraction", "Weight for particle's pull towards its personal best soution (phi_p).", new DoubleValue(1)));
     184      Parameters.Add(new ValueParameter<DoubleValue>("NeighborBestAttraction", "Weight for pull towards the neighborhood best solution or global best solution in case of a totally connected topology (phi_g).", new DoubleValue(1)));
    185185      Parameters.Add(new ConstrainedValueParameter<IParticleCreator>("ParticleCreator", "Operator that creates a new particle."));
    186186      Parameters.Add(new ConstrainedValueParameter<IParticleUpdater>("ParticleUpdater", "Operator that updates a particle."));
     
    189189      Parameters.Add(new OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>("InertiaUpdater", "Updates the omega parameter."));
    190190      Parameters.Add(new ConstrainedValueParameter<ISwarmUpdater>("SwarmUpdater", "Encoding-specific parameter which is provided by the problem. May provide additional encoding-specific parameters, such as velocity bounds for real valued problems"));
    191       ParticleUpdaterParameter.Hidden = true;
    192191
    193192      RandomCreator randomCreator = new RandomCreator();
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/HeuristicLab.Encodings.RealVectorEncoding-3.3.csproj

    r15091 r15096  
    116116    <Compile Include="Creators\NormalDistributedRealVectorCreator.cs" />
    117117    <Compile Include="Interfaces\IRealVectorMultiNeighborhoodShakingOperator.cs" />
     118    <Compile Include="ParticleOperators\SPSO2011ParticleUpdater.cs" />
     119    <Compile Include="ParticleOperators\SPSO2007ParticleUpdater.cs" />
    118120    <Compile Include="ParticleOperators\RealVectorParticleCreator.cs" />
    119121    <Compile Include="Crossovers\BlendAlphaBetaCrossover.cs" />
  • 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();
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/RealVector.cs

    r15091 r15096  
    7878    }
    7979
     80    public void Add(RealVector other) {
     81      if (other.Length != Length) throw new ArgumentException("Vectors are of unequal length.");
     82      for (var i = 0; i < Length; i++)
     83        this[i] += other[i];
     84    }
     85
     86    public void Subtract(RealVector other) {
     87      if (other.Length != Length) throw new ArgumentException("Vectors are of unequal length.");
     88      for (var i = 0; i < Length; i++)
     89        this[i] -= other[i];
     90    }
     91
    8092    public double DotProduct(RealVector other) {
    8193      if (other.Length != Length) throw new ArgumentException("Vectors are of unequal length.");
  • trunk/sources/HeuristicLab.Tests/HeuristicLab-3.3/Samples/PsoSchwefelSampleTest.cs

    r15092 r15096  
    5252      SamplesUtils.RunAlgorithm(pso);
    5353      if (Environment.Is64BitProcess) {
    54         Assert.AreEqual(118.43833503632464, SamplesUtils.GetDoubleResult(pso, "BestQuality"));
    55         Assert.AreEqual(118.43935663125784, SamplesUtils.GetDoubleResult(pso, "CurrentAverageQuality"));
    56         Assert.AreEqual(118.44721627137812, SamplesUtils.GetDoubleResult(pso, "CurrentWorstQuality"));
    57         Assert.AreEqual(1000, SamplesUtils.GetIntResult(pso, "Iterations"));
     54        Assert.AreEqual(2.8334909529803554E-08, SamplesUtils.GetDoubleResult(pso, "BestQuality"));
     55        Assert.AreEqual(128.08680460446624, SamplesUtils.GetDoubleResult(pso, "CurrentAverageQuality"));
     56        Assert.AreEqual(713.67728101375587, SamplesUtils.GetDoubleResult(pso, "CurrentWorstQuality"));
     57        Assert.AreEqual(200, SamplesUtils.GetIntResult(pso, "Iterations"));
    5858      } else {
    59         Assert.AreEqual(118.4383350363247, SamplesUtils.GetDoubleResult(pso, "BestQuality"));
    60         Assert.AreEqual(118.43935663125787, SamplesUtils.GetDoubleResult(pso, "CurrentAverageQuality"));
    61         Assert.AreEqual(118.44721627137824, SamplesUtils.GetDoubleResult(pso, "CurrentWorstQuality"));
    62         Assert.AreEqual(1000, SamplesUtils.GetIntResult(pso, "Iterations"));
     59        Assert.AreEqual(2.8334909529803554E-08, SamplesUtils.GetDoubleResult(pso, "BestQuality"));
     60        Assert.AreEqual(128.08680460446624, SamplesUtils.GetDoubleResult(pso, "CurrentAverageQuality"));
     61        Assert.AreEqual(713.67728101375587, SamplesUtils.GetDoubleResult(pso, "CurrentWorstQuality"));
     62        Assert.AreEqual(200, SamplesUtils.GetIntResult(pso, "Iterations"));
    6363      }
    6464    }
     
    8080      pso.Description = "A particle swarm optimization algorithm which solves the 2-dimensional Schwefel test function (based on the description in Pedersen, M.E.H. (2010). PhD thesis. University of Southampton)";
    8181      pso.Problem = problem;
    82       pso.Inertia.Value = 10;
    83       pso.MaxIterations.Value = 1000;
    84       pso.NeighborBestAttraction.Value = 0.5;
    85       pso.PersonalBestAttraction.Value = -0.01;
    86       pso.SwarmSize.Value = 50;
     82      pso.Inertia.Value = 1.1;
     83      pso.MaxIterations.Value = 200;
     84      pso.NeighborBestAttraction.Value = 1;
     85      pso.PersonalBestAttraction.Value = 1;
     86      pso.SwarmSize.Value = 40;
    8787
    8888      var inertiaUpdater = pso.InertiaUpdaterParameter.ValidValues
    8989        .OfType<ExponentialDiscreteDoubleValueModifier>()
    9090        .Single();
    91       inertiaUpdater.StartValueParameter.Value = new DoubleValue(10);
    92       inertiaUpdater.EndValueParameter.Value = new DoubleValue(0.8);
     91      inertiaUpdater.EndValueParameter.Value = new DoubleValue(0.721);
    9392      pso.InertiaUpdater = inertiaUpdater;
    94 
    95       pso.ParticleCreator = pso.ParticleCreatorParameter.ValidValues
    96         .OfType<RealVectorParticleCreator>()
    97         .Single();
    98       var swarmUpdater = pso.SwarmUpdaterParameter.ValidValues
    99         .OfType<RealVectorSwarmUpdater>()
    100         .Single();
    101       swarmUpdater.MaxVelocityParameter.Value = new DoubleValue(20.0);
    102       swarmUpdater.FinalMaxVelocityParameter.Value = new DoubleValue(1.0);
    103       swarmUpdater.MaxVelocityScalingOperatorParameter.Value = swarmUpdater.MaxVelocityScalingOperatorParameter.ValidValues
    104         .OfType<ExponentialDiscreteDoubleValueModifier>()
    105         .Single();
    106 
     93     
    10794      pso.TopologyInitializer = null;
    10895      pso.TopologyUpdater = null;
    109       pso.SwarmUpdater = swarmUpdater;
    11096      pso.Seed.Value = 0;
    11197      pso.SetSeedRandomly.Value = true;
Note: See TracChangeset for help on using the changeset viewer.