Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/ParticleSwarmOptimizationMainLoop.cs @ 17536

Last change on this file since 17536 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 10.5 KB
RevLine 
[5560]1#region License Information
2/* HeuristicLab
[17181]3 * Copyright (C) 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;
[17097]29using HEAL.Attic;
[5560]30
31namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
32  [Item("ParticleSwarmOptimizationMainLoop", "An operator which represents the main loop of a particle swarm optimization algorithm.")]
[17097]33  [StorableType("C9824DCA-F791-4540-8EC8-F27C8FBC6BF5")]
[5560]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]
[17097]87    protected ParticleSwarmOptimizationMainLoop(StorableConstructorFlag _) : base(_) { }
[5560]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();
[15277]124      Placeholder analyzer2Placeholder = new Placeholder();
[5560]125      UniformSubScopesProcessor uniformSubScopeProcessor = new UniformSubScopesProcessor();
126      Placeholder particleUpdaterPlaceholder = new Placeholder();
127      Placeholder topologyUpdaterPlaceholder = new Placeholder();
128      UniformSubScopesProcessor evaluationProcessor = new UniformSubScopesProcessor();
[5566]129      Placeholder swarmUpdater = new Placeholder();
[5935]130      IntCounter iterationsCounter = new IntCounter();
131      Comparator iterationsComparator = new Comparator();
[5560]132      ConditionalBranch conditionalBranch = new ConditionalBranch();
[5566]133      Placeholder inertiaUpdaterPlaceholder = new Placeholder();
[5643]134      SubScopesCounter subScopesCounter = new SubScopesCounter();
[5560]135      #endregion
136
137      #region Create operator graph
[5568]138      OperatorGraph.InitialOperator = resultsCollector;
[5645]139      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
140      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("CurrentInertia"));
[5643]141      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
[5568]142      resultsCollector.ResultsParameter.ActualName = "Results";
143      resultsCollector.Successor = swarmUpdaterPlaceholer1;
[5560]144
[5568]145      swarmUpdaterPlaceholer1.Name = "(Swarm Updater)";
146      swarmUpdaterPlaceholer1.OperatorParameter.ActualName = SwarmUpdaterParameter.ActualName;
147      swarmUpdaterPlaceholer1.Successor = analyzerPlaceholder;
148
[5566]149      analyzerPlaceholder.Name = "(Analyzer)";
[5568]150      analyzerPlaceholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
[5566]151      analyzerPlaceholder.Successor = uniformSubScopeProcessor;
[5560]152
[5566]153      uniformSubScopeProcessor.Operator = particleUpdaterPlaceholder;
154      uniformSubScopeProcessor.Successor = evaluationProcessor;
[5560]155
[5566]156      particleUpdaterPlaceholder.Name = "(ParticleUpdater)";
[5568]157      particleUpdaterPlaceholder.OperatorParameter.ActualName = ParticleUpdaterParameter.Name;
[5560]158
[5566]159      evaluationProcessor.Parallel = new BoolValue(true);
160      evaluationProcessor.Operator = evaluatorPlaceholder;
[5643]161      evaluationProcessor.Successor = subScopesCounter;
[5560]162
[5566]163      evaluatorPlaceholder.Name = "(Evaluator)";
[5568]164      evaluatorPlaceholder.OperatorParameter.ActualName = EvaluatorParameter.Name;
[5560]165
[5643]166      subScopesCounter.Name = "Increment EvaluatedSolutions";
167      subScopesCounter.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
[5645]168      subScopesCounter.Successor = topologyUpdaterPlaceholder;
[5643]169
[5566]170      topologyUpdaterPlaceholder.Name = "(TopologyUpdater)";
[5568]171      topologyUpdaterPlaceholder.OperatorParameter.ActualName = TopologyUpdaterParameter.Name;
[5566]172      topologyUpdaterPlaceholder.Successor = swarmUpdater;
[5560]173
[5866]174      swarmUpdater.Name = "(Swarm Updater)";
[5568]175      swarmUpdater.OperatorParameter.ActualName = SwarmUpdaterParameter.ActualName;
176      swarmUpdater.Successor = inertiaUpdaterPlaceholder;
[5560]177
[5568]178      inertiaUpdaterPlaceholder.Name = "(Inertia Updater)";
179      inertiaUpdaterPlaceholder.OperatorParameter.ActualName = InertiaUpdaterParameter.ActualName;
[5935]180      inertiaUpdaterPlaceholder.Successor = iterationsCounter;
[5568]181
[5935]182      iterationsCounter.Name = "Iterations++";
183      iterationsCounter.ValueParameter.ActualName = "Iterations";
184      iterationsCounter.Successor = iterationsComparator;
[5560]185
[5935]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;
[5560]191
[5566]192      conditionalBranch.Name = "ContinueIteration?";
193      conditionalBranch.ConditionParameter.ActualName = "ContinueIteration";
[5581]194      conditionalBranch.TrueBranch = analyzerPlaceholder;
[15277]195      conditionalBranch.FalseBranch = analyzer2Placeholder;
196
197      analyzer2Placeholder.Name = "(Analyzer)";
198      analyzer2Placeholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
[5560]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.