Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorParticleUpdater.cs @ 15091

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

#2797:

  • Updated PSO to make it more compatible with SPSO 2011
  • Removed truncation of velocity vector and instead rescaled it given the maximum velocity
  • Added non-zero initial velocity according to SPSO 2011
  • Removed complicated bouncing code due to box constraints and instead implemented as described in SPSO 2011
  • Calculating neighbor best has been changed to use personal best
  • Avoiding local and global particle update and instead relying on neighborbest
  • More randomization during velocity update by using a separate random numbers per dimension
  • Reusing problem specific solution creator in RealVectorParticleCreator instead of always using UniformRandomRealVectorCreator
File size: 6.5 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.Operators;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Encodings.RealVectorEncoding {
31
32  [Item("RealVectorParticleUpdater", "Updates a certain particle taking the current position and velocity into account, as well as the best point and the best point in a local neighborhood.")]
33  [StorableClass]
34  public abstract class RealVectorParticleUpdater : SingleSuccessorOperator, IRealVectorParticleUpdater {
35
36    public override bool CanChangeName {
37      get { return false; }
38    }
39
40    #region Parameter properties
41    public ILookupParameter<IRandom> RandomParameter {
42      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
43    }
44    public ILookupParameter<RealVector> VelocityParameter {
45      get { return (ILookupParameter<RealVector>)Parameters["Velocity"]; }
46    }
47    public ILookupParameter<RealVector> PersonalBestParameter {
48      get { return (ILookupParameter<RealVector>)Parameters["PersonalBest"]; }
49    }
50    public ILookupParameter<RealVector> NeighborBestParameter {
51      get { return (ILookupParameter<RealVector>)Parameters["NeighborBest"]; }
52    }
53    public ILookupParameter<RealVector> RealVectorParameter {
54      get { return (ILookupParameter<RealVector>)Parameters["RealVector"]; }
55    }
56    public ILookupParameter<DoubleMatrix> BoundsParameter {
57      get { return (ILookupParameter<DoubleMatrix>)Parameters["Bounds"]; }
58    }
59    public ILookupParameter<DoubleValue> CurrentMaxVelocityParameter {
60      get { return (ILookupParameter<DoubleValue>)Parameters["CurrentMaxVelocity"]; }
61    }
62    public ILookupParameter<DoubleValue> InertiaParameter {
63      get { return (ILookupParameter<DoubleValue>)Parameters["CurrentInertia"]; }
64    }
65    public ILookupParameter<DoubleValue> PersonalBestAttractionParameter {
66      get { return (ILookupParameter<DoubleValue>)Parameters["PersonalBestAttraction"]; }
67    }
68    public ILookupParameter<DoubleValue> NeighborBestAttractionParameter {
69      get { return (ILookupParameter<DoubleValue>)Parameters["NeighborBestAttraction"]; }
70    }
71    #endregion
72   
73    #region Construction & Cloning
74    [StorableConstructor]
75    protected RealVectorParticleUpdater(bool deserializing) : base(deserializing) { }
76    protected RealVectorParticleUpdater(RealVectorParticleUpdater original, Cloner cloner) : base(original, cloner) { }
77    public RealVectorParticleUpdater()
78      : base() {
79      Parameters.Add(new LookupParameter<IRandom>("Random", "Random number generator."));
80      Parameters.Add(new LookupParameter<RealVector>("RealVector", "Particle's current solution"));
81      Parameters.Add(new LookupParameter<RealVector>("Velocity", "Particle's current velocity."));
82      Parameters.Add(new LookupParameter<RealVector>("PersonalBest", "Particle's personal best solution."));
83      Parameters.Add(new LookupParameter<RealVector>("NeighborBest", "Best neighboring solution."));
84      Parameters.Add(new LookupParameter<DoubleMatrix>("Bounds", "The lower and upper bounds for each dimension of the position vector for the current problem."));
85      Parameters.Add(new LookupParameter<DoubleValue>("CurrentMaxVelocity", "Maximum for the particle's velocity vector."));
86      Parameters.Add(new LookupParameter<DoubleValue>("CurrentInertia", "The weight for the particle's velocity vector."));
87      Parameters.Add(new LookupParameter<DoubleValue>("PersonalBestAttraction", "The weight for the particle's personal best position."));
88      Parameters.Add(new LookupParameter<DoubleValue>("NeighborBestAttraction", "The weight for the global best position."));
89    }
90    #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    }
143  }
144}
Note: See TracBrowser for help on using the repository browser.