Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/SPSO2011ParticleUpdater.cs @ 15181

Last change on this file since 15181 was 15181, checked in by abeham, 7 years ago

#2797:

  • Added IStochasticOperator interface to MultiPSOTopologyUpdater
  • Changed parameter defaults to those described in the paper
  • Added analyzer placeholder for the last iteration (has not been previously analyzed)
  • Changed random topology initializer to include itself (to be able to use it with SPSOSwarmUpdater -> this should not change the old RealVectorSwarmUpdater)
  • Changed ring topology initializer to include itself (same as above)
  • Changed von neumann topology initializer to include itself (same as above)
  • Added SPSO compatible random topology initializer (as described in the paper by Clerc)
  • Changed sampling of the random directional vector to be uniformly random on the surface of a hypersphere to avoid a slight bias in diagonal direction
  • Updating SwarmBestQuality and BestRealVector parameters in SPSOSwarmUpdater (an oversight)
  • Added a faster method to create a copy of a RealVector (based on Array.Copy)
  • Updated the sample
  • Updated the sample's test results (due to changed sampling in SPSO2011ParticleUpdater)
File size: 5.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Random;
28
29namespace HeuristicLab.Encodings.RealVectorEncoding {
30  [Item("SPSO 2011 Particle Updater", "Updates the particle's position according to the formulae described in SPSO 2011.")]
31  [StorableClass]
32  public sealed class SPSO2011ParticleUpdater : SPSOParticleUpdater {
33    #region Construction & Cloning
34    [StorableConstructor]
35    private SPSO2011ParticleUpdater(bool deserializing) : base(deserializing) { }
36    private SPSO2011ParticleUpdater(SPSO2011ParticleUpdater original, Cloner cloner) : base(original, cloner) { }
37    public SPSO2011ParticleUpdater() : base() { }
38    public override IDeepCloneable Clone(Cloner cloner) {
39      return new SPSO2011ParticleUpdater(this, cloner);
40    }
41    #endregion
42   
43    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) {
44      var gravity = new double[velocity.Length];
45      var direction = new RealVector(velocity.Length);
46      var radius = 0.0;
47
48      var nd = new NormalDistributedRandom(random, 0, 1);
49
50      for (int i = 0; i < velocity.Length; i++) {
51        var g_id = c * ((personalBest[i] + neighborBest[i] - 2 * position[i]) / 3.0);
52        // center of the hyper-sphere
53        gravity[i] = g_id + position[i];
54        // a random direction vector uniform over the surface of hyper-sphere, see http://mathworld.wolfram.com/HyperspherePointPicking.html
55        direction[i] = nd.NextDouble();
56        radius += g_id * g_id;
57      }
58
59      // randomly choose a radius within the hyper-sphere
60      radius = random.NextDouble() * Math.Sqrt(radius);
61
62      // unitscale is used to rescale the random direction vector to unit length, resp. length of the radius
63      var unitscale = Math.Sqrt(direction.DotProduct(direction));
64      if (unitscale > 0) {
65        for (var i = 0; i < velocity.Length; i++) {
66          var sampledPos = gravity[i] + direction[i] * radius / unitscale;
67          velocity[i] = velocity[i] * inertia + sampledPos - position[i];
68        }
69      }
70
71      var speed = Math.Sqrt(velocity.DotProduct(velocity));
72      if (speed > maxVelocity) {
73        for (var i = 0; i < velocity.Length; i++) {
74          velocity[i] *= maxVelocity / speed;
75        }
76      }
77    }
78
79    public static void UpdatePosition(DoubleMatrix bounds, RealVector velocity, RealVector position) {
80      for (int i = 0; i < velocity.Length; i++) {
81        position[i] += velocity[i];
82      }
83
84      for (int i = 0; i < position.Length; i++) {
85        double min = bounds[i % bounds.Rows, 0];
86        double max = bounds[i % bounds.Rows, 1];
87        if (position[i] < min) {
88          position[i] = min;
89          velocity[i] = -0.5 * velocity[i];
90        }
91        if (position[i] > max) {
92          position[i] = max;
93          velocity[i] = -0.5 * velocity[i];
94        }
95      }
96    }
97
98    public override IOperation Apply() {
99      var random = RandomParameter.ActualValue;
100      var velocity = VelocityParameter.ActualValue;
101      var maxVelocity = CurrentMaxVelocityParameter.ActualValue.Value;
102      var position = RealVectorParameter.ActualValue;
103      var bounds = BoundsParameter.ActualValue;
104
105      var inertia = CurrentInertiaParameter.ActualValue.Value;
106      var personalBest = PersonalBestParameter.ActualValue;
107      var personalBestAttraction = PersonalBestAttractionParameter.ActualValue.Value;
108      var neighborBest = NeighborBestParameter.ActualValue;
109      var neighborBestAttraction = NeighborBestAttractionParameter.ActualValue.Value;
110      var maxBeyond = MaxBeyondBestParameter.ActualValue.Value;
111
112      UpdateVelocity(random, velocity, maxVelocity, position, inertia, personalBest, personalBestAttraction, neighborBest, neighborBestAttraction, maxBeyond);
113      UpdatePosition(bounds, velocity, position);
114
115      return base.Apply();
116    }
117  }
118}
Note: See TracBrowser for help on using the repository browser.