Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5581 was 5581, checked in by mkofler, 14 years ago

#852: PSO code refactoring. Fixed wiring in main loop (Analyzer was not in loop) and fixed small issue in RealVectorSwarmUpdater.

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