Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2434_crossvalidation/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/ParticleSwarmOptimizationMainLoop.cs @ 17603

Last change on this file since 17603 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 10.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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      UniformSubScopesProcessor uniformSubScopeProcessor = new UniformSubScopesProcessor();
125      Placeholder particleUpdaterPlaceholder = new Placeholder();
126      Placeholder topologyUpdaterPlaceholder = new Placeholder();
127      UniformSubScopesProcessor evaluationProcessor = new UniformSubScopesProcessor();
128      Placeholder swarmUpdater = new Placeholder();
129      IntCounter iterationsCounter = new IntCounter();
130      Comparator iterationsComparator = new Comparator();
131      ConditionalBranch conditionalBranch = new ConditionalBranch();
132      Placeholder inertiaUpdaterPlaceholder = new Placeholder();
133      SubScopesCounter subScopesCounter = new SubScopesCounter();
134      #endregion
135
136      #region Create operator graph
137      OperatorGraph.InitialOperator = resultsCollector;
138      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
139      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("CurrentInertia"));
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 = subScopesCounter;
161
162      evaluatorPlaceholder.Name = "(Evaluator)";
163      evaluatorPlaceholder.OperatorParameter.ActualName = EvaluatorParameter.Name;
164
165      subScopesCounter.Name = "Increment EvaluatedSolutions";
166      subScopesCounter.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
167      subScopesCounter.Successor = topologyUpdaterPlaceholder;
168
169      topologyUpdaterPlaceholder.Name = "(TopologyUpdater)";
170      topologyUpdaterPlaceholder.OperatorParameter.ActualName = TopologyUpdaterParameter.Name;
171      topologyUpdaterPlaceholder.Successor = swarmUpdater;
172
173      swarmUpdater.Name = "(Swarm Updater)";
174      swarmUpdater.OperatorParameter.ActualName = SwarmUpdaterParameter.ActualName;
175      swarmUpdater.Successor = inertiaUpdaterPlaceholder;
176
177      inertiaUpdaterPlaceholder.Name = "(Inertia Updater)";
178      inertiaUpdaterPlaceholder.OperatorParameter.ActualName = InertiaUpdaterParameter.ActualName;
179      inertiaUpdaterPlaceholder.Successor = iterationsCounter;
180
181      iterationsCounter.Name = "Iterations++";
182      iterationsCounter.ValueParameter.ActualName = "Iterations";
183      iterationsCounter.Successor = iterationsComparator;
184
185      iterationsComparator.LeftSideParameter.ActualName = "Iterations";
186      iterationsComparator.Comparison = new Comparison(ComparisonType.Less);
187      iterationsComparator.RightSideParameter.ActualName = "MaxIterations";
188      iterationsComparator.ResultParameter.ActualName = "ContinueIteration";
189      iterationsComparator.Successor = conditionalBranch;
190
191      conditionalBranch.Name = "ContinueIteration?";
192      conditionalBranch.ConditionParameter.ActualName = "ContinueIteration";
193      conditionalBranch.TrueBranch = analyzerPlaceholder;
194      #endregion
195    }
196
197    public override IOperation Apply() {
198      if (this.ParticleUpdaterParameter.ActualValue == null)
199        return null;
200      return base.Apply();
201    }
202  }
203}
Note: See TracBrowser for help on using the repository browser.