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.Random;
|
---|
35 |
|
---|
36 | namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
|
---|
37 |
|
---|
38 | [Item("Particle Swarm Optimization", "A particle swarm optimization algorithm.")]
|
---|
39 | [Creatable("Algorithms")]
|
---|
40 | [StorableClass]
|
---|
41 | public class ParticleSwarmOptimization : EngineAlgorithm {
|
---|
42 |
|
---|
43 | #region Problem Properties
|
---|
44 | public override Type ProblemType {
|
---|
45 | get { return typeof(ISingleObjectiveProblem); }
|
---|
46 | }
|
---|
47 | public new ISingleObjectiveProblem Problem {
|
---|
48 | get { return (ISingleObjectiveProblem)base.Problem; }
|
---|
49 | set { base.Problem = value; }
|
---|
50 | }
|
---|
51 | public MultiAnalyzer Analyzer {
|
---|
52 | get { return AnalyzerParameter.Value; }
|
---|
53 | set { AnalyzerParameter.Value = value; }
|
---|
54 | }
|
---|
55 | #endregion
|
---|
56 |
|
---|
57 | #region Parameter Properties
|
---|
58 | public ValueParameter<IntValue> SeedParameter {
|
---|
59 | get { return (ValueParameter<IntValue>)Parameters["Seed"]; }
|
---|
60 | }
|
---|
61 | public ValueParameter<BoolValue> SetSeedRandomlyParameter {
|
---|
62 | get { return (ValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
|
---|
63 | }
|
---|
64 | public ValueParameter<IntValue> SwarmSizeParameter {
|
---|
65 | get { return (ValueParameter<IntValue>)Parameters["SwarmSize"]; }
|
---|
66 | }
|
---|
67 | public ValueParameter<IntValue> MaxIterationsParameter {
|
---|
68 | get { return (ValueParameter<IntValue>)Parameters["MaxIterations"]; }
|
---|
69 | }
|
---|
70 | public ValueParameter<DoubleValue> OmegaParameter {
|
---|
71 | get { return (ValueParameter<DoubleValue>)Parameters["Omega"]; }
|
---|
72 | }
|
---|
73 | public ValueParameter<DoubleValue> Phi_PParameter {
|
---|
74 | get { return (ValueParameter<DoubleValue>)Parameters["Phi_P"]; }
|
---|
75 | }
|
---|
76 | public ValueParameter<DoubleValue> Phi_GParameter {
|
---|
77 | get { return (ValueParameter<DoubleValue>)Parameters["Phi_G"]; }
|
---|
78 | }
|
---|
79 | public ValueParameter<MultiAnalyzer> AnalyzerParameter {
|
---|
80 | get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
|
---|
81 | }
|
---|
82 | public ValueLookupParameter<DoubleMatrix> VelocityBoundsParameter {
|
---|
83 | get { return (ValueLookupParameter<DoubleMatrix>)Parameters["VelocityBounds"]; }
|
---|
84 | }
|
---|
85 | #endregion
|
---|
86 |
|
---|
87 | #region Properties
|
---|
88 | [Storable]
|
---|
89 | private BestAverageWorstQualityAnalyzer qualityAnalyzer;
|
---|
90 | #endregion
|
---|
91 |
|
---|
92 | [StorableConstructor]
|
---|
93 | protected ParticleSwarmOptimization(bool deserializing) : base(deserializing) { }
|
---|
94 | protected ParticleSwarmOptimization(ParticleSwarmOptimization original, Cloner cloner)
|
---|
95 | : base(original, cloner) {
|
---|
96 | qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
|
---|
97 | }
|
---|
98 |
|
---|
99 | public ParticleSwarmOptimization()
|
---|
100 | : base() {
|
---|
101 | Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
102 | Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
103 | Parameters.Add(new ValueParameter<IntValue>("SwarmSize", "Size of the particle swarm.", new IntValue(10)));
|
---|
104 | Parameters.Add(new ValueParameter<IntValue>("MaxIterations", "Maximal number of iterations.", new IntValue(1000)));
|
---|
105 | Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each generation.", new MultiAnalyzer()));
|
---|
106 | Parameters.Add(new ValueParameter<DoubleValue>("Omega", "Weight for particle's velocity vector.", new DoubleValue(-0.2)));
|
---|
107 | Parameters.Add(new ValueParameter<DoubleValue>("Phi_P", "Weight for particle's personal best position.", new DoubleValue(-0.01)));
|
---|
108 | Parameters.Add(new ValueParameter<DoubleValue>("Phi_G", "Weight for global best position.", new DoubleValue(3.7)));
|
---|
109 | Parameters.Add(new ValueLookupParameter<DoubleMatrix>("VelocityBounds", "Maximum Velocity in every dimension", new DoubleMatrix(new double[,] { { -1, 1 } })));
|
---|
110 |
|
---|
111 | RandomCreator randomCreator = new RandomCreator();
|
---|
112 | VariableCreator variableCreator = new VariableCreator();
|
---|
113 | SolutionsCreator solutionsCreator = new SolutionsCreator();
|
---|
114 | CombinedOperator particleCreator = new CombinedOperator();
|
---|
115 | Placeholder evaluatorPlaceholder = new Placeholder();
|
---|
116 | Assigner bestPersonalQualityAssigner = new Assigner();
|
---|
117 | BestPointInitializer bestPositionInitializer = new BestPointInitializer();
|
---|
118 | Placeholder analyzerPlaceholder = new Placeholder();
|
---|
119 | UniformSubScopesProcessor uniformSubScopeProcessor = new UniformSubScopesProcessor();
|
---|
120 | ParticleUpdater particleUpdater = new ParticleUpdater();
|
---|
121 | Placeholder evaluatorPlaceholder2 = new Placeholder();
|
---|
122 | SwarmUpdater swarmUpdater = new SwarmUpdater();
|
---|
123 | Placeholder analyzerPlaceholder2 = new Placeholder();
|
---|
124 | IntCounter currentIterationCounter = new IntCounter();
|
---|
125 | Comparator currentIterationComparator = new Comparator();
|
---|
126 | ConditionalBranch conditionalBranch = new ConditionalBranch();
|
---|
127 |
|
---|
128 | OperatorGraph.InitialOperator = randomCreator;
|
---|
129 |
|
---|
130 | randomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
131 | randomCreator.SeedParameter.Value = null;
|
---|
132 | randomCreator.Successor = variableCreator;
|
---|
133 |
|
---|
134 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("CurrentIteration", new IntValue(0)));
|
---|
135 | variableCreator.Successor = solutionsCreator;
|
---|
136 |
|
---|
137 | solutionsCreator.NumberOfSolutionsParameter.ActualName = "SwarmSize";
|
---|
138 | solutionsCreator.EvaluatorParameter.Value = evaluatorPlaceholder;
|
---|
139 | solutionsCreator.SolutionCreatorParameter.Value = particleCreator;
|
---|
140 | solutionsCreator.Successor = bestPositionInitializer;
|
---|
141 |
|
---|
142 | InitializeParticleCreator(particleCreator);
|
---|
143 |
|
---|
144 | evaluatorPlaceholder.Name = "(Evaluator)";
|
---|
145 | evaluatorPlaceholder.OperatorParameter.ActualName = "Evaluator";
|
---|
146 | evaluatorPlaceholder.Successor = bestPersonalQualityAssigner;
|
---|
147 |
|
---|
148 | bestPersonalQualityAssigner.LeftSideParameter.ActualName = "PersonalBestQuality";
|
---|
149 | bestPersonalQualityAssigner.RightSideParameter.ActualName = "Quality";
|
---|
150 |
|
---|
151 | bestPositionInitializer.Successor = analyzerPlaceholder;
|
---|
152 |
|
---|
153 | analyzerPlaceholder.Name = "(Analyzer)";
|
---|
154 | analyzerPlaceholder.OperatorParameter.ActualName = "Analyzer";
|
---|
155 | analyzerPlaceholder.Successor = uniformSubScopeProcessor;
|
---|
156 |
|
---|
157 | uniformSubScopeProcessor.Operator = particleUpdater;
|
---|
158 | uniformSubScopeProcessor.Successor = analyzerPlaceholder2;
|
---|
159 |
|
---|
160 | particleUpdater.Successor = evaluatorPlaceholder2;
|
---|
161 |
|
---|
162 | evaluatorPlaceholder2.Name = "(Evaluator)";
|
---|
163 | evaluatorPlaceholder2.OperatorParameter.ActualName = "Evaluator";
|
---|
164 | evaluatorPlaceholder2.Successor = swarmUpdater;
|
---|
165 |
|
---|
166 | analyzerPlaceholder2.Name = "(Analyzer)";
|
---|
167 | analyzerPlaceholder2.OperatorParameter.ActualName = "Analyzer";
|
---|
168 | analyzerPlaceholder2.Successor = currentIterationCounter;
|
---|
169 |
|
---|
170 | currentIterationCounter.Name = "CurrentIteration++";
|
---|
171 | currentIterationCounter.ValueParameter.ActualName = "CurrentIteration";
|
---|
172 | currentIterationCounter.Successor = currentIterationComparator;
|
---|
173 |
|
---|
174 | currentIterationComparator.LeftSideParameter.ActualName = "CurrentIteration";
|
---|
175 | currentIterationComparator.Comparison = new Comparison(ComparisonType.Less);
|
---|
176 | currentIterationComparator.RightSideParameter.ActualName = "MaxIterations";
|
---|
177 | currentIterationComparator.ResultParameter.ActualName = "ContinueIteration";
|
---|
178 | currentIterationComparator.Successor = conditionalBranch;
|
---|
179 |
|
---|
180 | conditionalBranch.Name = "ContinueIteration?";
|
---|
181 | conditionalBranch.ConditionParameter.ActualName = "ContinueIteration";
|
---|
182 | conditionalBranch.TrueBranch = uniformSubScopeProcessor;
|
---|
183 |
|
---|
184 | InitializeAnalyzers();
|
---|
185 | UpdateAnalyzers();
|
---|
186 | }
|
---|
187 |
|
---|
188 | private static void InitializeParticleCreator(CombinedOperator particleCreator) {
|
---|
189 | Placeholder positionCreator = new Placeholder();
|
---|
190 | Assigner personalBestPositionAssigner = new Assigner();
|
---|
191 | UniformRandomRealVectorCreator velocityCreator = new UniformRandomRealVectorCreator();
|
---|
192 |
|
---|
193 | particleCreator.Name = "Particle Creator";
|
---|
194 | particleCreator.OperatorGraph.InitialOperator = positionCreator;
|
---|
195 |
|
---|
196 | positionCreator.Name = "(SolutionCreator)";
|
---|
197 | positionCreator.OperatorParameter.ActualName = "SolutionCreator";
|
---|
198 | positionCreator.Successor = personalBestPositionAssigner;
|
---|
199 |
|
---|
200 | personalBestPositionAssigner.LeftSideParameter.ActualName = "PersonalBestPoint";
|
---|
201 | personalBestPositionAssigner.RightSideParameter.ActualName = "Point";
|
---|
202 | personalBestPositionAssigner.Successor = velocityCreator;
|
---|
203 |
|
---|
204 | velocityCreator.LengthParameter.ActualName = "ProblemSize";
|
---|
205 | velocityCreator.BoundsParameter.ActualName = "VelocityBounds";
|
---|
206 | velocityCreator.RealVectorParameter.ActualName = "Velocity";
|
---|
207 | }
|
---|
208 |
|
---|
209 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
210 | return new ParticleSwarmOptimization(this, cloner);
|
---|
211 | }
|
---|
212 |
|
---|
213 | public override void Prepare() {
|
---|
214 | if (Problem != null) base.Prepare();
|
---|
215 | }
|
---|
216 |
|
---|
217 | #region Events
|
---|
218 | protected override void OnProblemChanged() {
|
---|
219 | UpdateAnalyzers();
|
---|
220 | ParameterizeAnalyzers();
|
---|
221 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
222 | base.OnProblemChanged();
|
---|
223 | }
|
---|
224 |
|
---|
225 | #endregion
|
---|
226 |
|
---|
227 | #region Helpers
|
---|
228 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
229 | }
|
---|
230 |
|
---|
231 | private void InitializeAnalyzers() {
|
---|
232 | qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
233 | ParameterizeAnalyzers();
|
---|
234 | }
|
---|
235 |
|
---|
236 | private void ParameterizeAnalyzers() {
|
---|
237 | if (Problem != null) {
|
---|
238 | qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
239 | qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
240 | qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
241 | }
|
---|
242 | }
|
---|
243 |
|
---|
244 | private void UpdateAnalyzers() {
|
---|
245 | Analyzer.Operators.Clear();
|
---|
246 | if (Problem != null) {
|
---|
247 | foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>())
|
---|
248 | Analyzer.Operators.Add(analyzer);
|
---|
249 | }
|
---|
250 | Analyzer.Operators.Add(qualityAnalyzer);
|
---|
251 | }
|
---|
252 |
|
---|
253 | #endregion
|
---|
254 | }
|
---|
255 | }
|
---|