Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PSO/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/BestPointInitializer.cs @ 5785

Last change on this file since 5785 was 5410, checked in by mkofler, 14 years ago

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

File size: 4.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 System.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.RealVectorEncoding;
27using HeuristicLab.Operators;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
32
33  [Item("Best Point Initializer", "Determines the best quality and point of the current particle population.")]
34  [StorableClass]
35  public class BestPointInitializer : SingleSuccessorOperator {
36
37    #region Parameter properties
38    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
39      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
40    }
41    public ILookupParameter<DoubleValue> BestQualityParameter {
42      get { return (ILookupParameter<DoubleValue>)Parameters["BestQuality"]; }
43    }
44    public IScopeTreeLookupParameter<RealVector> PointParameter {
45      get { return (IScopeTreeLookupParameter<RealVector>)Parameters["Point"]; }
46    }
47    public ILookupParameter<RealVector> BestPointParameter {
48      get { return (ILookupParameter<RealVector>)Parameters["BestPoint"]; }
49    }
50    public IValueLookupParameter<BoolValue> MaximizationParameter {
51      get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
52    }
53    #endregion
54
55    #region Parameter values
56    public ItemArray<DoubleValue> Quality {
57      get { return QualityParameter.ActualValue; }
58    }
59    public double BestQuality {
60      get { return BestQualityParameter.ActualValue.Value; }
61      set { BestQualityParameter.ActualValue = new DoubleValue(value); }
62    }
63    public ItemArray<RealVector> Point {
64      get { return PointParameter.ActualValue; }
65    }
66    public RealVector BestPoint {
67      get { return BestPointParameter.ActualValue; }
68      set { BestPointParameter.ActualValue = value; }
69    }
70    public bool Maximization {
71      get { return MaximizationParameter.ActualValue.Value; }
72    }
73    #endregion
74
75    #region Construction & Cloning
76
77    [StorableConstructor]
78    protected BestPointInitializer(bool deserializing) : base(deserializing) { }
79    protected BestPointInitializer(BestPointInitializer original, Cloner cloner)
80      : base(original, cloner) {
81    }
82
83    public BestPointInitializer()
84      : base() {
85      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "Particle's quality"));
86      Parameters.Add(new LookupParameter<DoubleValue>("BestQuality", "Global best particle quality"));
87      Parameters.Add(new ScopeTreeLookupParameter<RealVector>("Point", "Particle's position"));
88      Parameters.Add(new LookupParameter<RealVector>("BestPoint", "Globa best particle position"));
89      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
90    }
91
92    public override IDeepCloneable Clone(Cloner cloner) {
93      return new BestPointInitializer(this, cloner);
94    }
95
96    #endregion
97
98    public override IOperation Apply() {
99      BestQuality = Maximization ? Quality.Max(v => v.Value) : Quality.Min(v => v.Value);
100      int bestIndex = Quality.FindIndex(v => v.Value == BestQuality);
101      BestPoint = (RealVector)Point[bestIndex].Clone();
102      return base.Apply();
103    }
104
105    public override bool CanChangeName {
106      get { return false; }
107    }
108  }
109}
Note: See TracBrowser for help on using the repository browser.