Free cookie consent management tool by TermsFeed Policy Generator

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

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

#852: Code refactoring. Created new interfaces and moved operators to respective projects as suggested in A. Beham's review. Work in progress.

File size: 10.0 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 System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Operators;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Parameters;
31using HeuristicLab.Data;
32using HeuristicLab.Analysis;
33using HeuristicLab.Optimization;
34using HeuristicLab.Random;
35using HeuristicLab.Optimization.Operators;
36using HeuristicLab.Encodings.RealVectorEncoding;
37
38namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
39  [Item("ParticleSwarmOptimizationMainLoop", "An operator which represents the main loop of a particle swarm optimization algorithm.")]
40  [StorableClass]
41  public class ParticleSwarmOptimizationMainLoop : AlgorithmOperator {
42    #region Parameter Properties
43    public IValueLookupParameter<IRandom> RandomParameter {
44      get { return (IValueLookupParameter<IRandom>)Parameters["Random"]; }
45    }
46    public IValueLookupParameter<IntValue> SwarmSizeParameter {
47      get { return (IValueLookupParameter<IntValue>)Parameters["SwarmSize"]; }
48    }
49    public IValueLookupParameter<IntValue> MaxIterationsParameter {
50      get { return (IValueLookupParameter<IntValue>)Parameters["MaxIterations"]; }
51    }
52    public IValueLookupParameter<IOperator> AnalyzerParameter {
53      get { return (IValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
54    }
55    public IValueLookupParameter<DoubleValue> InertiaParameter {
56      get { return (IValueLookupParameter<DoubleValue>)Parameters["Inertia"]; }
57    }
58    public IValueLookupParameter<DoubleValue> PersonalBestAttractionParameter {
59      get { return (IValueLookupParameter<DoubleValue>)Parameters["PersonalBestAttraction"]; }
60    }
61    public IValueLookupParameter<DoubleValue> NeighborsBestAttractionParameter {
62      get { return (IValueLookupParameter<DoubleValue>)Parameters["NeighborsBestAttraction"]; }
63    }
64    public IValueLookupParameter<DoubleMatrix> VelocityBoundsParameter {
65      get { return (IValueLookupParameter<DoubleMatrix>)Parameters["VelocityBounds"]; }
66    }                                     
67    public IValueLookupParameter<IOperator> ParticleUpdaterParameter {
68      get { return (IValueLookupParameter<IOperator>)Parameters["ParticleUpdater"]; }
69    }
70    public IValueLookupParameter<IOperator> TopologyUpdaterParameter {
71      get { return (IValueLookupParameter<IOperator>)Parameters["TopologyUpdater"]; }
72    }
73    public IValueLookupParameter<IOperator> InertiaUpdaterParameter {
74      get { return (IValueLookupParameter<IOperator>)Parameters["InertiaUpdater"]; }
75    }
76    public IValueLookupParameter<VariableCollection> ResultsParameter {
77      get { return (IValueLookupParameter<VariableCollection>)Parameters["Results"]; }
78    }
79    public IValueLookupParameter<IOperator> EvaluatorParameter {
80      get { return (IValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
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.", new IntValue(10)));
103      Parameters.Add(new ValueLookupParameter<IntValue>("MaxIterations", "Maximal number of iterations.", new IntValue(1000)));
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>("NeighborsBestAttraction", "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<VariableCollection>("Results", "The variable collection where results should be stored."));
117      #endregion
118
119      #region Create operators
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      ISwarmUpdater swarmUpdater = new RealVectorSwarmUpdater();
129      IntCounter currentIterationCounter = new IntCounter();
130      Comparator currentIterationComparator = new Comparator();
131      ConditionalBranch conditionalBranch = new ConditionalBranch();
132      Placeholder velocityBoundsUpdaterPlaceholder = new Placeholder();
133      Placeholder omegaUpdaterPlaceholder = new Placeholder();
134      #endregion
135
136      #region Create operator graph
137      //OperatorGraph.InitialOperator = analyzerPlaceholder;
138
139      //analyzerPlaceholder.Name = "(Analyzer)";
140      //analyzerPlaceholder.OperatorParameter.ActualName = "Analyzer";
141      //analyzerPlaceholder.Successor = uniformSubScopeProcessor;
142
143      //uniformSubScopeProcessor.Operator = particleUpdaterPlaceholder;
144      //uniformSubScopeProcessor.Successor = evaluationProcessor;
145
146      //particleUpdaterPlaceholder.Name = "(ParticleUpdater)";
147      //particleUpdaterPlaceholder.OperatorParameter.ActualName = "ParticleUpdater";
148
149      //evaluationProcessor.Parallel = new BoolValue(true);
150      //evaluationProcessor.Operator = evaluatorPlaceholder;
151      //evaluationProcessor.Successor = topologyUpdaterPlaceholder;
152
153      //evaluatorPlaceholder.Name = "(Evaluator)";
154      //evaluatorPlaceholder.OperatorParameter.ActualName = "Evaluator";
155
156      //topologyUpdaterPlaceholder.Name = "(TopologyUpdater)";
157      //topologyUpdaterPlaceholder.OperatorParameter.ActualName = "TopologyUpdater";
158      //topologyUpdaterPlaceholder.Successor = neighborUpdater;
159
160      //neighborUpdater.Successor = uniformSubscopesProcessor2;
161
162      //uniformSubscopesProcessor2.Operator = swarmUpdater;
163      //uniformSubscopesProcessor2.Successor = analyzerPlaceholder;
164
165      //analyzerPlaceholder.Name = "(Analyzer)";
166      //analyzerPlaceholder.OperatorParameter.ActualName = "Analyzer";
167      //analyzerPlaceholder.Successor = currentIterationCounter;
168
169      //currentIterationCounter.Name = "CurrentIteration++";
170      //currentIterationCounter.ValueParameter.ActualName = "CurrentIteration";
171      //currentIterationCounter.Successor = omegaUpdaterPlaceholder;
172
173      //omegaUpdaterPlaceholder.Name = "(Inertia Updater)";
174      //omegaUpdaterPlaceholder.OperatorParameter.ActualName = "InertiaUpdater";
175      //omegaUpdaterPlaceholder.Successor = velocityBoundsUpdaterPlaceholder;
176
177      //velocityBoundsUpdaterPlaceholder.Name = "(Velocity Bounds Updater)";
178      //velocityBoundsUpdaterPlaceholder.OperatorParameter.ActualName = "VelocityBoundsUpdater";
179      //velocityBoundsUpdaterPlaceholder.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 = uniformSubScopeProcessor;
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.