[5560] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14185] | 3 | * Copyright (C) 2002-2016 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 |
|
---|
[15096] | 22 | using System;
|
---|
[5560] | 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[15096] | 26 | using HeuristicLab.PluginInfrastructure;
|
---|
[5560] | 27 |
|
---|
| 28 | namespace HeuristicLab.Encodings.RealVectorEncoding {
|
---|
| 29 | [Item("Neighborhood Particle Updater", "Updates the particle's position using (among other things) the best neighbor's position. Point = Point + Velocity*Inertia + (PersonalBestPoint-Point)*Phi_P*r_p + (BestNeighborPoint-Point)*Phi_G*r_g.")]
|
---|
| 30 | [StorableClass]
|
---|
[15096] | 31 | [NonDiscoverableType]
|
---|
| 32 | [Obsolete("Replaced by SPSO2007ParticleUpdater")]
|
---|
| 33 | internal sealed class RealVectorNeighborhoodParticleUpdater : RealVectorParticleUpdater {
|
---|
[5560] | 34 |
|
---|
[5592] | 35 | #region Construction & Cloning
|
---|
[5560] | 36 | [StorableConstructor]
|
---|
| 37 | private RealVectorNeighborhoodParticleUpdater(bool deserializing) : base(deserializing) { }
|
---|
| 38 | private RealVectorNeighborhoodParticleUpdater(RealVectorNeighborhoodParticleUpdater original, Cloner cloner) : base(original, cloner) { }
|
---|
| 39 | public RealVectorNeighborhoodParticleUpdater() : base() { }
|
---|
| 40 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 41 | return new RealVectorNeighborhoodParticleUpdater(this, cloner);
|
---|
| 42 | }
|
---|
[5592] | 43 | #endregion
|
---|
[5560] | 44 |
|
---|
[15096] | 45 | private void UpdateVelocity() {
|
---|
| 46 | var velocity = VelocityParameter.ActualValue;
|
---|
| 47 | var position = RealVectorParameter.ActualValue;
|
---|
| 48 | var inertia = CurrentInertiaParameter.ActualValue.Value;
|
---|
| 49 | var personalBest = PersonalBestParameter.ActualValue;
|
---|
| 50 | var personalBestAttraction = PersonalBestAttractionParameter.ActualValue.Value;
|
---|
| 51 | var neighborBest = NeighborBestParameter.ActualValue;
|
---|
| 52 | var neighborBestAttraction = NeighborBestAttractionParameter.ActualValue.Value;
|
---|
| 53 |
|
---|
| 54 | var random = RandomParameter.ActualValue;
|
---|
| 55 |
|
---|
| 56 | for (int i = 0; i < velocity.Length; i++) {
|
---|
| 57 | double r_p = random.NextDouble();
|
---|
| 58 | double r_g = random.NextDouble();
|
---|
| 59 | velocity[i] =
|
---|
| 60 | velocity[i] * inertia +
|
---|
| 61 | (personalBest[i] - position[i]) * personalBestAttraction * r_p +
|
---|
| 62 | (neighborBest[i] - position[i]) * neighborBestAttraction * r_g;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | var maxVelocity = CurrentMaxVelocityParameter.ActualValue.Value;
|
---|
| 66 | var speed = Math.Sqrt(velocity.DotProduct(velocity));
|
---|
| 67 | if (speed > maxVelocity) {
|
---|
| 68 | for (var i = 0; i < velocity.Length; i++) {
|
---|
| 69 | velocity[i] *= maxVelocity / speed;
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | private void UpdatePosition() {
|
---|
| 75 | var velocity = VelocityParameter.ActualValue;
|
---|
| 76 | var position = RealVectorParameter.ActualValue;
|
---|
| 77 |
|
---|
| 78 | for (int i = 0; i < velocity.Length; i++) {
|
---|
| 79 | position[i] += velocity[i];
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | var bounds = BoundsParameter.ActualValue;
|
---|
| 83 | for (int i = 0; i < position.Length; i++) {
|
---|
| 84 | double min = bounds[i % bounds.Rows, 0];
|
---|
| 85 | double max = bounds[i % bounds.Rows, 1];
|
---|
| 86 | if (position[i] < min) {
|
---|
| 87 | position[i] = min;
|
---|
| 88 | velocity[i] = -0.5 * velocity[i]; // SPSO 2011
|
---|
| 89 | }
|
---|
| 90 | if (position[i] > max) {
|
---|
| 91 | position[i] = max;
|
---|
| 92 | velocity[i] = -0.5 * velocity[i]; // SPSO 2011
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[5560] | 97 | public override IOperation Apply() {
|
---|
[15091] | 98 | UpdateVelocity();
|
---|
| 99 | UpdatePosition();
|
---|
[5592] | 100 |
|
---|
[5560] | 101 | return base.Apply();
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 | }
|
---|