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 |
|
---|
22 | using System;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Parameters;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Encodings.RealVectorEncoding {
|
---|
30 | [Item("SPSO 2007 Particle Updater", "Updates the particle's position according to the formulae described in SPSO 2007.")]
|
---|
31 | [StorableClass]
|
---|
32 | public sealed class SPSO2007ParticleUpdater : SPSOParticleUpdater {
|
---|
33 |
|
---|
34 | #region Construction & Cloning
|
---|
35 | [StorableConstructor]
|
---|
36 | private SPSO2007ParticleUpdater(bool deserializing) : base(deserializing) { }
|
---|
37 | private SPSO2007ParticleUpdater(SPSO2007ParticleUpdater original, Cloner cloner) : base(original, cloner) { }
|
---|
38 | public SPSO2007ParticleUpdater() : base() { }
|
---|
39 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
40 | return new SPSO2007ParticleUpdater(this, cloner);
|
---|
41 | }
|
---|
42 | #endregion
|
---|
43 |
|
---|
44 | 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) {
|
---|
45 | for (int i = 0; i < velocity.Length; i++) {
|
---|
46 | double r_p = random.NextDouble();
|
---|
47 | double r_g = random.NextDouble();
|
---|
48 | velocity[i] =
|
---|
49 | velocity[i] * inertia +
|
---|
50 | (personalBest[i] - position[i]) * personalBestAttraction * r_p +
|
---|
51 | (neighborBest[i] - position[i]) * neighborBestAttraction * r_g;
|
---|
52 | }
|
---|
53 |
|
---|
54 | var speed = Math.Sqrt(velocity.DotProduct(velocity));
|
---|
55 | if (speed > maxVelocity) {
|
---|
56 | for (var i = 0; i < velocity.Length; i++) {
|
---|
57 | velocity[i] *= maxVelocity / speed;
|
---|
58 | }
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | public static void UpdatePosition(DoubleMatrix bounds, RealVector velocity, RealVector position) {
|
---|
63 | for (int i = 0; i < velocity.Length; i++) {
|
---|
64 | position[i] += velocity[i];
|
---|
65 | }
|
---|
66 |
|
---|
67 | for (int i = 0; i < position.Length; i++) {
|
---|
68 | double min = bounds[i % bounds.Rows, 0];
|
---|
69 | double max = bounds[i % bounds.Rows, 1];
|
---|
70 | if (position[i] < min) {
|
---|
71 | position[i] = min;
|
---|
72 | velocity[i] = 0; // SPSO 2007
|
---|
73 | }
|
---|
74 | if (position[i] > max) {
|
---|
75 | position[i] = max;
|
---|
76 | velocity[i] = 0; // SPSO 2007
|
---|
77 | }
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | public override IOperation Apply() {
|
---|
82 | var random = RandomParameter.ActualValue;
|
---|
83 | var velocity = VelocityParameter.ActualValue;
|
---|
84 | var maxVelocity = CurrentMaxVelocityParameter.ActualValue.Value;
|
---|
85 | var position = RealVectorParameter.ActualValue;
|
---|
86 | var bounds = BoundsParameter.ActualValue;
|
---|
87 |
|
---|
88 | var inertia = CurrentInertiaParameter.ActualValue.Value;
|
---|
89 | var personalBest = PersonalBestParameter.ActualValue;
|
---|
90 | var personalBestAttraction = PersonalBestAttractionParameter.ActualValue.Value;
|
---|
91 | var neighborBest = NeighborBestParameter.ActualValue;
|
---|
92 | var neighborBestAttraction = NeighborBestAttractionParameter.ActualValue.Value;
|
---|
93 |
|
---|
94 | UpdateVelocity(random, velocity, position, personalBest, neighborBest, inertia, personalBestAttraction, neighborBestAttraction, maxVelocity);
|
---|
95 | UpdatePosition(bounds, velocity, position);
|
---|
96 |
|
---|
97 | return base.Apply();
|
---|
98 | }
|
---|
99 | }
|
---|
100 | }
|
---|