Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PSO/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/SwarmUpdater.cs @ 5209

Last change on this file since 5209 was 5034, checked in by epitzer, 14 years ago

Add missing StorableClass attributes (#852)

File size: 5.1 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  [StorableClass]
33  public class SwarmUpdater : SingleSuccessorOperator {
34
35    #region Parameter properties
36    public LookupParameter<DoubleValue> QualityParameter {
37      get { return (LookupParameter<DoubleValue>)Parameters["Quality"]; }
38    }
39    public LookupParameter<DoubleValue> PersonalBestQualityParameter {
40      get { return (LookupParameter<DoubleValue>)Parameters["PersonalBestQuality"]; }
41    }
42    public LookupParameter<DoubleValue> BestQualityParameter {
43      get { return (LookupParameter<DoubleValue>)Parameters["BestQuality"]; }
44    }
45    public LookupParameter<RealVector> PointParameter {
46      get { return (LookupParameter<RealVector>)Parameters["Point"]; }
47    }
48    public LookupParameter<RealVector> PersonalBestPointParameter {
49      get { return (LookupParameter<RealVector>)Parameters["PersonalBestPoint"]; }
50    }
51    public LookupParameter<RealVector> BestPointParameter {
52      get { return (LookupParameter<RealVector>)Parameters["BestPoint"]; }
53    }
54    public ValueLookupParameter<BoolValue> MaximizationParameter {
55      get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
56    }
57    #endregion
58
59    #region Parameter values
60    public double Quality {
61      get { return QualityParameter.ActualValue.Value; }
62    }
63    public double PersonalBestQuality {
64      get { return PersonalBestQualityParameter.ActualValue.Value; }
65      set { PersonalBestQualityParameter.ActualValue = new DoubleValue(value); }
66    }
67    public double BestQuality {
68      get { return BestQualityParameter.ActualValue.Value; }
69      set { BestQualityParameter.ActualValue = new DoubleValue(value); }
70    }
71    public RealVector Point {
72      get { return PointParameter.ActualValue; }
73    }
74    public RealVector PersonalBestPoint {
75      get { return PersonalBestPointParameter.ActualValue; }
76      set { PersonalBestPointParameter.ActualValue = value; }
77    }
78    public RealVector BestPoint {
79      get { return BestPointParameter.ActualValue; }
80      set { BestPointParameter.ActualValue = value; }
81    }
82    public bool Maximization {
83      get { return MaximizationParameter.ActualValue.Value; }
84    }
85    #endregion
86
87    #region Construction & Cloning
88
89    [StorableConstructor]
90    protected SwarmUpdater(bool deserializing) : base(deserializing) { }
91    protected SwarmUpdater(SwarmUpdater original, Cloner cloner)
92      : base(original, cloner) {
93    }
94
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
114      if (Maximization && Quality > PersonalBestQuality ||
115         !Maximization && Quality < PersonalBestQuality) {
116        PersonalBestQuality = Quality;
117        PersonalBestPoint = Point;
118        if (Maximization && PersonalBestQuality > BestQuality ||
119           !Maximization && PersonalBestQuality < BestQuality) {
120          BestQuality = PersonalBestQuality;
121          BestPoint = PersonalBestPoint;
122        }
123      }
124      return base.Apply();
125    }
126
127    public override bool CanChangeName {
128      get { return false; }
129    }
130  }
131}
Note: See TracBrowser for help on using the repository browser.