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 |
|
---|
22 | using HeuristicLab.Common;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Data;
|
---|
25 | using HeuristicLab.Operators;
|
---|
26 | using HeuristicLab.Optimization;
|
---|
27 | using HeuristicLab.Optimization.Operators;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 |
|
---|
31 | namespace 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["Inertia"]; }
|
---|
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<DoubleMatrix> VelocityBoundsParameter {
|
---|
59 | get { return (IValueLookupParameter<DoubleMatrix>)Parameters["VelocityBounds"]; }
|
---|
60 | }
|
---|
61 | public IValueLookupParameter<IOperator> ParticleUpdaterParameter {
|
---|
62 | get { return (IValueLookupParameter<IOperator>)Parameters["ParticleUpdater"]; }
|
---|
63 | }
|
---|
64 | public IValueLookupParameter<IOperator> TopologyUpdaterParameter {
|
---|
65 | get { return (IValueLookupParameter<IOperator>)Parameters["TopologyUpdater"]; }
|
---|
66 | }
|
---|
67 | public IValueLookupParameter<IOperator> InertiaUpdaterParameter {
|
---|
68 | get { return (IValueLookupParameter<IOperator>)Parameters["InertiaUpdater"]; }
|
---|
69 | }
|
---|
70 | public IValueLookupParameter<ResultCollection> ResultsParameter {
|
---|
71 | get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
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>("Inertia", "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 |
|
---|
115 | Parameters.Add(new ValueLookupParameter<ISwarmUpdater>("SwarmUpdater", "The encoding-specific swarm updater."));
|
---|
116 | #endregion
|
---|
117 |
|
---|
118 | #region Create operators
|
---|
119 | ResultsCollector resultsCollector = new ResultsCollector();
|
---|
120 | Placeholder swarmUpdaterPlaceholer1 = new Placeholder();
|
---|
121 | Placeholder evaluatorPlaceholder = new Placeholder();
|
---|
122 | Placeholder analyzerPlaceholder = new Placeholder();
|
---|
123 | UniformSubScopesProcessor uniformSubScopeProcessor = new UniformSubScopesProcessor();
|
---|
124 | Placeholder particleUpdaterPlaceholder = new Placeholder();
|
---|
125 | Placeholder topologyUpdaterPlaceholder = new Placeholder();
|
---|
126 | UniformSubScopesProcessor uniformSubscopesProcessor2 = new UniformSubScopesProcessor();
|
---|
127 | UniformSubScopesProcessor evaluationProcessor = new UniformSubScopesProcessor();
|
---|
128 | NeighborUpdater neighborUpdater = new NeighborUpdater();
|
---|
129 | Placeholder swarmUpdater = new Placeholder();
|
---|
130 | IntCounter currentIterationCounter = new IntCounter();
|
---|
131 | Comparator currentIterationComparator = new Comparator();
|
---|
132 | ConditionalBranch conditionalBranch = new ConditionalBranch();
|
---|
133 | Placeholder velocityBoundsUpdaterPlaceholder = new Placeholder();
|
---|
134 | Placeholder inertiaUpdaterPlaceholder = new Placeholder();
|
---|
135 | #endregion
|
---|
136 |
|
---|
137 | #region Create operator graph
|
---|
138 | OperatorGraph.InitialOperator = resultsCollector;
|
---|
139 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Iterations", null, "CurrentIteration"));
|
---|
140 | //resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Current Inertia", null, "Inertia"));
|
---|
141 | //resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
|
---|
142 | resultsCollector.ResultsParameter.ActualName = "Results";
|
---|
143 | resultsCollector.Successor = swarmUpdaterPlaceholer1;
|
---|
144 |
|
---|
145 | swarmUpdaterPlaceholer1.Name = "(Swarm Updater)";
|
---|
146 | swarmUpdaterPlaceholer1.OperatorParameter.ActualName = SwarmUpdaterParameter.ActualName;
|
---|
147 | swarmUpdaterPlaceholer1.Successor = analyzerPlaceholder;
|
---|
148 |
|
---|
149 | analyzerPlaceholder.Name = "(Analyzer)";
|
---|
150 | analyzerPlaceholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
151 | analyzerPlaceholder.Successor = uniformSubScopeProcessor;
|
---|
152 |
|
---|
153 | uniformSubScopeProcessor.Operator = particleUpdaterPlaceholder;
|
---|
154 | uniformSubScopeProcessor.Successor = evaluationProcessor;
|
---|
155 |
|
---|
156 | particleUpdaterPlaceholder.Name = "(ParticleUpdater)";
|
---|
157 | particleUpdaterPlaceholder.OperatorParameter.ActualName = ParticleUpdaterParameter.Name;
|
---|
158 |
|
---|
159 | evaluationProcessor.Parallel = new BoolValue(true);
|
---|
160 | evaluationProcessor.Operator = evaluatorPlaceholder;
|
---|
161 | evaluationProcessor.Successor = topologyUpdaterPlaceholder;
|
---|
162 |
|
---|
163 | evaluatorPlaceholder.Name = "(Evaluator)";
|
---|
164 | evaluatorPlaceholder.OperatorParameter.ActualName = EvaluatorParameter.Name;
|
---|
165 |
|
---|
166 | topologyUpdaterPlaceholder.Name = "(TopologyUpdater)";
|
---|
167 | topologyUpdaterPlaceholder.OperatorParameter.ActualName = TopologyUpdaterParameter.Name;
|
---|
168 | topologyUpdaterPlaceholder.Successor = swarmUpdater;
|
---|
169 |
|
---|
170 | swarmUpdater.Name = "Swarm Updater";
|
---|
171 | swarmUpdater.OperatorParameter.ActualName = SwarmUpdaterParameter.ActualName;
|
---|
172 | swarmUpdater.Successor = inertiaUpdaterPlaceholder;
|
---|
173 |
|
---|
174 | inertiaUpdaterPlaceholder.Name = "(Inertia Updater)";
|
---|
175 | inertiaUpdaterPlaceholder.OperatorParameter.ActualName = InertiaUpdaterParameter.ActualName;
|
---|
176 | inertiaUpdaterPlaceholder.Successor = currentIterationCounter;
|
---|
177 |
|
---|
178 | currentIterationCounter.Name = "CurrentIteration++";
|
---|
179 | currentIterationCounter.ValueParameter.ActualName = "CurrentIteration";
|
---|
180 | currentIterationCounter.Successor = currentIterationComparator;
|
---|
181 |
|
---|
182 | currentIterationComparator.LeftSideParameter.ActualName = "CurrentIteration";
|
---|
183 | currentIterationComparator.Comparison = new Comparison(ComparisonType.Less);
|
---|
184 | currentIterationComparator.RightSideParameter.ActualName = "MaxIterations";
|
---|
185 | currentIterationComparator.ResultParameter.ActualName = "ContinueIteration";
|
---|
186 | currentIterationComparator.Successor = conditionalBranch;
|
---|
187 |
|
---|
188 | conditionalBranch.Name = "ContinueIteration?";
|
---|
189 | conditionalBranch.ConditionParameter.ActualName = "ContinueIteration";
|
---|
190 | conditionalBranch.TrueBranch = analyzerPlaceholder;
|
---|
191 | #endregion
|
---|
192 | }
|
---|
193 |
|
---|
194 | public override IOperation Apply() {
|
---|
195 | if (this.ParticleUpdaterParameter.ActualValue == null)
|
---|
196 | return null;
|
---|
197 | return base.Apply();
|
---|
198 | }
|
---|
199 | }
|
---|
200 | }
|
---|