Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5477 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 5.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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  [Item("Swarm Updater", "Updates personal best point and quality as well as global best point and quality.")]
32  [StorableClass]
33  public sealed class SwarmUpdater : SingleSuccessorOperator {
34    public override bool CanChangeName {
35      get { return false; }
36    }
37
38    #region Parameter properties
39    public ILookupParameter<DoubleValue> QualityParameter {
40      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
41    }
42    public ILookupParameter<DoubleValue> PersonalBestQualityParameter {
43      get { return (ILookupParameter<DoubleValue>)Parameters["PersonalBestQuality"]; }
44    }
45    public ILookupParameter<DoubleValue> BestQualityParameter {
46      get { return (ILookupParameter<DoubleValue>)Parameters["BestQuality"]; }
47    }
48    public ILookupParameter<RealVector> PointParameter {
49      get { return (ILookupParameter<RealVector>)Parameters["Point"]; }
50    }
51    public ILookupParameter<RealVector> PersonalBestPointParameter {
52      get { return (ILookupParameter<RealVector>)Parameters["PersonalBestPoint"]; }
53    }
54    public ILookupParameter<RealVector> BestPointParameter {
55      get { return (ILookupParameter<RealVector>)Parameters["BestPoint"]; }
56    }
57    public IValueLookupParameter<BoolValue> MaximizationParameter {
58      get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
59    }
60    #endregion
61
62    #region Parameter values
63    private double Quality {
64      get { return QualityParameter.ActualValue.Value; }
65    }
66    private double PersonalBestQuality {
67      get { return PersonalBestQualityParameter.ActualValue.Value; }
68      set { PersonalBestQualityParameter.ActualValue = new DoubleValue(value); }
69    }
70    private double BestQuality {
71      get { return BestQualityParameter.ActualValue.Value; }
72      set { BestQualityParameter.ActualValue = new DoubleValue(value); }
73    }
74    private RealVector Point {
75      get { return PointParameter.ActualValue; }
76    }
77    private RealVector PersonalBestPoint {
78      get { return PersonalBestPointParameter.ActualValue; }
79      set { PersonalBestPointParameter.ActualValue = value; }
80    }
81    private RealVector BestPoint {
82      get { return BestPointParameter.ActualValue; }
83      set { BestPointParameter.ActualValue = value; }
84    }
85    private bool Maximization {
86      get { return MaximizationParameter.ActualValue.Value; }
87    }
88    #endregion
89
90    #region Construction & Cloning
91
92    [StorableConstructor]
93    private SwarmUpdater(bool deserializing) : base(deserializing) { }
94    private SwarmUpdater(SwarmUpdater original, Cloner cloner) : base(original, cloner) { }
95    public SwarmUpdater()
96      : base() {
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"));
103      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
104    }
105
106    public override IDeepCloneable Clone(Cloner cloner) {
107      return new SwarmUpdater(this, cloner);
108    }
109
110    #endregion
111
112    public override IOperation Apply() {
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;
121        }
122      }
123      return base.Apply();
124    }
125  }
126}
Note: See TracBrowser for help on using the repository browser.