[13368] | 1 | #region License Information
|
---|
[5560] | 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 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
|
---|
| 21 |
|
---|
[5892] | 22 | using System;
|
---|
[5560] | 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Operators;
|
---|
| 27 | using HeuristicLab.Parameters;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Encodings.RealVectorEncoding {
|
---|
[5592] | 31 |
|
---|
[5560] | 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.")]
|
---|
[14711] | 33 | [StorableType("3D25AEC9-BBA1-429D-AE17-5FAD8ABA1E19")]
|
---|
[5560] | 34 | public abstract class RealVectorParticleUpdater : SingleSuccessorOperator, IRealVectorParticleUpdater {
|
---|
[5592] | 35 |
|
---|
[5560] | 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 {
|
---|
[5566] | 48 | get { return (ILookupParameter<RealVector>)Parameters["PersonalBest"]; }
|
---|
[5560] | 49 | }
|
---|
[5568] | 50 | public ILookupParameter<RealVector> NeighborBestParameter {
|
---|
| 51 | get { return (ILookupParameter<RealVector>)Parameters["NeighborBest"]; }
|
---|
[5560] | 52 | }
|
---|
[5643] | 53 | public LookupParameter<RealVector> BestRealVectorParameter {
|
---|
| 54 | get { return (LookupParameter<RealVector>)Parameters["BestRealVector"]; }
|
---|
[5568] | 55 | }
|
---|
[5560] | 56 | public ILookupParameter<RealVector> RealVectorParameter {
|
---|
| 57 | get { return (ILookupParameter<RealVector>)Parameters["RealVector"]; }
|
---|
| 58 | }
|
---|
| 59 | public ILookupParameter<DoubleMatrix> BoundsParameter {
|
---|
| 60 | get { return (ILookupParameter<DoubleMatrix>)Parameters["Bounds"]; }
|
---|
| 61 | }
|
---|
[5866] | 62 | public ILookupParameter<DoubleMatrix> CurrentVelocityBoundsParameter {
|
---|
| 63 | get { return (ILookupParameter<DoubleMatrix>)Parameters["CurrentVelocityBounds"]; }
|
---|
[5560] | 64 | }
|
---|
| 65 | public ILookupParameter<DoubleValue> InertiaParameter {
|
---|
[5645] | 66 | get { return (ILookupParameter<DoubleValue>)Parameters["CurrentInertia"]; }
|
---|
[5560] | 67 | }
|
---|
| 68 | public ILookupParameter<DoubleValue> PersonalBestAttractionParameter {
|
---|
| 69 | get { return (ILookupParameter<DoubleValue>)Parameters["PersonalBestAttraction"]; }
|
---|
| 70 | }
|
---|
[5568] | 71 | public ILookupParameter<DoubleValue> NeighborBestAttractionParameter {
|
---|
| 72 | get { return (ILookupParameter<DoubleValue>)Parameters["NeighborBestAttraction"]; }
|
---|
[5560] | 73 | }
|
---|
| 74 | #endregion
|
---|
| 75 |
|
---|
| 76 | #region Parameter Values
|
---|
| 77 | protected IRandom Random {
|
---|
| 78 | get { return RandomParameter.ActualValue; }
|
---|
| 79 | }
|
---|
| 80 | protected RealVector Velocity {
|
---|
| 81 | get { return VelocityParameter.ActualValue; }
|
---|
| 82 | set { VelocityParameter.ActualValue = value; }
|
---|
| 83 | }
|
---|
| 84 | protected RealVector PersonalBest {
|
---|
| 85 | get { return PersonalBestParameter.ActualValue; }
|
---|
| 86 | }
|
---|
[5568] | 87 | protected RealVector BestPoint {
|
---|
[5643] | 88 | get { return BestRealVectorParameter.ActualValue; }
|
---|
[5568] | 89 | }
|
---|
[5560] | 90 | protected RealVector RealVector {
|
---|
| 91 | get { return RealVectorParameter.ActualValue; }
|
---|
| 92 | set { RealVectorParameter.ActualValue = value; }
|
---|
| 93 | }
|
---|
[5568] | 94 | protected RealVector NeighborBest {
|
---|
| 95 | get { return NeighborBestParameter.ActualValue; }
|
---|
[5560] | 96 | }
|
---|
| 97 | protected DoubleMatrix Bounds {
|
---|
| 98 | get { return BoundsParameter.ActualValue; }
|
---|
| 99 | }
|
---|
[5866] | 100 | protected DoubleMatrix CurrentVelocityBounds {
|
---|
| 101 | get { return CurrentVelocityBoundsParameter.ActualValue; }
|
---|
[5560] | 102 | }
|
---|
| 103 | protected DoubleValue Inertia {
|
---|
| 104 | get { return InertiaParameter.ActualValue; }
|
---|
| 105 | }
|
---|
| 106 | protected DoubleValue PersonalBestAttraction {
|
---|
| 107 | get { return PersonalBestAttractionParameter.ActualValue; }
|
---|
| 108 | }
|
---|
[5568] | 109 | protected DoubleValue NeighborBestAttraction {
|
---|
| 110 | get { return NeighborBestAttractionParameter.ActualValue; }
|
---|
[5560] | 111 | }
|
---|
| 112 | #endregion
|
---|
| 113 |
|
---|
| 114 | #region Construction & Cloning
|
---|
| 115 | [StorableConstructor]
|
---|
| 116 | protected RealVectorParticleUpdater(bool deserializing) : base(deserializing) { }
|
---|
| 117 | protected RealVectorParticleUpdater(RealVectorParticleUpdater original, Cloner cloner) : base(original, cloner) { }
|
---|
| 118 | public RealVectorParticleUpdater()
|
---|
| 119 | : base() {
|
---|
| 120 | Parameters.Add(new LookupParameter<IRandom>("Random", "Random number generator."));
|
---|
| 121 | Parameters.Add(new LookupParameter<RealVector>("RealVector", "Particle's current solution"));
|
---|
| 122 | Parameters.Add(new LookupParameter<RealVector>("Velocity", "Particle's current velocity."));
|
---|
| 123 | Parameters.Add(new LookupParameter<RealVector>("PersonalBest", "Particle's personal best solution."));
|
---|
[5643] | 124 | Parameters.Add(new LookupParameter<RealVector>("BestRealVector", "Global best position."));
|
---|
[5568] | 125 | Parameters.Add(new LookupParameter<RealVector>("NeighborBest", "Best neighboring solution."));
|
---|
[5560] | 126 | Parameters.Add(new LookupParameter<DoubleMatrix>("Bounds", "The lower and upper bounds for each dimension of the position vector for the current problem."));
|
---|
[5866] | 127 | Parameters.Add(new LookupParameter<DoubleMatrix>("CurrentVelocityBounds", "Upper and lower bounds for the particle's velocity vector."));
|
---|
[5645] | 128 | Parameters.Add(new LookupParameter<DoubleValue>("CurrentInertia", "The weight for the particle's velocity vector."));
|
---|
[5560] | 129 | Parameters.Add(new LookupParameter<DoubleValue>("PersonalBestAttraction", "The weight for the particle's personal best position."));
|
---|
[5568] | 130 | Parameters.Add(new LookupParameter<DoubleValue>("NeighborBestAttraction", "The weight for the global best position."));
|
---|
[5560] | 131 | }
|
---|
| 132 | #endregion
|
---|
[5892] | 133 |
|
---|
| 134 | protected void MoveParticle(RealVector velocity, RealVector position) {
|
---|
| 135 | BoundsChecker.Apply(velocity, CurrentVelocityBounds);
|
---|
| 136 | for (int i = 0; i < velocity.Length; i++) {
|
---|
| 137 | position[i] = RealVector[i] + velocity[i];
|
---|
| 138 | }
|
---|
| 139 | for (int i = 0; i < position.Length; i++) {
|
---|
| 140 | double min = Bounds[i % Bounds.Rows, 0];
|
---|
| 141 | double max = Bounds[i % Bounds.Rows, 1];
|
---|
| 142 | if (position[i] < min) {
|
---|
| 143 | int reflectionCount = (int)Math.Truncate((min - position[i]) / (max - min)) + 1;
|
---|
| 144 | double reflection = (min - position[i]) % (max - min);
|
---|
| 145 | if (IsOdd(reflectionCount)) {
|
---|
| 146 | position[i] = min + reflection;
|
---|
| 147 | velocity[i] = -velocity[i];
|
---|
| 148 |
|
---|
| 149 | } else {
|
---|
| 150 | position[i] = max - reflection;
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 | if (position[i] > max) {
|
---|
| 154 | int reflectionCount = (int)Math.Truncate((position[i] - max) / (max - min)) + 1;
|
---|
| 155 | double reflection = (position[i] - max) % (max - min);
|
---|
| 156 | if (IsOdd(reflectionCount)) {
|
---|
| 157 | position[i] = max - reflection;
|
---|
| 158 | velocity[i] = -velocity[i];
|
---|
| 159 | } else {
|
---|
| 160 | position[i] = min + reflection;
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | RealVector = position;
|
---|
| 166 | Velocity = velocity;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | private static bool IsOdd(int number) {
|
---|
| 170 | return number % 2 == 1;
|
---|
| 171 | }
|
---|
[5560] | 172 | }
|
---|
| 173 | }
|
---|