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