[5560] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5560] | 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
|
---|
[15102] | 21 |
|
---|
| 22 | using System;
|
---|
[5560] | 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Operators;
|
---|
| 27 | using HeuristicLab.Parameters;
|
---|
[16565] | 28 | using HEAL.Attic;
|
---|
[15102] | 29 | using HeuristicLab.PluginInfrastructure;
|
---|
[5560] | 30 |
|
---|
| 31 | namespace HeuristicLab.Encodings.RealVectorEncoding {
|
---|
[5592] | 32 |
|
---|
[5560] | 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.")]
|
---|
[16565] | 34 | [StorableType("1396AC75-4875-4A0F-B6C9-640047078E6E")]
|
---|
[15102] | 35 | [NonDiscoverableType]
|
---|
| 36 | [Obsolete("Use SPSO2011ParticleUpdater")]
|
---|
| 37 | internal abstract class RealVectorParticleUpdater : SingleSuccessorOperator, IRealVectorParticleUpdater {
|
---|
[5592] | 38 |
|
---|
[5560] | 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 {
|
---|
[5566] | 51 | get { return (ILookupParameter<RealVector>)Parameters["PersonalBest"]; }
|
---|
[5560] | 52 | }
|
---|
[5568] | 53 | public ILookupParameter<RealVector> NeighborBestParameter {
|
---|
| 54 | get { return (ILookupParameter<RealVector>)Parameters["NeighborBest"]; }
|
---|
[5560] | 55 | }
|
---|
[15102] | 56 | public LookupParameter<RealVector> BestRealVectorParameter {
|
---|
| 57 | get { return (LookupParameter<RealVector>)Parameters["BestRealVector"]; }
|
---|
| 58 | }
|
---|
[5560] | 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 | }
|
---|
[15102] | 65 | public ILookupParameter<DoubleMatrix> CurrentVelocityBoundsParameter {
|
---|
| 66 | get { return (ILookupParameter<DoubleMatrix>)Parameters["CurrentVelocityBounds"]; }
|
---|
| 67 | }
|
---|
[15091] | 68 | public ILookupParameter<DoubleValue> CurrentMaxVelocityParameter {
|
---|
| 69 | get { return (ILookupParameter<DoubleValue>)Parameters["CurrentMaxVelocity"]; }
|
---|
[5560] | 70 | }
|
---|
[15102] | 71 | public ILookupParameter<DoubleValue> InertiaParameter {
|
---|
[5645] | 72 | get { return (ILookupParameter<DoubleValue>)Parameters["CurrentInertia"]; }
|
---|
[5560] | 73 | }
|
---|
| 74 | public ILookupParameter<DoubleValue> PersonalBestAttractionParameter {
|
---|
| 75 | get { return (ILookupParameter<DoubleValue>)Parameters["PersonalBestAttraction"]; }
|
---|
| 76 | }
|
---|
[5568] | 77 | public ILookupParameter<DoubleValue> NeighborBestAttractionParameter {
|
---|
| 78 | get { return (ILookupParameter<DoubleValue>)Parameters["NeighborBestAttraction"]; }
|
---|
[5560] | 79 | }
|
---|
| 80 | #endregion
|
---|
[15102] | 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 |
|
---|
[5560] | 120 | #region Construction & Cloning
|
---|
| 121 | [StorableConstructor]
|
---|
[16565] | 122 | protected RealVectorParticleUpdater(StorableConstructorFlag _) : base(_) { }
|
---|
[5560] | 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."));
|
---|
[15102] | 130 | Parameters.Add(new LookupParameter<RealVector>("BestRealVector", "Global best position."));
|
---|
[5568] | 131 | Parameters.Add(new LookupParameter<RealVector>("NeighborBest", "Best neighboring solution."));
|
---|
[5560] | 132 | Parameters.Add(new LookupParameter<DoubleMatrix>("Bounds", "The lower and upper bounds for each dimension of the position vector for the current problem."));
|
---|
[15102] | 133 | Parameters.Add(new LookupParameter<DoubleMatrix>("CurrentVelocityBounds", "Upper and lower bounds for the particle's velocity vector."));
|
---|
[15091] | 134 | Parameters.Add(new LookupParameter<DoubleValue>("CurrentMaxVelocity", "Maximum for the particle's velocity vector."));
|
---|
[5645] | 135 | Parameters.Add(new LookupParameter<DoubleValue>("CurrentInertia", "The weight for the particle's velocity vector."));
|
---|
[5560] | 136 | Parameters.Add(new LookupParameter<DoubleValue>("PersonalBestAttraction", "The weight for the particle's personal best position."));
|
---|
[5568] | 137 | Parameters.Add(new LookupParameter<DoubleValue>("NeighborBestAttraction", "The weight for the global best position."));
|
---|
[5560] | 138 | }
|
---|
| 139 | #endregion
|
---|
[15102] | 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 | }
|
---|
[5560] | 185 | }
|
---|
| 186 | }
|
---|