Free cookie consent management tool by TermsFeed Policy Generator

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

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

#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
File size: 4.4 KB
RevLine 
[15096]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;
27
28namespace HeuristicLab.Encodings.RealVectorEncoding {
29  [Item("SPSO 2011 Particle Updater", "Updates the particle's position according to the formulae described in SPSO 2011.")]
30  [StorableClass]
31  public sealed class SPSO2011ParticleUpdater : RealVectorParticleUpdater {
32
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) {
44      var gravity = new double[velocity.Length];
45      var direct = new RealVector(velocity.Length);
46      var radius = 0.0;
47
48      for (int i = 0; i < velocity.Length; i++) {
49        var g_id = 1.193 * ((personalBest[i] + neighborBest[i] - 2 * position[i]) / 3.0);
50        gravity[i] = g_id + position[i];
51        direct[i] = (random.NextDouble() - 0.5) * 2;
52        radius += g_id * g_id;
53      }
54
55      radius = random.NextDouble() * Math.Sqrt(radius);
56
57      var unitscale = Math.Sqrt(direct.DotProduct(direct));
58      if (unitscale > 0) {
59        for (var i = 0; i < velocity.Length; i++) {
60          velocity[i] = velocity[i] * inertia + gravity[i] + direct[i] * radius / unitscale - position[i];
61        }
62      }
63
64      var speed = Math.Sqrt(velocity.DotProduct(velocity));
65      if (speed > maxVelocity) {
66        for (var i = 0; i < velocity.Length; i++) {
67          velocity[i] *= maxVelocity / speed;
68        }
69      }
70    }
71
72    public static void UpdatePosition(DoubleMatrix bounds, RealVector velocity, RealVector position) {
73      for (int i = 0; i < velocity.Length; i++) {
74        position[i] += velocity[i];
75      }
76
77      for (int i = 0; i < position.Length; i++) {
78        double min = bounds[i % bounds.Rows, 0];
79        double max = bounds[i % bounds.Rows, 1];
80        if (position[i] < min) {
81          position[i] = min;
82          velocity[i] = -0.5 * velocity[i];
83        }
84        if (position[i] > max) {
85          position[i] = max;
86          velocity[i] = -0.5 * velocity[i];
87        }
88      }
89    }
90
91    public override IOperation Apply() {
92      var random = RandomParameter.ActualValue;
93      var velocity = VelocityParameter.ActualValue;
94      var maxVelocity = CurrentMaxVelocityParameter.ActualValue.Value;
95      var position = RealVectorParameter.ActualValue;
96      var bounds = BoundsParameter.ActualValue;
97
98      var inertia = CurrentInertiaParameter.ActualValue.Value;
99      var personalBest = PersonalBestParameter.ActualValue;
100      var personalBestAttraction = PersonalBestAttractionParameter.ActualValue.Value;
101      var neighborBest = NeighborBestParameter.ActualValue;
102      var neighborBestAttraction = NeighborBestAttractionParameter.ActualValue.Value;
103     
104      UpdateVelocity(random, velocity, maxVelocity, position, inertia, personalBest, personalBestAttraction, neighborBest, neighborBestAttraction);
105      UpdatePosition(bounds, velocity, position);
106
107      return base.Apply();
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.