Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2870_AutoDiff-nuget/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/AdaptiveRandomTopologyUpdater.cs @ 16008

Last change on this file since 16008 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
33  [Item("SPSO Adaptive Random Topology Updater", "Each unsuccessful iteration the topology initializer is applied again.")]
34  [StorableClass]
35  public sealed class SPSOAdaptiveRandomTopologyUpdater : SingleSuccessorOperator, ITopologyUpdater, ISingleObjectiveOperator {
36
37    public override bool CanChangeName {
38      get { return false; }
39    }
40
41    #region Parameters
42    public ILookupParameter<BoolValue> MaximizationParameter {
43      get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
44    }
45    public ILookupParameter<DoubleValue> SwarmBestQualityParameter {
46      get { return (ILookupParameter<DoubleValue>)Parameters["SwarmBestQuality"]; }
47    }
48    public ILookupParameter<DoubleValue> PreviousBestQualityParameter {
49      get { return (ILookupParameter<DoubleValue>)Parameters["PreviousBestQuality"]; }
50    }
51    public ILookupParameter<ITopologyInitializer> TopologyInitializerParameter {
52      get { return (ILookupParameter<ITopologyInitializer>)Parameters["TopologyInitializer"]; }
53    }
54    public IScopeTreeLookupParameter<IntArray> NeighborsParameter {
55      get { return (IScopeTreeLookupParameter<IntArray>)Parameters["Neighbors"]; }
56    }
57    #endregion
58
59    #region Construction & Cloning
60    [StorableConstructor]
61    private SPSOAdaptiveRandomTopologyUpdater(bool deserializing) : base(deserializing) { }
62    private SPSOAdaptiveRandomTopologyUpdater(SPSOAdaptiveRandomTopologyUpdater original, Cloner cloner) : base(original, cloner) { }
63    public SPSOAdaptiveRandomTopologyUpdater()
64      : base() {
65      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "Whether the problem is to be maximized or not."));
66      Parameters.Add(new LookupParameter<DoubleValue>("SwarmBestQuality", "The swarm's best quality."));
67      Parameters.Add(new LookupParameter<DoubleValue>("PreviousBestQuality", "The best quality of the previous iteration."));
68      Parameters.Add(new LookupParameter<ITopologyInitializer>("TopologyInitializer", "The topology initializer is called again in case no improvement is made."));
69      Parameters.Add(new ScopeTreeLookupParameter<IntArray>("Neighbors", "The list of neighbors for each particle."));
70    }
71    public override IDeepCloneable Clone(Cloner cloner) {
72      return new SPSOAdaptiveRandomTopologyUpdater(this, cloner);
73    }
74    #endregion
75
76    public override IOperation Apply() {
77      var swarmBest = SwarmBestQualityParameter.ActualValue;
78      if (swarmBest == null) return base.Apply();
79
80      var previousBest = PreviousBestQualityParameter.ActualValue;
81      if (previousBest == null) {
82        PreviousBestQualityParameter.ActualValue = new DoubleValue(swarmBest.Value);
83        return base.Apply();
84      };
85
86      var successor = new OperationCollection(new[] { base.Apply() });
87      var max = MaximizationParameter.ActualValue.Value;
88      if (max && swarmBest.Value <= previousBest.Value
89        || !max && swarmBest.Value >= previousBest.Value)
90        successor.Insert(0, ExecutionContext.CreateOperation(TopologyInitializerParameter.ActualValue));
91
92      previousBest.Value = swarmBest.Value;
93      return successor;
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.