Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2520_PersistenceReintegration/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/ParticleSwarmOptimization.cs @ 16752

Last change on this file since 16752 was 16559, checked in by jkarder, 6 years ago

#2520: renamed Fossil to Attic and set version to 1.0.0-pre01

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