Free cookie consent management tool by TermsFeed Policy Generator

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

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

Add missing StorableClass attributes (#852)

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