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 |
|
---|
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 ScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
39 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
40 | }
|
---|
41 | public LookupParameter<DoubleValue> BestQualityParameter {
|
---|
42 | get { return (LookupParameter<DoubleValue>)Parameters["BestQuality"]; }
|
---|
43 | }
|
---|
44 | public ScopeTreeLookupParameter<RealVector> PointParameter {
|
---|
45 | get { return (ScopeTreeLookupParameter<RealVector>)Parameters["Point"]; }
|
---|
46 | }
|
---|
47 | public LookupParameter<RealVector> BestPointParameter {
|
---|
48 | get { return (LookupParameter<RealVector>)Parameters["BestPoint"]; }
|
---|
49 | }
|
---|
50 | public ValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
51 | get { return (ValueLookupParameter<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 | }
|
---|