Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/ParticleSwarmOptimization.cs @ 15428

Last change on this file since 15428 was 15214, checked in by abeham, 7 years ago

#2797:

  • Fixed adaptive random topology updater
  • Adapted default values of the best attraction parameters
  • Changed code of the new topology initializer
  • Fixed the parameters of the SPSO particle updaters (c parameter is actually (personal|neighbor)bestattraction), reordered the method signature and provided defaults
  • Removed the max beyond parameter again
  • Updated the sample and updated the unit test
    • In the sample no inertia updating is used, but the topology initializers / updaters of SPSO are used
File size: 20.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Analysis;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Operators;
30using HeuristicLab.Optimization;
31using HeuristicLab.Optimization.Operators;
32using HeuristicLab.Parameters;
33using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
34using HeuristicLab.PluginInfrastructure;
35using HeuristicLab.Random;
36
37namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
38  [Item("Particle Swarm Optimization (PSO)", "A particle swarm optimization algorithm based on Standard PSO (SPSO) as described in Clerc, M. (2012). Standard particle swarm optimisation.")]
39  [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 300)]
40  [StorableClass]
41  public sealed class ParticleSwarmOptimization : HeuristicOptimizationEngineAlgorithm, IStorableContent {
42    #region Parameter Properties
43    public IValueParameter<IntValue> SeedParameter {
44      get { return (IValueParameter<IntValue>)Parameters["Seed"]; }
45    }
46    public IValueParameter<BoolValue> SetSeedRandomlyParameter {
47      get { return (IValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
48    }
49    public IValueParameter<IntValue> SwarmSizeParameter {
50      get { return (IValueParameter<IntValue>)Parameters["SwarmSize"]; }
51    }
52    public IValueParameter<IntValue> MaxIterationsParameter {
53      get { return (IValueParameter<IntValue>)Parameters["MaxIterations"]; }
54    }
55    public IValueParameter<DoubleValue> InertiaParameter {
56      get { return (IValueParameter<DoubleValue>)Parameters["Inertia"]; }
57    }
58    public IValueParameter<DoubleValue> PersonalBestAttractionParameter {
59      get { return (IValueParameter<DoubleValue>)Parameters["PersonalBestAttraction"]; }
60    }
61    public IValueParameter<DoubleValue> NeighborBestAttractionParameter {
62      get { return (IValueParameter<DoubleValue>)Parameters["NeighborBestAttraction"]; }
63    }
64    public IValueParameter<MultiAnalyzer> AnalyzerParameter {
65      get { return (IValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
66    }
67    public IConstrainedValueParameter<IParticleCreator> ParticleCreatorParameter {
68      get { return (IConstrainedValueParameter<IParticleCreator>)Parameters["ParticleCreator"]; }
69    }
70    public IConstrainedValueParameter<IParticleUpdater> ParticleUpdaterParameter {
71      get { return (IConstrainedValueParameter<IParticleUpdater>)Parameters["ParticleUpdater"]; }
72    }
73    public IConstrainedValueParameter<ITopologyInitializer> TopologyInitializerParameter {
74      get { return (IConstrainedValueParameter<ITopologyInitializer>)Parameters["TopologyInitializer"]; }
75    }
76    public IConstrainedValueParameter<ITopologyUpdater> TopologyUpdaterParameter {
77      get { return (IConstrainedValueParameter<ITopologyUpdater>)Parameters["TopologyUpdater"]; }
78    }
79    public IConstrainedValueParameter<IDiscreteDoubleValueModifier> InertiaUpdaterParameter {
80      get { return (IConstrainedValueParameter<IDiscreteDoubleValueModifier>)Parameters["InertiaUpdater"]; }
81    }
82    public IConstrainedValueParameter<ISwarmUpdater> SwarmUpdaterParameter {
83      get { return (IConstrainedValueParameter<ISwarmUpdater>)Parameters["SwarmUpdater"]; }
84
85    }
86    #endregion
87
88    #region Properties
89
90    public string Filename { get; set; }
91
92    [Storable]
93    private BestAverageWorstQualityAnalyzer qualityAnalyzer;
94
95    [Storable]
96    private SolutionsCreator solutionsCreator;
97
98    [Storable]
99    private ParticleSwarmOptimizationMainLoop mainLoop;
100
101    public override Type ProblemType {
102      get { return typeof(ISingleObjectiveHeuristicOptimizationProblem); }
103    }
104    public new ISingleObjectiveHeuristicOptimizationProblem Problem {
105      get { return (ISingleObjectiveHeuristicOptimizationProblem)base.Problem; }
106      set { base.Problem = value; }
107    }
108    public IntValue Seed {
109      get { return SeedParameter.Value; }
110      set { SeedParameter.Value = value; }
111    }
112    public BoolValue SetSeedRandomly {
113      get { return SetSeedRandomlyParameter.Value; }
114      set { SetSeedRandomlyParameter.Value = value; }
115    }
116    public IntValue SwarmSize {
117      get { return SwarmSizeParameter.Value; }
118      set { SwarmSizeParameter.Value = value; }
119    }
120    public IntValue MaxIterations {
121      get { return MaxIterationsParameter.Value; }
122      set { MaxIterationsParameter.Value = value; }
123    }
124    public DoubleValue Inertia {
125      get { return InertiaParameter.Value; }
126      set { InertiaParameter.Value = value; }
127    }
128    public DoubleValue PersonalBestAttraction {
129      get { return PersonalBestAttractionParameter.Value; }
130      set { PersonalBestAttractionParameter.Value = value; }
131    }
132    public DoubleValue NeighborBestAttraction {
133      get { return NeighborBestAttractionParameter.Value; }
134      set { NeighborBestAttractionParameter.Value = value; }
135    }
136    public MultiAnalyzer Analyzer {
137      get { return AnalyzerParameter.Value; }
138      set { AnalyzerParameter.Value = value; }
139    }
140    public IParticleCreator ParticleCreator {
141      get { return ParticleCreatorParameter.Value; }
142      set { ParticleCreatorParameter.Value = value; }
143    }
144    public IParticleUpdater ParticleUpdater {
145      get { return ParticleUpdaterParameter.Value; }
146      set { ParticleUpdaterParameter.Value = value; }
147    }
148    public ITopologyInitializer TopologyInitializer {
149      get { return TopologyInitializerParameter.Value; }
150      set { TopologyInitializerParameter.Value = value; }
151    }
152    public ITopologyUpdater TopologyUpdater {
153      get { return TopologyUpdaterParameter.Value; }
154      set { TopologyUpdaterParameter.Value = value; }
155    }
156    public IDiscreteDoubleValueModifier InertiaUpdater {
157      get { return InertiaUpdaterParameter.Value; }
158      set { InertiaUpdaterParameter.Value = value; }
159    }
160    public ISwarmUpdater SwarmUpdater {
161      get { return SwarmUpdaterParameter.Value; }
162      set { SwarmUpdaterParameter.Value = value; }
163    }
164    #endregion
165
166    [StorableConstructor]
167    private ParticleSwarmOptimization(bool deserializing) : base(deserializing) { }
168    private ParticleSwarmOptimization(ParticleSwarmOptimization original, Cloner cloner)
169      : base(original, cloner) {
170      qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
171      solutionsCreator = cloner.Clone(original.solutionsCreator);
172      mainLoop = cloner.Clone(original.mainLoop);
173      Initialize();
174    }
175    public ParticleSwarmOptimization()
176      : base() {
177      Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
178      Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
179      Parameters.Add(new ValueParameter<IntValue>("SwarmSize", "Size of the particle swarm.", new IntValue(40)));
180      Parameters.Add(new ValueParameter<IntValue>("MaxIterations", "Maximal number of iterations.", new IntValue(1000)));
181      Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each generation.", new MultiAnalyzer()));
182      Parameters.Add(new ValueParameter<DoubleValue>("Inertia", "Inertia weight on a particle's movement (omega).", new DoubleValue(0.721)));
183      Parameters.Add(new ValueParameter<DoubleValue>("PersonalBestAttraction", "Weight for particle's pull towards its personal best soution (phi_p).", new DoubleValue(1.193)));
184      Parameters.Add(new ValueParameter<DoubleValue>("NeighborBestAttraction", "Weight for pull towards the neighborhood best solution or global best solution in case of a totally connected topology (phi_g).", new DoubleValue(1.193)));
185      Parameters.Add(new ConstrainedValueParameter<IParticleCreator>("ParticleCreator", "Operator that creates a new particle."));
186      Parameters.Add(new ConstrainedValueParameter<IParticleUpdater>("ParticleUpdater", "Operator that updates a particle."));
187      Parameters.Add(new OptionalConstrainedValueParameter<ITopologyInitializer>("TopologyInitializer", "Creates neighborhood description vectors."));
188      Parameters.Add(new OptionalConstrainedValueParameter<ITopologyUpdater>("TopologyUpdater", "Updates the neighborhood description vectors."));
189      Parameters.Add(new OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>("InertiaUpdater", "Updates the omega parameter."));
190      Parameters.Add(new ConstrainedValueParameter<ISwarmUpdater>("SwarmUpdater", "Encoding-specific parameter which is provided by the problem. May provide additional encoding-specific parameters, such as velocity bounds for real valued problems"));
191
192      RandomCreator randomCreator = new RandomCreator();
193      VariableCreator variableCreator = new VariableCreator();
194      Assigner currentInertiaAssigner = new Assigner();
195      solutionsCreator = new SolutionsCreator();
196      SubScopesCounter subScopesCounter = new SubScopesCounter();
197      Placeholder topologyInitializerPlaceholder = new Placeholder();
198      mainLoop = new ParticleSwarmOptimizationMainLoop();
199
200      OperatorGraph.InitialOperator = randomCreator;
201
202      randomCreator.SetSeedRandomlyParameter.Value = null;
203      randomCreator.SeedParameter.Value = null;
204      randomCreator.Successor = variableCreator;
205
206      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0)));
207      variableCreator.Successor = currentInertiaAssigner;
208
209      currentInertiaAssigner.Name = "CurrentInertia := Inertia";
210      currentInertiaAssigner.LeftSideParameter.ActualName = "CurrentInertia";
211      currentInertiaAssigner.RightSideParameter.ActualName = "Inertia";
212      currentInertiaAssigner.Successor = solutionsCreator;
213
214      solutionsCreator.NumberOfSolutionsParameter.ActualName = "SwarmSize";
215      ParameterizeSolutionsCreator();
216      solutionsCreator.Successor = subScopesCounter;
217
218      subScopesCounter.Name = "Initialize EvaluatedSolutions";
219      subScopesCounter.ValueParameter.ActualName = "EvaluatedSolutions";
220      subScopesCounter.Successor = topologyInitializerPlaceholder;
221
222      topologyInitializerPlaceholder.Name = "(TopologyInitializer)";
223      topologyInitializerPlaceholder.OperatorParameter.ActualName = "TopologyInitializer";
224      topologyInitializerPlaceholder.Successor = mainLoop;
225
226      mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
227      mainLoop.InertiaParameter.ActualName = "CurrentInertia";
228      mainLoop.MaxIterationsParameter.ActualName = MaxIterationsParameter.Name;
229      mainLoop.NeighborBestAttractionParameter.ActualName = NeighborBestAttractionParameter.Name;
230      mainLoop.InertiaUpdaterParameter.ActualName = InertiaUpdaterParameter.Name;
231      mainLoop.ParticleUpdaterParameter.ActualName = ParticleUpdaterParameter.Name;
232      mainLoop.PersonalBestAttractionParameter.ActualName = PersonalBestAttractionParameter.Name;
233      mainLoop.RandomParameter.ActualName = randomCreator.RandomParameter.ActualName;
234      mainLoop.SwarmSizeParameter.ActualName = SwarmSizeParameter.Name;
235      mainLoop.TopologyUpdaterParameter.ActualName = TopologyUpdaterParameter.Name;
236      mainLoop.RandomParameter.ActualName = randomCreator.RandomParameter.ActualName;
237      mainLoop.ResultsParameter.ActualName = "Results";
238
239      InitializeAnalyzers();
240      InitializeParticleCreator();
241      InitializeSwarmUpdater();
242      ParameterizeSolutionsCreator();
243      UpdateAnalyzers();
244      UpdateInertiaUpdater();
245      InitInertiaUpdater();
246      UpdateTopologyInitializer();
247      Initialize();
248      ParameterizeMainLoop();
249    }
250
251    public override IDeepCloneable Clone(Cloner cloner) {
252      return new ParticleSwarmOptimization(this, cloner);
253    }
254
255    [StorableHook(HookType.AfterDeserialization)]
256    private void AfterDeserialization() {
257      Initialize();
258    }
259
260    #region Events
261    protected override void OnProblemChanged() {
262      UpdateAnalyzers();
263      ParameterizeAnalyzers();
264      UpdateParticleUpdaterParameter();
265      UpdateTopologyParameters();
266      InitializeParticleCreator();
267      InitializeSwarmUpdater();
268      ParameterizeSolutionsCreator();
269      base.OnProblemChanged();
270    }
271
272    void TopologyInitializerParameter_ValueChanged(object sender, EventArgs e) {
273      this.UpdateTopologyParameters();
274    }
275    #endregion
276
277    #region Helpers
278    private void Initialize() {
279      TopologyInitializerParameter.ValueChanged += new EventHandler(TopologyInitializerParameter_ValueChanged);
280    }
281
282    private void InitializeParticleCreator() {
283      ParticleCreatorParameter.ValidValues.Clear();
284      if (Problem != null) {
285        IParticleCreator oldParticleCreator = ParticleCreator;
286        IParticleCreator defaultParticleCreator = Problem.Operators.OfType<IParticleCreator>().FirstOrDefault();
287        foreach (var creator in Problem.Operators.OfType<IParticleCreator>().OrderBy(x => x.Name)) {
288          ParticleCreatorParameter.ValidValues.Add(creator);
289        }
290        if (oldParticleCreator != null) {
291          var creator = ParticleCreatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldParticleCreator.GetType());
292          if (creator != null) ParticleCreator = creator;
293          else oldParticleCreator = null;
294        }
295        if (oldParticleCreator == null && defaultParticleCreator != null)
296          ParticleCreator = defaultParticleCreator;
297      }
298    }
299
300    private void InitializeAnalyzers() {
301      qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
302      qualityAnalyzer.ResultsParameter.ActualName = "Results";
303      ParameterizeAnalyzers();
304    }
305
306    private void ParameterizeAnalyzers() {
307      if (Problem != null) {
308        qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
309        qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
310        qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
311      }
312    }
313
314    private void UpdateAnalyzers() {
315      Analyzer.Operators.Clear();
316      if (Problem != null) {
317        foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>())
318          Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
319      }
320      Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
321    }
322
323    private void InitInertiaUpdater() {
324      foreach (IDiscreteDoubleValueModifier updater in InertiaUpdaterParameter.ValidValues) {
325        updater.EndIndexParameter.ActualName = MaxIterationsParameter.Name;
326        updater.EndIndexParameter.Hidden = true;
327        updater.StartIndexParameter.Value = new IntValue(0);
328        updater.StartIndexParameter.Hidden = true;
329        updater.IndexParameter.ActualName = "Iterations";
330        updater.ValueParameter.ActualName = "CurrentInertia";
331        updater.StartValueParameter.ActualName = InertiaParameter.Name;
332        updater.EndValueParameter.Value = new DoubleValue(0.70);
333      }
334    }
335
336    private void UpdateInertiaUpdater() {
337      IDiscreteDoubleValueModifier oldInertiaUpdater = InertiaUpdater;
338      InertiaUpdaterParameter.ValidValues.Clear();
339      foreach (IDiscreteDoubleValueModifier updater in ApplicationManager.Manager.GetInstances<IDiscreteDoubleValueModifier>().OrderBy(x => x.Name)) {
340        InertiaUpdaterParameter.ValidValues.Add(updater);
341      }
342      if (oldInertiaUpdater != null) {
343        IDiscreteDoubleValueModifier updater = InertiaUpdaterParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldInertiaUpdater.GetType());
344        if (updater != null) InertiaUpdaterParameter.Value = updater;
345      }
346    }
347
348    private void UpdateTopologyInitializer() {
349      var oldTopologyInitializer = TopologyInitializer;
350      TopologyInitializerParameter.ValidValues.Clear();
351      foreach (ITopologyInitializer topologyInitializer in ApplicationManager.Manager.GetInstances<ITopologyInitializer>().OrderBy(x => x.Name)) {
352        TopologyInitializerParameter.ValidValues.Add(topologyInitializer);
353      }
354
355      if (oldTopologyInitializer != null && TopologyInitializerParameter.ValidValues.Any(x => x.GetType() == oldTopologyInitializer.GetType()))
356        TopologyInitializer = TopologyInitializerParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldTopologyInitializer.GetType());
357      UpdateTopologyParameters();
358    }
359
360    private void ParameterizeTopologyUpdaters() {
361      foreach (var updater in TopologyUpdaterParameter.ValidValues.OfType<MultiPSOTopologyUpdater>()) {
362        updater.CurrentIterationParameter.ActualName = "Iterations";
363      }
364    }
365
366    private void UpdateParticleUpdaterParameter() {
367      var oldParticleUpdater = ParticleUpdater;
368      ParticleUpdaterParameter.ValidValues.Clear();
369      if (Problem != null) {
370        var defaultParticleUpdater = Problem.Operators.OfType<IParticleUpdater>().FirstOrDefault();
371
372        foreach (var particleUpdater in Problem.Operators.OfType<IParticleUpdater>().OrderBy(x => x.Name))
373          ParticleUpdaterParameter.ValidValues.Add(particleUpdater);
374
375        if (oldParticleUpdater != null) {
376          var newParticleUpdater = ParticleUpdaterParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldParticleUpdater.GetType());
377          if (newParticleUpdater != null) ParticleUpdater = newParticleUpdater;
378          else oldParticleUpdater = null;
379        }
380        if (oldParticleUpdater == null && defaultParticleUpdater != null)
381          ParticleUpdater = defaultParticleUpdater;
382      }
383    }
384
385    private void UpdateTopologyParameters() {
386      ITopologyUpdater oldTopologyUpdater = TopologyUpdater;
387      TopologyUpdaterParameter.ValidValues.Clear();
388      if (Problem != null) {
389        if (TopologyInitializerParameter.Value != null) {
390          foreach (ITopologyUpdater topologyUpdater in ApplicationManager.Manager.GetInstances<ITopologyUpdater>())
391            TopologyUpdaterParameter.ValidValues.Add(topologyUpdater);
392
393          if (oldTopologyUpdater != null) {
394            ITopologyUpdater newTopologyUpdater = TopologyUpdaterParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldTopologyUpdater.GetType());
395            if (newTopologyUpdater != null) TopologyUpdater = newTopologyUpdater;
396          }
397          ParameterizeTopologyUpdaters();
398        }
399      }
400    }
401
402    private void ParameterizeSolutionsCreator() {
403      if (Problem != null) {
404        solutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
405        if (ParticleCreatorParameter.Value != null)
406          solutionsCreator.SolutionCreatorParameter.ActualName = ParticleCreatorParameter.Name;
407        else
408          solutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
409      }
410    }
411
412    private void ParameterizeMainLoop() {
413      if (Problem != null) {
414        mainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
415      }
416    }
417
418    private void InitializeSwarmUpdater() {
419      if (Problem != null) {
420        ISwarmUpdater updater = Problem.Operators.OfType<ISwarmUpdater>().FirstOrDefault();
421        SwarmUpdaterParameter.ValidValues.Clear();
422        if (updater != null) {
423          SwarmUpdaterParameter.ValidValues.Add(updater);
424          SwarmUpdaterParameter.Value = updater;
425        }
426      }
427    }
428    #endregion
429
430  }
431}
Note: See TracBrowser for help on using the repository browser.