Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5643 was 5643, checked in by mkofler, 12 years ago

#852: PSO code refactoring. Cleanup and minor improvements.

File size: 10.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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["Inertia"]; }
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<DoubleMatrix> VelocityBoundsParameter {
59      get { return (IValueLookupParameter<DoubleMatrix>)Parameters["VelocityBounds"]; }
60    }
61    public IValueLookupParameter<IOperator> ParticleUpdaterParameter {
62      get { return (IValueLookupParameter<IOperator>)Parameters["ParticleUpdater"]; }
63    }
64    public IValueLookupParameter<IOperator> TopologyUpdaterParameter {
65      get { return (IValueLookupParameter<IOperator>)Parameters["TopologyUpdater"]; }
66    }
67    public IValueLookupParameter<IOperator> InertiaUpdaterParameter {
68      get { return (IValueLookupParameter<IOperator>)Parameters["InertiaUpdater"]; }
69    }
70    public IValueLookupParameter<ResultCollection> ResultsParameter {
71      get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; }
72    }
73    public LookupParameter<IntValue> EvaluatedSolutionsParameter {
74      get { return (LookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
75    }
76    public IValueLookupParameter<IOperator> EvaluatorParameter {
77      get { return (IValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
78    }
79    public ValueLookupParameter<ISwarmUpdater> SwarmUpdaterParameter {
80      get { return (ValueLookupParameter<ISwarmUpdater>)Parameters["SwarmUpdater"]; }
81    }
82    #endregion
83
84    public ParticleSwarmOptimizationMainLoop()
85      : base() {
86      Initialize();
87    }
88
89    [StorableConstructor]
90    protected ParticleSwarmOptimizationMainLoop(bool deserializing) : base(deserializing) { }
91    protected ParticleSwarmOptimizationMainLoop(ParticleSwarmOptimizationMainLoop original, Cloner cloner)
92      : base(original, cloner) {
93    }
94
95    public override IDeepCloneable Clone(Cloner cloner) {
96      return new ParticleSwarmOptimizationMainLoop(this, cloner);
97    }
98
99    private void Initialize() {
100      #region Create parameters
101      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
102      Parameters.Add(new ValueLookupParameter<IntValue>("SwarmSize", "Size of the particle swarm."));
103      Parameters.Add(new ValueLookupParameter<IntValue>("MaxIterations", "Maximal number of iterations."));
104
105      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
106
107      Parameters.Add(new ValueLookupParameter<DoubleValue>("Inertia", "Inertia weight on a particle's movement (omega)."));
108      Parameters.Add(new ValueLookupParameter<DoubleValue>("PersonalBestAttraction", "Weight for particle's pull towards its personal best soution (phi_p)."));
109      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)."));
110
111      Parameters.Add(new ValueLookupParameter<IOperator>("ParticleUpdater", "Operator that calculates new position and velocity of a particle"));
112      Parameters.Add(new ValueLookupParameter<IOperator>("TopologyUpdater", "Updates the neighborhood description vectors"));
113      Parameters.Add(new ValueLookupParameter<IOperator>("InertiaUpdater", "Updates the omega parameter"));
114      Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator", "Evaluates a particle solution."));
115
116      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The variable collection where results should be stored."));
117      Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of times solutions have been evaluated."));
118
119      Parameters.Add(new ValueLookupParameter<ISwarmUpdater>("SwarmUpdater", "The encoding-specific swarm updater."));
120      #endregion
121
122      #region Create operators
123      ResultsCollector resultsCollector = new ResultsCollector();
124      Placeholder swarmUpdaterPlaceholer1 = new Placeholder();
125      Placeholder evaluatorPlaceholder = new Placeholder();
126      Placeholder analyzerPlaceholder = new Placeholder();
127      UniformSubScopesProcessor uniformSubScopeProcessor = new UniformSubScopesProcessor();
128      Placeholder particleUpdaterPlaceholder = new Placeholder();
129      Placeholder topologyUpdaterPlaceholder = new Placeholder();
130      UniformSubScopesProcessor uniformSubscopesProcessor2 = new UniformSubScopesProcessor();
131      UniformSubScopesProcessor evaluationProcessor = new UniformSubScopesProcessor();
132      Placeholder swarmUpdater = new Placeholder();
133      IntCounter currentIterationCounter = new IntCounter();
134      Comparator currentIterationComparator = new Comparator();
135      ConditionalBranch conditionalBranch = new ConditionalBranch();
136      Placeholder velocityBoundsUpdaterPlaceholder = new Placeholder();
137      Placeholder inertiaUpdaterPlaceholder = new Placeholder();
138      SubScopesCounter subScopesCounter = new SubScopesCounter();
139      #endregion
140
141      #region Create operator graph
142      OperatorGraph.InitialOperator = resultsCollector;
143      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Iterations", null, "CurrentIteration"));
144      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Inertia", null, "CurrentInertia"));
145      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
146      resultsCollector.ResultsParameter.ActualName = "Results";
147      resultsCollector.Successor = swarmUpdaterPlaceholer1;
148
149      swarmUpdaterPlaceholer1.Name = "(Swarm Updater)";
150      swarmUpdaterPlaceholer1.OperatorParameter.ActualName = SwarmUpdaterParameter.ActualName;
151      swarmUpdaterPlaceholer1.Successor = analyzerPlaceholder;
152
153      analyzerPlaceholder.Name = "(Analyzer)";
154      analyzerPlaceholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
155      analyzerPlaceholder.Successor = uniformSubScopeProcessor;
156
157      uniformSubScopeProcessor.Operator = particleUpdaterPlaceholder;
158      uniformSubScopeProcessor.Successor = evaluationProcessor;
159
160      particleUpdaterPlaceholder.Name = "(ParticleUpdater)";
161      particleUpdaterPlaceholder.OperatorParameter.ActualName = ParticleUpdaterParameter.Name;
162
163      evaluationProcessor.Parallel = new BoolValue(true);
164      evaluationProcessor.Operator = evaluatorPlaceholder;
165      evaluationProcessor.Successor = subScopesCounter;
166
167      evaluatorPlaceholder.Name = "(Evaluator)";
168      evaluatorPlaceholder.OperatorParameter.ActualName = EvaluatorParameter.Name;
169
170      subScopesCounter.Name = "Increment EvaluatedSolutions";
171      subScopesCounter.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
172      subScopesCounter.Successor = topologyUpdaterPlaceholder;
173
174      topologyUpdaterPlaceholder.Name = "(TopologyUpdater)";
175      topologyUpdaterPlaceholder.OperatorParameter.ActualName = TopologyUpdaterParameter.Name;
176      topologyUpdaterPlaceholder.Successor = swarmUpdater;
177
178      swarmUpdater.Name = "Swarm Updater";
179      swarmUpdater.OperatorParameter.ActualName = SwarmUpdaterParameter.ActualName;
180      swarmUpdater.Successor = inertiaUpdaterPlaceholder;
181
182      inertiaUpdaterPlaceholder.Name = "(Inertia Updater)";
183      inertiaUpdaterPlaceholder.OperatorParameter.ActualName = InertiaUpdaterParameter.ActualName;
184      inertiaUpdaterPlaceholder.Successor = currentIterationCounter;
185
186      currentIterationCounter.Name = "CurrentIteration++";
187      currentIterationCounter.ValueParameter.ActualName = "CurrentIteration";
188      currentIterationCounter.Successor = currentIterationComparator;
189
190      currentIterationComparator.LeftSideParameter.ActualName = "CurrentIteration";
191      currentIterationComparator.Comparison = new Comparison(ComparisonType.Less);
192      currentIterationComparator.RightSideParameter.ActualName = "MaxIterations";
193      currentIterationComparator.ResultParameter.ActualName = "ContinueIteration";
194      currentIterationComparator.Successor = conditionalBranch;
195
196      conditionalBranch.Name = "ContinueIteration?";
197      conditionalBranch.ConditionParameter.ActualName = "ContinueIteration";
198      conditionalBranch.TrueBranch = analyzerPlaceholder;
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.