Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11956 was 11171, checked in by ascheibe, 10 years ago

#2115 merged r11170 (copyright update) into trunk

File size: 10.3 KB
RevLine 
[5560]1#region License Information
2/* HeuristicLab
[11171]3 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[5560]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;
[5568]25using HeuristicLab.Operators;
[5560]26using HeuristicLab.Optimization;
27using HeuristicLab.Optimization.Operators;
[5568]28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[5560]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 {
[5592]35
[5560]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 {
[5645]50      get { return (IValueLookupParameter<DoubleValue>)Parameters["CurrentInertia"]; }
[5560]51    }
52    public IValueLookupParameter<DoubleValue> PersonalBestAttractionParameter {
53      get { return (IValueLookupParameter<DoubleValue>)Parameters["PersonalBestAttraction"]; }
54    }
[5568]55    public IValueLookupParameter<DoubleValue> NeighborBestAttractionParameter {
56      get { return (IValueLookupParameter<DoubleValue>)Parameters["NeighborBestAttraction"]; }
[5560]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    }
[5568]67    public IValueLookupParameter<ResultCollection> ResultsParameter {
68      get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; }
[5560]69    }
[5643]70    public LookupParameter<IntValue> EvaluatedSolutionsParameter {
71      get { return (LookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
72    }
[5560]73    public IValueLookupParameter<IOperator> EvaluatorParameter {
74      get { return (IValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
75    }
[5568]76    public ValueLookupParameter<ISwarmUpdater> SwarmUpdaterParameter {
77      get { return (ValueLookupParameter<ISwarmUpdater>)Parameters["SwarmUpdater"]; }
78    }
[5560]79    #endregion
[5568]80
[5560]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."));
[5592]99      Parameters.Add(new ValueLookupParameter<IntValue>("SwarmSize", "Size of the particle swarm."));
100      Parameters.Add(new ValueLookupParameter<IntValue>("MaxIterations", "Maximal number of iterations."));
[5568]101
[5560]102      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
103
[5645]104      Parameters.Add(new ValueLookupParameter<DoubleValue>("CurrentInertia", "Inertia weight on a particle's movement (omega)."));
[5560]105      Parameters.Add(new ValueLookupParameter<DoubleValue>("PersonalBestAttraction", "Weight for particle's pull towards its personal best soution (phi_p)."));
[5568]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
[5560]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
[5568]113      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The variable collection where results should be stored."));
[5643]114      Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of times solutions have been evaluated."));
[5568]115
116      Parameters.Add(new ValueLookupParameter<ISwarmUpdater>("SwarmUpdater", "The encoding-specific swarm updater."));
[5560]117      #endregion
118
119      #region Create operators
[5568]120      ResultsCollector resultsCollector = new ResultsCollector();
121      Placeholder swarmUpdaterPlaceholer1 = new Placeholder();
[5560]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();
[5566]128      Placeholder swarmUpdater = new Placeholder();
[5935]129      IntCounter iterationsCounter = new IntCounter();
130      Comparator iterationsComparator = new Comparator();
[5560]131      ConditionalBranch conditionalBranch = new ConditionalBranch();
[5566]132      Placeholder inertiaUpdaterPlaceholder = new Placeholder();
[5643]133      SubScopesCounter subScopesCounter = new SubScopesCounter();
[5560]134      #endregion
135
136      #region Create operator graph
[5568]137      OperatorGraph.InitialOperator = resultsCollector;
[5645]138      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
139      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("CurrentInertia"));
[5643]140      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
[5568]141      resultsCollector.ResultsParameter.ActualName = "Results";
142      resultsCollector.Successor = swarmUpdaterPlaceholer1;
[5560]143
[5568]144      swarmUpdaterPlaceholer1.Name = "(Swarm Updater)";
145      swarmUpdaterPlaceholer1.OperatorParameter.ActualName = SwarmUpdaterParameter.ActualName;
146      swarmUpdaterPlaceholer1.Successor = analyzerPlaceholder;
147
[5566]148      analyzerPlaceholder.Name = "(Analyzer)";
[5568]149      analyzerPlaceholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
[5566]150      analyzerPlaceholder.Successor = uniformSubScopeProcessor;
[5560]151
[5566]152      uniformSubScopeProcessor.Operator = particleUpdaterPlaceholder;
153      uniformSubScopeProcessor.Successor = evaluationProcessor;
[5560]154
[5566]155      particleUpdaterPlaceholder.Name = "(ParticleUpdater)";
[5568]156      particleUpdaterPlaceholder.OperatorParameter.ActualName = ParticleUpdaterParameter.Name;
[5560]157
[5566]158      evaluationProcessor.Parallel = new BoolValue(true);
159      evaluationProcessor.Operator = evaluatorPlaceholder;
[5643]160      evaluationProcessor.Successor = subScopesCounter;
[5560]161
[5566]162      evaluatorPlaceholder.Name = "(Evaluator)";
[5568]163      evaluatorPlaceholder.OperatorParameter.ActualName = EvaluatorParameter.Name;
[5560]164
[5643]165      subScopesCounter.Name = "Increment EvaluatedSolutions";
166      subScopesCounter.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
[5645]167      subScopesCounter.Successor = topologyUpdaterPlaceholder;
[5643]168
[5566]169      topologyUpdaterPlaceholder.Name = "(TopologyUpdater)";
[5568]170      topologyUpdaterPlaceholder.OperatorParameter.ActualName = TopologyUpdaterParameter.Name;
[5566]171      topologyUpdaterPlaceholder.Successor = swarmUpdater;
[5560]172
[5866]173      swarmUpdater.Name = "(Swarm Updater)";
[5568]174      swarmUpdater.OperatorParameter.ActualName = SwarmUpdaterParameter.ActualName;
175      swarmUpdater.Successor = inertiaUpdaterPlaceholder;
[5560]176
[5568]177      inertiaUpdaterPlaceholder.Name = "(Inertia Updater)";
178      inertiaUpdaterPlaceholder.OperatorParameter.ActualName = InertiaUpdaterParameter.ActualName;
[5935]179      inertiaUpdaterPlaceholder.Successor = iterationsCounter;
[5568]180
[5935]181      iterationsCounter.Name = "Iterations++";
182      iterationsCounter.ValueParameter.ActualName = "Iterations";
183      iterationsCounter.Successor = iterationsComparator;
[5560]184
[5935]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;
[5560]190
[5566]191      conditionalBranch.Name = "ContinueIteration?";
192      conditionalBranch.ConditionParameter.ActualName = "ContinueIteration";
[5581]193      conditionalBranch.TrueBranch = analyzerPlaceholder;
[5560]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.