Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15444 was 15181, checked in by abeham, 7 years ago

#2797:

  • Added IStochasticOperator interface to MultiPSOTopologyUpdater
  • Changed parameter defaults to those described in the paper
  • Added analyzer placeholder for the last iteration (has not been previously analyzed)
  • Changed random topology initializer to include itself (to be able to use it with SPSOSwarmUpdater -> this should not change the old RealVectorSwarmUpdater)
  • Changed ring topology initializer to include itself (same as above)
  • Changed von neumann topology initializer to include itself (same as above)
  • Added SPSO compatible random topology initializer (as described in the paper by Clerc)
  • Changed sampling of the random directional vector to be uniformly random on the surface of a hypersphere to avoid a slight bias in diagonal direction
  • Updating SwarmBestQuality and BestRealVector parameters in SPSOSwarmUpdater (an oversight)
  • Added a faster method to create a copy of a RealVector (based on Array.Copy)
  • Updated the sample
  • Updated the sample's test results (due to changed sampling in SPSO2011ParticleUpdater)
File size: 10.5 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 HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Operators;
26using HeuristicLab.Optimization;
27using HeuristicLab.Optimization.Operators;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
32  [Item("ParticleSwarmOptimizationMainLoop", "An operator which represents the main loop of a particle swarm optimization algorithm.")]
33  [StorableClass]
34  public class ParticleSwarmOptimizationMainLoop : AlgorithmOperator {
35
36    #region Parameter Properties
37    public IValueLookupParameter<IRandom> RandomParameter {
38      get { return (IValueLookupParameter<IRandom>)Parameters["Random"]; }
39    }
40    public IValueLookupParameter<IntValue> SwarmSizeParameter {
41      get { return (IValueLookupParameter<IntValue>)Parameters["SwarmSize"]; }
42    }
43    public IValueLookupParameter<IntValue> MaxIterationsParameter {
44      get { return (IValueLookupParameter<IntValue>)Parameters["MaxIterations"]; }
45    }
46    public IValueLookupParameter<IOperator> AnalyzerParameter {
47      get { return (IValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
48    }
49    public IValueLookupParameter<DoubleValue> InertiaParameter {
50      get { return (IValueLookupParameter<DoubleValue>)Parameters["CurrentInertia"]; }
51    }
52    public IValueLookupParameter<DoubleValue> PersonalBestAttractionParameter {
53      get { return (IValueLookupParameter<DoubleValue>)Parameters["PersonalBestAttraction"]; }
54    }
55    public IValueLookupParameter<DoubleValue> NeighborBestAttractionParameter {
56      get { return (IValueLookupParameter<DoubleValue>)Parameters["NeighborBestAttraction"]; }
57    }
58    public IValueLookupParameter<IOperator> ParticleUpdaterParameter {
59      get { return (IValueLookupParameter<IOperator>)Parameters["ParticleUpdater"]; }
60    }
61    public IValueLookupParameter<IOperator> TopologyUpdaterParameter {
62      get { return (IValueLookupParameter<IOperator>)Parameters["TopologyUpdater"]; }
63    }
64    public IValueLookupParameter<IOperator> InertiaUpdaterParameter {
65      get { return (IValueLookupParameter<IOperator>)Parameters["InertiaUpdater"]; }
66    }
67    public IValueLookupParameter<ResultCollection> ResultsParameter {
68      get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; }
69    }
70    public LookupParameter<IntValue> EvaluatedSolutionsParameter {
71      get { return (LookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
72    }
73    public IValueLookupParameter<IOperator> EvaluatorParameter {
74      get { return (IValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
75    }
76    public ValueLookupParameter<ISwarmUpdater> SwarmUpdaterParameter {
77      get { return (ValueLookupParameter<ISwarmUpdater>)Parameters["SwarmUpdater"]; }
78    }
79    #endregion
80
81    public ParticleSwarmOptimizationMainLoop()
82      : base() {
83      Initialize();
84    }
85
86    [StorableConstructor]
87    protected ParticleSwarmOptimizationMainLoop(bool deserializing) : base(deserializing) { }
88    protected ParticleSwarmOptimizationMainLoop(ParticleSwarmOptimizationMainLoop original, Cloner cloner)
89      : base(original, cloner) {
90    }
91
92    public override IDeepCloneable Clone(Cloner cloner) {
93      return new ParticleSwarmOptimizationMainLoop(this, cloner);
94    }
95
96    private void Initialize() {
97      #region Create parameters
98      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
99      Parameters.Add(new ValueLookupParameter<IntValue>("SwarmSize", "Size of the particle swarm."));
100      Parameters.Add(new ValueLookupParameter<IntValue>("MaxIterations", "Maximal number of iterations."));
101
102      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
103
104      Parameters.Add(new ValueLookupParameter<DoubleValue>("CurrentInertia", "Inertia weight on a particle's movement (omega)."));
105      Parameters.Add(new ValueLookupParameter<DoubleValue>("PersonalBestAttraction", "Weight for particle's pull towards its personal best soution (phi_p)."));
106      Parameters.Add(new ValueLookupParameter<DoubleValue>("NeighborBestAttraction", "Weight for pull towards the neighborhood best solution or global best solution in case of a totally connected topology (phi_g)."));
107
108      Parameters.Add(new ValueLookupParameter<IOperator>("ParticleUpdater", "Operator that calculates new position and velocity of a particle"));
109      Parameters.Add(new ValueLookupParameter<IOperator>("TopologyUpdater", "Updates the neighborhood description vectors"));
110      Parameters.Add(new ValueLookupParameter<IOperator>("InertiaUpdater", "Updates the omega parameter"));
111      Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator", "Evaluates a particle solution."));
112
113      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The variable collection where results should be stored."));
114      Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of times solutions have been evaluated."));
115
116      Parameters.Add(new ValueLookupParameter<ISwarmUpdater>("SwarmUpdater", "The encoding-specific swarm updater."));
117      #endregion
118
119      #region Create operators
120      ResultsCollector resultsCollector = new ResultsCollector();
121      Placeholder swarmUpdaterPlaceholer1 = new Placeholder();
122      Placeholder evaluatorPlaceholder = new Placeholder();
123      Placeholder analyzerPlaceholder = new Placeholder();
124      Placeholder analyzer2Placeholder = new Placeholder();
125      UniformSubScopesProcessor uniformSubScopeProcessor = new UniformSubScopesProcessor();
126      Placeholder particleUpdaterPlaceholder = new Placeholder();
127      Placeholder topologyUpdaterPlaceholder = new Placeholder();
128      UniformSubScopesProcessor evaluationProcessor = new UniformSubScopesProcessor();
129      Placeholder swarmUpdater = new Placeholder();
130      IntCounter iterationsCounter = new IntCounter();
131      Comparator iterationsComparator = new Comparator();
132      ConditionalBranch conditionalBranch = new ConditionalBranch();
133      Placeholder inertiaUpdaterPlaceholder = new Placeholder();
134      SubScopesCounter subScopesCounter = new SubScopesCounter();
135      #endregion
136
137      #region Create operator graph
138      OperatorGraph.InitialOperator = resultsCollector;
139      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
140      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("CurrentInertia"));
141      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
142      resultsCollector.ResultsParameter.ActualName = "Results";
143      resultsCollector.Successor = swarmUpdaterPlaceholer1;
144
145      swarmUpdaterPlaceholer1.Name = "(Swarm Updater)";
146      swarmUpdaterPlaceholer1.OperatorParameter.ActualName = SwarmUpdaterParameter.ActualName;
147      swarmUpdaterPlaceholer1.Successor = analyzerPlaceholder;
148
149      analyzerPlaceholder.Name = "(Analyzer)";
150      analyzerPlaceholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
151      analyzerPlaceholder.Successor = uniformSubScopeProcessor;
152
153      uniformSubScopeProcessor.Operator = particleUpdaterPlaceholder;
154      uniformSubScopeProcessor.Successor = evaluationProcessor;
155
156      particleUpdaterPlaceholder.Name = "(ParticleUpdater)";
157      particleUpdaterPlaceholder.OperatorParameter.ActualName = ParticleUpdaterParameter.Name;
158
159      evaluationProcessor.Parallel = new BoolValue(true);
160      evaluationProcessor.Operator = evaluatorPlaceholder;
161      evaluationProcessor.Successor = subScopesCounter;
162
163      evaluatorPlaceholder.Name = "(Evaluator)";
164      evaluatorPlaceholder.OperatorParameter.ActualName = EvaluatorParameter.Name;
165
166      subScopesCounter.Name = "Increment EvaluatedSolutions";
167      subScopesCounter.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
168      subScopesCounter.Successor = topologyUpdaterPlaceholder;
169
170      topologyUpdaterPlaceholder.Name = "(TopologyUpdater)";
171      topologyUpdaterPlaceholder.OperatorParameter.ActualName = TopologyUpdaterParameter.Name;
172      topologyUpdaterPlaceholder.Successor = swarmUpdater;
173
174      swarmUpdater.Name = "(Swarm Updater)";
175      swarmUpdater.OperatorParameter.ActualName = SwarmUpdaterParameter.ActualName;
176      swarmUpdater.Successor = inertiaUpdaterPlaceholder;
177
178      inertiaUpdaterPlaceholder.Name = "(Inertia Updater)";
179      inertiaUpdaterPlaceholder.OperatorParameter.ActualName = InertiaUpdaterParameter.ActualName;
180      inertiaUpdaterPlaceholder.Successor = iterationsCounter;
181
182      iterationsCounter.Name = "Iterations++";
183      iterationsCounter.ValueParameter.ActualName = "Iterations";
184      iterationsCounter.Successor = iterationsComparator;
185
186      iterationsComparator.LeftSideParameter.ActualName = "Iterations";
187      iterationsComparator.Comparison = new Comparison(ComparisonType.Less);
188      iterationsComparator.RightSideParameter.ActualName = "MaxIterations";
189      iterationsComparator.ResultParameter.ActualName = "ContinueIteration";
190      iterationsComparator.Successor = conditionalBranch;
191
192      conditionalBranch.Name = "ContinueIteration?";
193      conditionalBranch.ConditionParameter.ActualName = "ContinueIteration";
194      conditionalBranch.TrueBranch = analyzerPlaceholder;
195      conditionalBranch.FalseBranch = analyzer2Placeholder;
196
197      analyzer2Placeholder.Name = "(Analyzer)";
198      analyzer2Placeholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
199      #endregion
200    }
201
202    public override IOperation Apply() {
203      if (this.ParticleUpdaterParameter.ActualValue == null)
204        return null;
205      return base.Apply();
206    }
207  }
208}
Note: See TracBrowser for help on using the repository browser.