[5033] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5033] | 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;
|
---|
[5034] | 23 | using HeuristicLab.Common;
|
---|
[5033] | 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 {
|
---|
[5316] | 32 | [Item("Best Point Initializer", "Determines the best quality and point of the current particle population.")]
|
---|
[5034] | 33 | [StorableClass]
|
---|
[5435] | 34 | public sealed class BestPointInitializer : SingleSuccessorOperator {
|
---|
| 35 | public override bool CanChangeName {
|
---|
| 36 | get { return false; }
|
---|
| 37 | }
|
---|
[5033] | 38 |
|
---|
| 39 | #region Parameter properties
|
---|
[5410] | 40 | public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
| 41 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
[5033] | 42 | }
|
---|
[5410] | 43 | public ILookupParameter<DoubleValue> BestQualityParameter {
|
---|
| 44 | get { return (ILookupParameter<DoubleValue>)Parameters["BestQuality"]; }
|
---|
[5033] | 45 | }
|
---|
[5410] | 46 | public IScopeTreeLookupParameter<RealVector> PointParameter {
|
---|
| 47 | get { return (IScopeTreeLookupParameter<RealVector>)Parameters["Point"]; }
|
---|
[5033] | 48 | }
|
---|
[5410] | 49 | public ILookupParameter<RealVector> BestPointParameter {
|
---|
| 50 | get { return (ILookupParameter<RealVector>)Parameters["BestPoint"]; }
|
---|
[5033] | 51 | }
|
---|
[5410] | 52 | public IValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
| 53 | get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
[5033] | 54 | }
|
---|
| 55 | #endregion
|
---|
| 56 |
|
---|
| 57 | #region Parameter values
|
---|
[5435] | 58 | private ItemArray<DoubleValue> Quality {
|
---|
[5033] | 59 | get { return QualityParameter.ActualValue; }
|
---|
| 60 | }
|
---|
[5435] | 61 | private double BestQuality {
|
---|
[5033] | 62 | get { return BestQualityParameter.ActualValue.Value; }
|
---|
| 63 | set { BestQualityParameter.ActualValue = new DoubleValue(value); }
|
---|
| 64 | }
|
---|
[5435] | 65 | private ItemArray<RealVector> Point {
|
---|
[5033] | 66 | get { return PointParameter.ActualValue; }
|
---|
| 67 | }
|
---|
[5435] | 68 | private RealVector BestPoint {
|
---|
[5033] | 69 | get { return BestPointParameter.ActualValue; }
|
---|
| 70 | set { BestPointParameter.ActualValue = value; }
|
---|
| 71 | }
|
---|
[5435] | 72 | private bool Maximization {
|
---|
[5033] | 73 | get { return MaximizationParameter.ActualValue.Value; }
|
---|
| 74 | }
|
---|
| 75 | #endregion
|
---|
| 76 |
|
---|
| 77 | #region Construction & Cloning
|
---|
| 78 |
|
---|
| 79 | [StorableConstructor]
|
---|
[5435] | 80 | private BestPointInitializer(bool deserializing) : base(deserializing) { }
|
---|
| 81 | private BestPointInitializer(BestPointInitializer original, Cloner cloner) : base(original, cloner) { }
|
---|
[5033] | 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 | }
|
---|