Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3044_variableScaling/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/AdaptiveRandomTopologyUpdater.cs @ 18242

Last change on this file since 18242 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

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