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 |
|
---|
22 | using System.Linq;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
27 | using HeuristicLab.Operators;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
|
---|
32 | [Item("Best Point Initializer", "Determines the best quality and point of the current particle population.")]
|
---|
33 | [StorableClass]
|
---|
34 | public sealed class BestPointInitializer : SingleSuccessorOperator {
|
---|
35 | public override bool CanChangeName {
|
---|
36 | get { return false; }
|
---|
37 | }
|
---|
38 |
|
---|
39 | #region Parameter properties
|
---|
40 | public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
41 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
42 | }
|
---|
43 | public ILookupParameter<DoubleValue> BestQualityParameter {
|
---|
44 | get { return (ILookupParameter<DoubleValue>)Parameters["BestQuality"]; }
|
---|
45 | }
|
---|
46 | public IScopeTreeLookupParameter<RealVector> PointParameter {
|
---|
47 | get { return (IScopeTreeLookupParameter<RealVector>)Parameters["Point"]; }
|
---|
48 | }
|
---|
49 | public ILookupParameter<RealVector> BestPointParameter {
|
---|
50 | get { return (ILookupParameter<RealVector>)Parameters["BestPoint"]; }
|
---|
51 | }
|
---|
52 | public IValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
53 | get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
54 | }
|
---|
55 | #endregion
|
---|
56 |
|
---|
57 | #region Parameter values
|
---|
58 | private ItemArray<DoubleValue> Quality {
|
---|
59 | get { return QualityParameter.ActualValue; }
|
---|
60 | }
|
---|
61 | private double BestQuality {
|
---|
62 | get { return BestQualityParameter.ActualValue.Value; }
|
---|
63 | set { BestQualityParameter.ActualValue = new DoubleValue(value); }
|
---|
64 | }
|
---|
65 | private ItemArray<RealVector> Point {
|
---|
66 | get { return PointParameter.ActualValue; }
|
---|
67 | }
|
---|
68 | private RealVector BestPoint {
|
---|
69 | get { return BestPointParameter.ActualValue; }
|
---|
70 | set { BestPointParameter.ActualValue = value; }
|
---|
71 | }
|
---|
72 | private bool Maximization {
|
---|
73 | get { return MaximizationParameter.ActualValue.Value; }
|
---|
74 | }
|
---|
75 | #endregion
|
---|
76 |
|
---|
77 | #region Construction & Cloning
|
---|
78 |
|
---|
79 | [StorableConstructor]
|
---|
80 | private BestPointInitializer(bool deserializing) : base(deserializing) { }
|
---|
81 | private BestPointInitializer(BestPointInitializer original, Cloner cloner) : base(original, cloner) { }
|
---|
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 | }
|
---|