Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/SPSO2007ParticleUpdater.cs @ 17097

Last change on this file since 17097 was 17097, checked in by mkommend, 5 years ago

#2520: Merged 16565 - 16579 into stable.

File size: 4.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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 HEAL.Attic;
27
28namespace HeuristicLab.Encodings.RealVectorEncoding {
29  [Item("SPSO 2007 Particle Updater", "Updates the particle's position according to the formulae described in SPSO 2007.")]
30  [StorableType("60C34D7A-CA73-432C-9EB1-05E503D8D2DD")]
31  public sealed class SPSO2007ParticleUpdater : SPSOParticleUpdater {
32
33    #region Construction & Cloning
34    [StorableConstructor]
35    private SPSO2007ParticleUpdater(StorableConstructorFlag _) : base(_) { }
36    private SPSO2007ParticleUpdater(SPSO2007ParticleUpdater original, Cloner cloner) : base(original, cloner) { }
37    public SPSO2007ParticleUpdater() : base() { }
38    public override IDeepCloneable Clone(Cloner cloner) {
39      return new SPSO2007ParticleUpdater(this, cloner);
40    }
41    #endregion
42   
43    public static void UpdateVelocity(IRandom random, RealVector velocity, RealVector position, RealVector personalBest, RealVector neighborBest, double inertia = 0.721, double personalBestAttraction = 1.193, double neighborBestAttraction = 1.193, double maxVelocity = double.MaxValue) {
44      for (int i = 0; i < velocity.Length; i++) {
45        double r_p = random.NextDouble();
46        double r_g = random.NextDouble();
47        velocity[i] =
48          velocity[i] * inertia +
49          (personalBest[i] - position[i]) * personalBestAttraction * r_p +
50          (neighborBest[i] - position[i]) * neighborBestAttraction * r_g;
51      }
52
53      var speed = Math.Sqrt(velocity.DotProduct(velocity));
54      if (speed > maxVelocity) {
55        for (var i = 0; i < velocity.Length; i++) {
56          velocity[i] *= maxVelocity / speed;
57        }
58      }
59    }
60
61    public static void UpdatePosition(DoubleMatrix bounds, RealVector velocity, RealVector position) {
62      for (int i = 0; i < velocity.Length; i++) {
63        position[i] += velocity[i];
64      }
65
66      for (int i = 0; i < position.Length; i++) {
67        double min = bounds[i % bounds.Rows, 0];
68        double max = bounds[i % bounds.Rows, 1];
69        if (position[i] < min) {
70          position[i] = min;
71          velocity[i] = 0; // SPSO 2007
72        }
73        if (position[i] > max) {
74          position[i] = max;
75          velocity[i] = 0; // SPSO 2007
76        }
77      }
78    }
79
80    public override IOperation Apply() {
81      var random = RandomParameter.ActualValue;
82      var velocity = VelocityParameter.ActualValue;
83      var maxVelocity = CurrentMaxVelocityParameter.ActualValue.Value;
84      var position = RealVectorParameter.ActualValue;
85      var bounds = BoundsParameter.ActualValue;
86
87      var inertia = CurrentInertiaParameter.ActualValue.Value;
88      var personalBest = PersonalBestParameter.ActualValue;
89      var personalBestAttraction = PersonalBestAttractionParameter.ActualValue.Value;
90      var neighborBest = NeighborBestParameter.ActualValue;
91      var neighborBestAttraction = NeighborBestAttractionParameter.ActualValue.Value;
92
93      UpdateVelocity(random, velocity, position, personalBest, neighborBest, inertia, personalBestAttraction, neighborBestAttraction, maxVelocity);
94      UpdatePosition(bounds, velocity, position);
95
96      return base.Apply();
97    }
98  }
99}
Note: See TracBrowser for help on using the repository browser.