Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2922-DataCompletenessChartPerf/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorParticleUpdater.cs @ 18060

Last change on this file since 18060 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 8.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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;
29using HeuristicLab.PluginInfrastructure;
30
31namespace HeuristicLab.Encodings.RealVectorEncoding {
32
33  [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.")]
34  [StorableClass]
35  [NonDiscoverableType]
36  [Obsolete("Use SPSO2011ParticleUpdater")]
37  internal abstract class RealVectorParticleUpdater : SingleSuccessorOperator, IRealVectorParticleUpdater {
38
39    public override bool CanChangeName {
40      get { return false; }
41    }
42
43    #region Parameter properties
44    public ILookupParameter<IRandom> RandomParameter {
45      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
46    }
47    public ILookupParameter<RealVector> VelocityParameter {
48      get { return (ILookupParameter<RealVector>)Parameters["Velocity"]; }
49    }
50    public ILookupParameter<RealVector> PersonalBestParameter {
51      get { return (ILookupParameter<RealVector>)Parameters["PersonalBest"]; }
52    }
53    public ILookupParameter<RealVector> NeighborBestParameter {
54      get { return (ILookupParameter<RealVector>)Parameters["NeighborBest"]; }
55    }
56    public LookupParameter<RealVector> BestRealVectorParameter {
57      get { return (LookupParameter<RealVector>)Parameters["BestRealVector"]; }
58    }
59    public ILookupParameter<RealVector> RealVectorParameter {
60      get { return (ILookupParameter<RealVector>)Parameters["RealVector"]; }
61    }
62    public ILookupParameter<DoubleMatrix> BoundsParameter {
63      get { return (ILookupParameter<DoubleMatrix>)Parameters["Bounds"]; }
64    }
65    public ILookupParameter<DoubleMatrix> CurrentVelocityBoundsParameter {
66      get { return (ILookupParameter<DoubleMatrix>)Parameters["CurrentVelocityBounds"]; }
67    }
68    public ILookupParameter<DoubleValue> CurrentMaxVelocityParameter {
69      get { return (ILookupParameter<DoubleValue>)Parameters["CurrentMaxVelocity"]; }
70    }
71    public ILookupParameter<DoubleValue> InertiaParameter {
72      get { return (ILookupParameter<DoubleValue>)Parameters["CurrentInertia"]; }
73    }
74    public ILookupParameter<DoubleValue> PersonalBestAttractionParameter {
75      get { return (ILookupParameter<DoubleValue>)Parameters["PersonalBestAttraction"]; }
76    }
77    public ILookupParameter<DoubleValue> NeighborBestAttractionParameter {
78      get { return (ILookupParameter<DoubleValue>)Parameters["NeighborBestAttraction"]; }
79    }
80    #endregion
81
82    #region Parameter Values
83    protected IRandom Random {
84      get { return RandomParameter.ActualValue; }
85    }
86    protected RealVector Velocity {
87      get { return VelocityParameter.ActualValue; }
88      set { VelocityParameter.ActualValue = value; }
89    }
90    protected RealVector PersonalBest {
91      get { return PersonalBestParameter.ActualValue; }
92    }
93    protected RealVector BestPoint {
94      get { return BestRealVectorParameter.ActualValue; }
95    }
96    protected RealVector RealVector {
97      get { return RealVectorParameter.ActualValue; }
98      set { RealVectorParameter.ActualValue = value; }
99    }
100    protected RealVector NeighborBest {
101      get { return NeighborBestParameter.ActualValue; }
102    }
103    protected DoubleMatrix Bounds {
104      get { return BoundsParameter.ActualValue; }
105    }
106    protected DoubleMatrix CurrentVelocityBounds {
107      get { return CurrentVelocityBoundsParameter.ActualValue; }
108    }
109    protected DoubleValue Inertia {
110      get { return InertiaParameter.ActualValue; }
111    }
112    protected DoubleValue PersonalBestAttraction {
113      get { return PersonalBestAttractionParameter.ActualValue; }
114    }
115    protected DoubleValue NeighborBestAttraction {
116      get { return NeighborBestAttractionParameter.ActualValue; }
117    }
118    #endregion
119
120    #region Construction & Cloning
121    [StorableConstructor]
122    protected RealVectorParticleUpdater(bool deserializing) : base(deserializing) { }
123    protected RealVectorParticleUpdater(RealVectorParticleUpdater original, Cloner cloner) : base(original, cloner) { }
124    public RealVectorParticleUpdater()
125      : base() {
126      Parameters.Add(new LookupParameter<IRandom>("Random", "Random number generator."));
127      Parameters.Add(new LookupParameter<RealVector>("RealVector", "Particle's current solution"));
128      Parameters.Add(new LookupParameter<RealVector>("Velocity", "Particle's current velocity."));
129      Parameters.Add(new LookupParameter<RealVector>("PersonalBest", "Particle's personal best solution."));
130      Parameters.Add(new LookupParameter<RealVector>("BestRealVector", "Global best position."));
131      Parameters.Add(new LookupParameter<RealVector>("NeighborBest", "Best neighboring solution."));
132      Parameters.Add(new LookupParameter<DoubleMatrix>("Bounds", "The lower and upper bounds for each dimension of the position vector for the current problem."));
133      Parameters.Add(new LookupParameter<DoubleMatrix>("CurrentVelocityBounds", "Upper and lower bounds for the particle's velocity vector."));
134      Parameters.Add(new LookupParameter<DoubleValue>("CurrentMaxVelocity", "Maximum for the particle's velocity vector."));
135      Parameters.Add(new LookupParameter<DoubleValue>("CurrentInertia", "The weight for the particle's velocity vector."));
136      Parameters.Add(new LookupParameter<DoubleValue>("PersonalBestAttraction", "The weight for the particle's personal best position."));
137      Parameters.Add(new LookupParameter<DoubleValue>("NeighborBestAttraction", "The weight for the global best position."));
138    }
139    #endregion
140   
141    [StorableHook(HookType.AfterDeserialization)]
142    private void AfterDeserialization() {
143      if (!Parameters.ContainsKey("CurrentMaxVelocity"))
144        Parameters.Add(new LookupParameter<DoubleValue>("CurrentMaxVelocity", "Maximum for the particle's velocity vector."));
145    }
146
147    protected void MoveParticle(RealVector velocity, RealVector position) {
148      BoundsChecker.Apply(velocity, CurrentVelocityBounds);
149      for (int i = 0; i < velocity.Length; i++) {
150        position[i] = RealVector[i] + velocity[i];
151      }
152      for (int i = 0; i < position.Length; i++) {
153        double min = Bounds[i % Bounds.Rows, 0];
154        double max = Bounds[i % Bounds.Rows, 1];
155        if (position[i] < min) {
156          int reflectionCount = (int)Math.Truncate((min - position[i]) / (max - min)) + 1;
157          double reflection = (min - position[i]) % (max - min);
158          if (IsOdd(reflectionCount)) {
159            position[i] = min + reflection;
160            velocity[i] = -velocity[i];
161
162          } else {
163            position[i] = max - reflection;
164          }
165        }
166        if (position[i] > max) {
167          int reflectionCount = (int)Math.Truncate((position[i] - max) / (max - min)) + 1;
168          double reflection = (position[i] - max) % (max - min);
169          if (IsOdd(reflectionCount)) {
170            position[i] = max - reflection;
171            velocity[i] = -velocity[i];
172          } else {
173            position[i] = min + reflection;
174          }
175        }
176      }
177
178      RealVector = position;
179      Velocity = velocity;
180    }
181
182    private static bool IsOdd(int number) {
183      return number % 2 == 1;
184    }
185  }
186}
Note: See TracBrowser for help on using the repository browser.