Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5033 was 5033, checked in by epitzer, 13 years ago

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