Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/SwarmUpdater.cs @ 5426

Last change on this file since 5426 was 5410, checked in by mkofler, 13 years ago

#852: Realized code cleanup as described in the reviewer comments by Andreas Beham.

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