[3742] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 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 |
|
---|
[5034] | 22 | using HeuristicLab.Common;
|
---|
[3682] | 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Data;
|
---|
| 25 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
[4068] | 26 | using HeuristicLab.Operators;
|
---|
[3682] | 27 | using HeuristicLab.Parameters;
|
---|
[5033] | 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[3682] | 29 |
|
---|
| 30 | namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
|
---|
[5316] | 31 | [Item("Swarm Updater", "Updates personal best point and quality as well as global best point and quality.")]
|
---|
[5034] | 32 | [StorableClass]
|
---|
[5435] | 33 | public sealed class SwarmUpdater : SingleSuccessorOperator {
|
---|
| 34 | public override bool CanChangeName {
|
---|
| 35 | get { return false; }
|
---|
| 36 | }
|
---|
[5033] | 37 |
|
---|
[3682] | 38 | #region Parameter properties
|
---|
[5410] | 39 | public ILookupParameter<DoubleValue> QualityParameter {
|
---|
| 40 | get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
[3682] | 41 | }
|
---|
[5410] | 42 | public ILookupParameter<DoubleValue> PersonalBestQualityParameter {
|
---|
| 43 | get { return (ILookupParameter<DoubleValue>)Parameters["PersonalBestQuality"]; }
|
---|
[3682] | 44 | }
|
---|
[5410] | 45 | public ILookupParameter<DoubleValue> BestQualityParameter {
|
---|
| 46 | get { return (ILookupParameter<DoubleValue>)Parameters["BestQuality"]; }
|
---|
[3682] | 47 | }
|
---|
[5410] | 48 | public ILookupParameter<RealVector> PointParameter {
|
---|
| 49 | get { return (ILookupParameter<RealVector>)Parameters["Point"]; }
|
---|
[3682] | 50 | }
|
---|
[5410] | 51 | public ILookupParameter<RealVector> PersonalBestPointParameter {
|
---|
| 52 | get { return (ILookupParameter<RealVector>)Parameters["PersonalBestPoint"]; }
|
---|
[3682] | 53 | }
|
---|
[5410] | 54 | public ILookupParameter<RealVector> BestPointParameter {
|
---|
| 55 | get { return (ILookupParameter<RealVector>)Parameters["BestPoint"]; }
|
---|
[3682] | 56 | }
|
---|
[5410] | 57 | public IValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
| 58 | get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
[3682] | 59 | }
|
---|
| 60 | #endregion
|
---|
| 61 |
|
---|
[5033] | 62 | #region Parameter values
|
---|
[5435] | 63 | private double Quality {
|
---|
[5033] | 64 | get { return QualityParameter.ActualValue.Value; }
|
---|
| 65 | }
|
---|
[5435] | 66 | private double PersonalBestQuality {
|
---|
[5034] | 67 | get { return PersonalBestQualityParameter.ActualValue.Value; }
|
---|
| 68 | set { PersonalBestQualityParameter.ActualValue = new DoubleValue(value); }
|
---|
[5033] | 69 | }
|
---|
[5435] | 70 | private double BestQuality {
|
---|
[5033] | 71 | get { return BestQualityParameter.ActualValue.Value; }
|
---|
| 72 | set { BestQualityParameter.ActualValue = new DoubleValue(value); }
|
---|
| 73 | }
|
---|
[5435] | 74 | private RealVector Point {
|
---|
[5033] | 75 | get { return PointParameter.ActualValue; }
|
---|
| 76 | }
|
---|
[5435] | 77 | private RealVector PersonalBestPoint {
|
---|
[5033] | 78 | get { return PersonalBestPointParameter.ActualValue; }
|
---|
| 79 | set { PersonalBestPointParameter.ActualValue = value; }
|
---|
| 80 | }
|
---|
[5435] | 81 | private RealVector BestPoint {
|
---|
[5033] | 82 | get { return BestPointParameter.ActualValue; }
|
---|
| 83 | set { BestPointParameter.ActualValue = value; }
|
---|
| 84 | }
|
---|
[5435] | 85 | private bool Maximization {
|
---|
[5033] | 86 | get { return MaximizationParameter.ActualValue.Value; }
|
---|
| 87 | }
|
---|
| 88 | #endregion
|
---|
| 89 |
|
---|
| 90 | #region Construction & Cloning
|
---|
| 91 |
|
---|
| 92 | [StorableConstructor]
|
---|
[5435] | 93 | private SwarmUpdater(bool deserializing) : base(deserializing) { }
|
---|
| 94 | private SwarmUpdater(SwarmUpdater original, Cloner cloner) : base(original, cloner) { }
|
---|
[3682] | 95 | public SwarmUpdater()
|
---|
| 96 | : base() {
|
---|
[5033] | 97 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "Particle's quality"));
|
---|
| 98 | Parameters.Add(new LookupParameter<DoubleValue>("PersonalBestQuality", "Particle's personal best quality"));
|
---|
| 99 | Parameters.Add(new LookupParameter<DoubleValue>("BestQuality", "Global best particle quality"));
|
---|
| 100 | Parameters.Add(new LookupParameter<RealVector>("Point", "Particle's position"));
|
---|
| 101 | Parameters.Add(new LookupParameter<RealVector>("PersonalBestPoint", "Particle's personal best position"));
|
---|
| 102 | Parameters.Add(new LookupParameter<RealVector>("BestPoint", "Globa best particle position"));
|
---|
[3682] | 103 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[5033] | 106 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 107 | return new SwarmUpdater(this, cloner);
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | #endregion
|
---|
| 111 |
|
---|
[3682] | 112 | public override IOperation Apply() {
|
---|
[5033] | 113 | if (Maximization && Quality > PersonalBestQuality ||
|
---|
| 114 | !Maximization && Quality < PersonalBestQuality) {
|
---|
| 115 | PersonalBestQuality = Quality;
|
---|
| 116 | PersonalBestPoint = Point;
|
---|
| 117 | if (Maximization && PersonalBestQuality > BestQuality ||
|
---|
| 118 | !Maximization && PersonalBestQuality < BestQuality) {
|
---|
| 119 | BestQuality = PersonalBestQuality;
|
---|
| 120 | BestPoint = PersonalBestPoint;
|
---|
[3682] | 121 | }
|
---|
| 122 | }
|
---|
| 123 | return base.Apply();
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 | }
|
---|