1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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 System;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Analysis;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
29 | using HeuristicLab.Operators;
|
---|
30 | using HeuristicLab.Optimization;
|
---|
31 | using HeuristicLab.Optimization.Operators;
|
---|
32 | using HeuristicLab.Parameters;
|
---|
33 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
34 | using HeuristicLab.PluginInfrastructure;
|
---|
35 | using HeuristicLab.Random;
|
---|
36 |
|
---|
37 | namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
|
---|
38 |
|
---|
39 | [Item("Particle Swarm Optimization", "A particle swarm optimization algorithm based on the description in Pedersen, M.E.H. (2010). PhD thesis. University of Southampton.")]
|
---|
40 | [Creatable("Algorithms")]
|
---|
41 | [StorableClass]
|
---|
42 | public class ParticleSwarmOptimization : EngineAlgorithm {
|
---|
43 |
|
---|
44 | #region Problem Properties
|
---|
45 | public override Type ProblemType {
|
---|
46 | get { return typeof(ISingleObjectiveProblem); }
|
---|
47 | }
|
---|
48 | public new ISingleObjectiveProblem Problem {
|
---|
49 | get { return (ISingleObjectiveProblem)base.Problem; }
|
---|
50 | set { base.Problem = value; }
|
---|
51 | }
|
---|
52 | public MultiAnalyzer Analyzer {
|
---|
53 | get { return AnalyzerParameter.Value; }
|
---|
54 | set { AnalyzerParameter.Value = value; }
|
---|
55 | }
|
---|
56 | #endregion
|
---|
57 |
|
---|
58 | #region Parameter Properties
|
---|
59 | public ValueParameter<IntValue> SeedParameter {
|
---|
60 | get { return (ValueParameter<IntValue>)Parameters["Seed"]; }
|
---|
61 | }
|
---|
62 | public ValueParameter<BoolValue> SetSeedRandomlyParameter {
|
---|
63 | get { return (ValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
|
---|
64 | }
|
---|
65 | public ValueParameter<IntValue> SwarmSizeParameter {
|
---|
66 | get { return (ValueParameter<IntValue>)Parameters["SwarmSize"]; }
|
---|
67 | }
|
---|
68 | public ValueParameter<IntValue> MaxIterationsParameter {
|
---|
69 | get { return (ValueParameter<IntValue>)Parameters["MaxIterations"]; }
|
---|
70 | }
|
---|
71 | public ValueParameter<DoubleValue> OmegaParameter {
|
---|
72 | get { return (ValueParameter<DoubleValue>)Parameters["Omega"]; }
|
---|
73 | }
|
---|
74 | public ValueParameter<DoubleValue> Phi_PParameter {
|
---|
75 | get { return (ValueParameter<DoubleValue>)Parameters["Phi_P"]; }
|
---|
76 | }
|
---|
77 | public ValueParameter<DoubleValue> Phi_GParameter {
|
---|
78 | get { return (ValueParameter<DoubleValue>)Parameters["Phi_G"]; }
|
---|
79 | }
|
---|
80 | public ValueParameter<MultiAnalyzer> AnalyzerParameter {
|
---|
81 | get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
|
---|
82 | }
|
---|
83 | public ValueLookupParameter<DoubleMatrix> VelocityBoundsParameter {
|
---|
84 | get { return (ValueLookupParameter<DoubleMatrix>)Parameters["VelocityBounds"]; }
|
---|
85 | }
|
---|
86 | public ConstrainedValueParameter<IParticleUpdater> ParticleUpdaterParameter {
|
---|
87 | get { return (ConstrainedValueParameter<IParticleUpdater>)Parameters["ParticleUpdater"]; }
|
---|
88 | }
|
---|
89 | public ConstrainedValueParameter<ITopologyInitializer> TopologyInitializerParameter {
|
---|
90 | get { return (ConstrainedValueParameter<ITopologyInitializer>)Parameters["TopologyInitializer"]; }
|
---|
91 | }
|
---|
92 | public ConstrainedValueParameter<ITopologyUpdater> TopologyUpdaterParameter {
|
---|
93 | get { return (ConstrainedValueParameter<ITopologyUpdater>)Parameters["TopologyUpdater"]; }
|
---|
94 | }
|
---|
95 | #endregion
|
---|
96 |
|
---|
97 | #region Properties
|
---|
98 | [Storable]
|
---|
99 | private BestAverageWorstQualityAnalyzer qualityAnalyzer;
|
---|
100 | #endregion
|
---|
101 |
|
---|
102 | [StorableConstructor]
|
---|
103 | protected ParticleSwarmOptimization(bool deserializing) : base(deserializing) { }
|
---|
104 | protected ParticleSwarmOptimization(ParticleSwarmOptimization original, Cloner cloner)
|
---|
105 | : base(original, cloner) {
|
---|
106 | qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
|
---|
107 | }
|
---|
108 |
|
---|
109 | public ParticleSwarmOptimization()
|
---|
110 | : base() {
|
---|
111 | Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
112 | Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
113 | Parameters.Add(new ValueParameter<IntValue>("SwarmSize", "Size of the particle swarm.", new IntValue(10)));
|
---|
114 | Parameters.Add(new ValueParameter<IntValue>("MaxIterations", "Maximal number of iterations.", new IntValue(1000)));
|
---|
115 | Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each generation.", new MultiAnalyzer()));
|
---|
116 | Parameters.Add(new ValueParameter<DoubleValue>("Omega", "Weight for particle's velocity vector.", new DoubleValue(-0.2)));
|
---|
117 | Parameters.Add(new ValueParameter<DoubleValue>("Phi_P", "Weight for particle's personal best position.", new DoubleValue(-0.01)));
|
---|
118 | Parameters.Add(new ValueParameter<DoubleValue>("Phi_G", "Weight for global best position.", new DoubleValue(3.7)));
|
---|
119 | Parameters.Add(new ValueLookupParameter<DoubleMatrix>("VelocityBounds", "Maximum Velocity in every dimension", new DoubleMatrix(new double[,] { { -1, 1 } })));
|
---|
120 | Parameters.Add(new ConstrainedValueParameter<IParticleUpdater>("ParticleUpdater", "Operator that calculates new position and velocity of a particle",
|
---|
121 | new ItemSet<IParticleUpdater>(ApplicationManager.Manager.GetInstances<IParticleUpdater>())));
|
---|
122 | Parameters.Add(new ConstrainedValueParameter<ITopologyInitializer>("TopologyInitializer", "Creates neighborhood description vectors",
|
---|
123 | new ItemSet<ITopologyInitializer>(ApplicationManager.Manager.GetInstances<ITopologyInitializer>())));
|
---|
124 | Parameters.Add(new ConstrainedValueParameter<ITopologyUpdater>("TopologyUpdater", "Updates the neighborhood description vectors",
|
---|
125 | new ItemSet<ITopologyUpdater>(ApplicationManager.Manager.GetInstances<ITopologyUpdater>())));
|
---|
126 | ParticleUpdaterParameter.ActualValue = ParticleUpdaterParameter.ValidValues.SingleOrDefault(v => v.GetType() == typeof(TotallyConnectedParticleUpdater));
|
---|
127 | TopologyInitializerParameter.ActualValue = TopologyInitializerParameter.ValidValues.SingleOrDefault(v => v.GetType() == typeof(EmptyTopologyInitializer));
|
---|
128 | TopologyUpdaterParameter.ActualValue = TopologyUpdaterParameter.ValidValues.SingleOrDefault(v => v.GetType() == typeof(IdentityTopologyUpdater));
|
---|
129 |
|
---|
130 | RandomCreator randomCreator = new RandomCreator();
|
---|
131 | VariableCreator variableCreator = new VariableCreator();
|
---|
132 | SolutionsCreator solutionsCreator = new SolutionsCreator();
|
---|
133 | CombinedOperator particleCreator = new CombinedOperator();
|
---|
134 | Placeholder evaluatorPlaceholder = new Placeholder();
|
---|
135 | Assigner bestPersonalQualityAssigner = new Assigner();
|
---|
136 | BestPointInitializer bestPositionInitializer = new BestPointInitializer();
|
---|
137 | Placeholder topologyInitializerPlaceholder = new Placeholder();
|
---|
138 | NeighborUpdater neighborUpdater = new NeighborUpdater();
|
---|
139 | Placeholder analyzerPlaceholder = new Placeholder();
|
---|
140 | UniformSubScopesProcessor uniformSubScopeProcessor = new UniformSubScopesProcessor();
|
---|
141 | Placeholder particleUpdaterPlaceholder = new Placeholder();
|
---|
142 | Placeholder topologyUpdaterPlaceholder = new Placeholder();
|
---|
143 | UniformSubScopesProcessor uniformSubscopesProcessor2 = new UniformSubScopesProcessor();
|
---|
144 | NeighborUpdater neighborUpdater2 = new NeighborUpdater();
|
---|
145 | Placeholder evaluatorPlaceholder2 = new Placeholder();
|
---|
146 | SwarmUpdater swarmUpdater = new SwarmUpdater();
|
---|
147 | Placeholder analyzerPlaceholder2 = new Placeholder();
|
---|
148 | IntCounter currentIterationCounter = new IntCounter();
|
---|
149 | Comparator currentIterationComparator = new Comparator();
|
---|
150 | ConditionalBranch conditionalBranch = new ConditionalBranch();
|
---|
151 |
|
---|
152 | OperatorGraph.InitialOperator = randomCreator;
|
---|
153 |
|
---|
154 | randomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
155 | randomCreator.SeedParameter.Value = null;
|
---|
156 | randomCreator.Successor = variableCreator;
|
---|
157 |
|
---|
158 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("CurrentIteration", new IntValue(0)));
|
---|
159 | variableCreator.Successor = solutionsCreator;
|
---|
160 |
|
---|
161 | solutionsCreator.NumberOfSolutionsParameter.ActualName = "SwarmSize";
|
---|
162 | solutionsCreator.EvaluatorParameter.Value = evaluatorPlaceholder;
|
---|
163 | solutionsCreator.SolutionCreatorParameter.Value = particleCreator;
|
---|
164 | solutionsCreator.Successor = bestPositionInitializer;
|
---|
165 |
|
---|
166 | InitializeParticleCreator(particleCreator);
|
---|
167 |
|
---|
168 | evaluatorPlaceholder.Name = "(Evaluator)";
|
---|
169 | evaluatorPlaceholder.OperatorParameter.ActualName = "Evaluator";
|
---|
170 | evaluatorPlaceholder.Successor = bestPersonalQualityAssigner;
|
---|
171 |
|
---|
172 | bestPersonalQualityAssigner.LeftSideParameter.ActualName = "PersonalBestQuality";
|
---|
173 | bestPersonalQualityAssigner.RightSideParameter.ActualName = "Quality";
|
---|
174 |
|
---|
175 | bestPositionInitializer.Successor = topologyInitializerPlaceholder;
|
---|
176 |
|
---|
177 | topologyInitializerPlaceholder.Name = "(TopologyInitializer)";
|
---|
178 | topologyInitializerPlaceholder.OperatorParameter.ActualName = "TopologyInitializer";
|
---|
179 | topologyInitializerPlaceholder.Successor = neighborUpdater;
|
---|
180 |
|
---|
181 | neighborUpdater.Successor = analyzerPlaceholder;
|
---|
182 |
|
---|
183 | analyzerPlaceholder.Name = "(Analyzer)";
|
---|
184 | analyzerPlaceholder.OperatorParameter.ActualName = "Analyzer";
|
---|
185 | analyzerPlaceholder.Successor = uniformSubScopeProcessor;
|
---|
186 |
|
---|
187 | uniformSubScopeProcessor.Operator = particleUpdaterPlaceholder;
|
---|
188 | uniformSubScopeProcessor.Successor = topologyUpdaterPlaceholder;
|
---|
189 |
|
---|
190 | particleUpdaterPlaceholder.Name = "(ParticleUpdater)";
|
---|
191 | particleUpdaterPlaceholder.OperatorParameter.ActualName = "ParticleUpdater";
|
---|
192 | particleUpdaterPlaceholder.Successor = evaluatorPlaceholder2;
|
---|
193 |
|
---|
194 | evaluatorPlaceholder2.Name = "(Evaluator)";
|
---|
195 | evaluatorPlaceholder2.OperatorParameter.ActualName = "Evaluator";
|
---|
196 |
|
---|
197 | topologyUpdaterPlaceholder.Name = "(TopologyUpdater)";
|
---|
198 | topologyUpdaterPlaceholder.OperatorParameter.ActualName = "TopologyUpdater";
|
---|
199 | topologyUpdaterPlaceholder.Successor = neighborUpdater2;
|
---|
200 |
|
---|
201 | neighborUpdater2.Successor = uniformSubscopesProcessor2;
|
---|
202 |
|
---|
203 | uniformSubscopesProcessor2.Operator = swarmUpdater;
|
---|
204 | uniformSubscopesProcessor2.Successor = analyzerPlaceholder2;
|
---|
205 |
|
---|
206 | analyzerPlaceholder2.Name = "(Analyzer)";
|
---|
207 | analyzerPlaceholder2.OperatorParameter.ActualName = "Analyzer";
|
---|
208 | analyzerPlaceholder2.Successor = currentIterationCounter;
|
---|
209 |
|
---|
210 | currentIterationCounter.Name = "CurrentIteration++";
|
---|
211 | currentIterationCounter.ValueParameter.ActualName = "CurrentIteration";
|
---|
212 | currentIterationCounter.Successor = currentIterationComparator;
|
---|
213 |
|
---|
214 | currentIterationComparator.LeftSideParameter.ActualName = "CurrentIteration";
|
---|
215 | currentIterationComparator.Comparison = new Comparison(ComparisonType.Less);
|
---|
216 | currentIterationComparator.RightSideParameter.ActualName = "MaxIterations";
|
---|
217 | currentIterationComparator.ResultParameter.ActualName = "ContinueIteration";
|
---|
218 | currentIterationComparator.Successor = conditionalBranch;
|
---|
219 |
|
---|
220 | conditionalBranch.Name = "ContinueIteration?";
|
---|
221 | conditionalBranch.ConditionParameter.ActualName = "ContinueIteration";
|
---|
222 | conditionalBranch.TrueBranch = uniformSubScopeProcessor;
|
---|
223 |
|
---|
224 | InitializeAnalyzers();
|
---|
225 | UpdateAnalyzers();
|
---|
226 | }
|
---|
227 |
|
---|
228 | private static void InitializeParticleCreator(CombinedOperator particleCreator) {
|
---|
229 | Placeholder positionCreator = new Placeholder();
|
---|
230 | Assigner personalBestPositionAssigner = new Assigner();
|
---|
231 | UniformRandomRealVectorCreator velocityCreator = new UniformRandomRealVectorCreator();
|
---|
232 |
|
---|
233 | particleCreator.Name = "Particle Creator";
|
---|
234 | particleCreator.OperatorGraph.InitialOperator = positionCreator;
|
---|
235 |
|
---|
236 | positionCreator.Name = "(SolutionCreator)";
|
---|
237 | positionCreator.OperatorParameter.ActualName = "SolutionCreator";
|
---|
238 | positionCreator.Successor = personalBestPositionAssigner;
|
---|
239 |
|
---|
240 | personalBestPositionAssigner.LeftSideParameter.ActualName = "PersonalBestPoint";
|
---|
241 | personalBestPositionAssigner.RightSideParameter.ActualName = "Point";
|
---|
242 | personalBestPositionAssigner.Successor = velocityCreator;
|
---|
243 |
|
---|
244 | velocityCreator.LengthParameter.ActualName = "ProblemSize";
|
---|
245 | velocityCreator.BoundsParameter.ActualName = "VelocityBounds";
|
---|
246 | velocityCreator.RealVectorParameter.ActualName = "Velocity";
|
---|
247 | }
|
---|
248 |
|
---|
249 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
250 | return new ParticleSwarmOptimization(this, cloner);
|
---|
251 | }
|
---|
252 |
|
---|
253 | public override void Prepare() {
|
---|
254 | if (Problem != null) base.Prepare();
|
---|
255 | }
|
---|
256 |
|
---|
257 | #region Events
|
---|
258 | protected override void OnProblemChanged() {
|
---|
259 | UpdateAnalyzers();
|
---|
260 | ParameterizeAnalyzers();
|
---|
261 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
262 | base.OnProblemChanged();
|
---|
263 | }
|
---|
264 |
|
---|
265 | #endregion
|
---|
266 |
|
---|
267 | #region Helpers
|
---|
268 | private void InitializeParticleUpdaters() {
|
---|
269 | }
|
---|
270 |
|
---|
271 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
272 | }
|
---|
273 |
|
---|
274 | private void InitializeAnalyzers() {
|
---|
275 | qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
276 | ParameterizeAnalyzers();
|
---|
277 | }
|
---|
278 |
|
---|
279 | private void ParameterizeAnalyzers() {
|
---|
280 | if (Problem != null) {
|
---|
281 | qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
282 | qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
283 | qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
284 | }
|
---|
285 | }
|
---|
286 |
|
---|
287 | private void UpdateAnalyzers() {
|
---|
288 | Analyzer.Operators.Clear();
|
---|
289 | if (Problem != null) {
|
---|
290 | foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>())
|
---|
291 | Analyzer.Operators.Add(analyzer);
|
---|
292 | }
|
---|
293 | Analyzer.Operators.Add(qualityAnalyzer);
|
---|
294 | }
|
---|
295 |
|
---|
296 | #endregion
|
---|
297 | }
|
---|
298 | }
|
---|