#region License Information
/* HeuristicLab
* Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System.Collections.Generic;
using System.Linq;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Operators;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
[Item("SPSO Adaptive Random Topology Updater", "Each unsuccessful iteration the topology initializer is applied again.")]
[StorableClass]
public sealed class SPSOAdaptiveRandomTopologyUpdater : SingleSuccessorOperator, ITopologyUpdater, ISingleObjectiveOperator {
public override bool CanChangeName {
get { return false; }
}
#region Parameters
public ILookupParameter MaximizationParameter {
get { return (ILookupParameter)Parameters["Maximization"]; }
}
public ILookupParameter SwarmBestQualityParameter {
get { return (ILookupParameter)Parameters["SwarmBestQuality"]; }
}
public ILookupParameter PreviousBestQualityParameter {
get { return (ILookupParameter)Parameters["PreviousBestQuality"]; }
}
public ILookupParameter TopologyInitializerParameter {
get { return (ILookupParameter)Parameters["TopologyInitializer"]; }
}
public IScopeTreeLookupParameter NeighborsParameter {
get { return (IScopeTreeLookupParameter)Parameters["Neighbors"]; }
}
#endregion
#region Construction & Cloning
[StorableConstructor]
private SPSOAdaptiveRandomTopologyUpdater(bool deserializing) : base(deserializing) { }
private SPSOAdaptiveRandomTopologyUpdater(SPSOAdaptiveRandomTopologyUpdater original, Cloner cloner) : base(original, cloner) { }
public SPSOAdaptiveRandomTopologyUpdater()
: base() {
Parameters.Add(new LookupParameter("Maximization", "Whether the problem is to be maximized or not."));
Parameters.Add(new LookupParameter("SwarmBestQuality", "The swarm's best quality."));
Parameters.Add(new LookupParameter("PreviousBestQuality", "The best quality of the previous iteration."));
Parameters.Add(new LookupParameter("TopologyInitializer", "The topology initializer is called again in case no improvement is made."));
Parameters.Add(new ScopeTreeLookupParameter("Neighbors", "The list of neighbors for each particle."));
}
public override IDeepCloneable Clone(Cloner cloner) {
return new SPSOAdaptiveRandomTopologyUpdater(this, cloner);
}
#endregion
public override IOperation Apply() {
var swarmBest = SwarmBestQualityParameter.ActualValue;
if (swarmBest == null) return base.Apply();
var previousBest = PreviousBestQualityParameter.ActualValue;
if (previousBest == null) {
PreviousBestQualityParameter.ActualValue = new DoubleValue(swarmBest.Value);
return base.Apply();
};
var successor = new OperationCollection(new[] { base.Apply() });
var max = MaximizationParameter.ActualValue.Value;
if (max && swarmBest.Value <= previousBest.Value
|| !max && swarmBest.Value >= previousBest.Value)
successor.Insert(0, ExecutionContext.CreateOperation(TopologyInitializerParameter.ActualValue));
previousBest.Value = swarmBest.Value;
return successor;
}
}
}