[3348] | 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;
|
---|
[4068] | 24 | using HeuristicLab.Analysis;
|
---|
[3376] | 25 | using HeuristicLab.Common;
|
---|
[3348] | 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
[4068] | 29 | using HeuristicLab.Operators;
|
---|
[3348] | 30 | using HeuristicLab.Optimization;
|
---|
| 31 | using HeuristicLab.Optimization.Operators;
|
---|
| 32 | using HeuristicLab.Parameters;
|
---|
| 33 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[3368] | 34 | using HeuristicLab.Random;
|
---|
[3348] | 35 |
|
---|
| 36 | namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
|
---|
[5033] | 37 |
|
---|
[3348] | 38 | [Item("Particle Swarm Optimization", "A particle swarm optimization algorithm.")]
|
---|
| 39 | [Creatable("Algorithms")]
|
---|
| 40 | [StorableClass]
|
---|
[5033] | 41 | public class ParticleSwarmOptimization : EngineAlgorithm {
|
---|
| 42 |
|
---|
[3348] | 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 | }
|
---|
[3682] | 51 | public MultiAnalyzer Analyzer {
|
---|
| 52 | get { return AnalyzerParameter.Value; }
|
---|
| 53 | set { AnalyzerParameter.Value = value; }
|
---|
| 54 | }
|
---|
[4068] | 55 | #endregion
|
---|
[3348] | 56 |
|
---|
| 57 | #region Parameter Properties
|
---|
[5033] | 58 | public ValueParameter<IntValue> SeedParameter {
|
---|
[3348] | 59 | get { return (ValueParameter<IntValue>)Parameters["Seed"]; }
|
---|
| 60 | }
|
---|
[5033] | 61 | public ValueParameter<BoolValue> SetSeedRandomlyParameter {
|
---|
[3348] | 62 | get { return (ValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
|
---|
| 63 | }
|
---|
[5033] | 64 | public ValueParameter<IntValue> SwarmSizeParameter {
|
---|
[3348] | 65 | get { return (ValueParameter<IntValue>)Parameters["SwarmSize"]; }
|
---|
| 66 | }
|
---|
[5033] | 67 | public ValueParameter<IntValue> MaxIterationsParameter {
|
---|
[3348] | 68 | get { return (ValueParameter<IntValue>)Parameters["MaxIterations"]; }
|
---|
| 69 | }
|
---|
[5033] | 70 | public ValueParameter<DoubleValue> OmegaParameter {
|
---|
| 71 | get { return (ValueParameter<DoubleValue>)Parameters["Omega"]; }
|
---|
[3348] | 72 | }
|
---|
[5033] | 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 {
|
---|
[3682] | 80 | get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
|
---|
| 81 | }
|
---|
[5033] | 82 | public ValueLookupParameter<DoubleMatrix> VelocityBoundsParameter {
|
---|
| 83 | get { return (ValueLookupParameter<DoubleMatrix>)Parameters["VelocityBounds"]; }
|
---|
| 84 | }
|
---|
[3348] | 85 | #endregion
|
---|
| 86 |
|
---|
| 87 | #region Properties
|
---|
[3719] | 88 | [Storable]
|
---|
[3682] | 89 | private BestAverageWorstQualityAnalyzer qualityAnalyzer;
|
---|
[3348] | 90 | #endregion
|
---|
| 91 |
|
---|
[5033] | 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 |
|
---|
[3348] | 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)));
|
---|
[3724] | 103 | Parameters.Add(new ValueParameter<IntValue>("SwarmSize", "Size of the particle swarm.", new IntValue(10)));
|
---|
[3348] | 104 | Parameters.Add(new ValueParameter<IntValue>("MaxIterations", "Maximal number of iterations.", new IntValue(1000)));
|
---|
[3682] | 105 | Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each generation.", new MultiAnalyzer()));
|
---|
[5033] | 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 |
|
---|
[3348] | 111 | RandomCreator randomCreator = new RandomCreator();
|
---|
[5033] | 112 | VariableCreator variableCreator = new VariableCreator();
|
---|
[3348] | 113 | SolutionsCreator solutionsCreator = new SolutionsCreator();
|
---|
[5033] | 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();
|
---|
[3348] | 127 |
|
---|
[5033] | 128 | OperatorGraph.InitialOperator = randomCreator;
|
---|
[3682] | 129 |
|
---|
[5033] | 130 | randomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
[3348] | 131 | randomCreator.SeedParameter.Value = null;
|
---|
[5033] | 132 | randomCreator.Successor = variableCreator;
|
---|
[4068] | 133 |
|
---|
[5033] | 134 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("CurrentIteration", new IntValue(0)));
|
---|
| 135 | variableCreator.Successor = solutionsCreator;
|
---|
[4068] | 136 |
|
---|
[5033] | 137 | solutionsCreator.NumberOfSolutionsParameter.ActualName = "SwarmSize";
|
---|
| 138 | solutionsCreator.EvaluatorParameter.Value = evaluatorPlaceholder;
|
---|
| 139 | solutionsCreator.SolutionCreatorParameter.Value = particleCreator;
|
---|
| 140 | solutionsCreator.Successor = bestPositionInitializer;
|
---|
[4068] | 141 |
|
---|
[5033] | 142 | InitializeParticleCreator(particleCreator);
|
---|
[3348] | 143 |
|
---|
[5033] | 144 | evaluatorPlaceholder.Name = "(Evaluator)";
|
---|
| 145 | evaluatorPlaceholder.OperatorParameter.ActualName = "Evaluator";
|
---|
| 146 | evaluatorPlaceholder.Successor = bestPersonalQualityAssigner;
|
---|
[3348] | 147 |
|
---|
[5033] | 148 | bestPersonalQualityAssigner.LeftSideParameter.ActualName = "PersonalBestQuality";
|
---|
| 149 | bestPersonalQualityAssigner.RightSideParameter.ActualName = "Quality";
|
---|
[3682] | 150 |
|
---|
[5033] | 151 | bestPositionInitializer.Successor = analyzerPlaceholder;
|
---|
[3682] | 152 |
|
---|
[5033] | 153 | analyzerPlaceholder.Name = "(Analyzer)";
|
---|
| 154 | analyzerPlaceholder.OperatorParameter.ActualName = "Analyzer";
|
---|
| 155 | analyzerPlaceholder.Successor = uniformSubScopeProcessor;
|
---|
[3682] | 156 |
|
---|
[5033] | 157 | uniformSubScopeProcessor.Operator = particleUpdater;
|
---|
| 158 | uniformSubScopeProcessor.Successor = analyzerPlaceholder2;
|
---|
[3682] | 159 |
|
---|
[5033] | 160 | particleUpdater.Successor = evaluatorPlaceholder2;
|
---|
[3682] | 161 |
|
---|
[5033] | 162 | evaluatorPlaceholder2.Name = "(Evaluator)";
|
---|
| 163 | evaluatorPlaceholder2.OperatorParameter.ActualName = "Evaluator";
|
---|
| 164 | evaluatorPlaceholder2.Successor = swarmUpdater;
|
---|
[3682] | 165 |
|
---|
[5033] | 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 |
|
---|
[3719] | 184 | InitializeAnalyzers();
|
---|
| 185 | UpdateAnalyzers();
|
---|
[3348] | 186 | }
|
---|
| 187 |
|
---|
[5033] | 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";
|
---|
[3348] | 207 | }
|
---|
| 208 |
|
---|
| 209 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[5033] | 210 | return new ParticleSwarmOptimization(this, cloner);
|
---|
[3348] | 211 | }
|
---|
| 212 |
|
---|
| 213 | public override void Prepare() {
|
---|
| 214 | if (Problem != null) base.Prepare();
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | #region Events
|
---|
| 218 | protected override void OnProblemChanged() {
|
---|
[3682] | 219 | UpdateAnalyzers();
|
---|
[4068] | 220 | ParameterizeAnalyzers();
|
---|
[3682] | 221 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
[3348] | 222 | base.OnProblemChanged();
|
---|
| 223 | }
|
---|
[3415] | 224 |
|
---|
[3348] | 225 | #endregion
|
---|
| 226 |
|
---|
| 227 | #region Helpers
|
---|
[3682] | 228 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
| 229 | }
|
---|
[3348] | 230 |
|
---|
[3682] | 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;
|
---|
[3348] | 241 | }
|
---|
| 242 | }
|
---|
[3415] | 243 |
|
---|
[3682] | 244 | private void UpdateAnalyzers() {
|
---|
| 245 | Analyzer.Operators.Clear();
|
---|
| 246 | if (Problem != null) {
|
---|
[3816] | 247 | foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>())
|
---|
[3682] | 248 | Analyzer.Operators.Add(analyzer);
|
---|
| 249 | }
|
---|
[3799] | 250 | Analyzer.Operators.Add(qualityAnalyzer);
|
---|
[3682] | 251 | }
|
---|
| 252 |
|
---|
[3348] | 253 | #endregion
|
---|
| 254 | }
|
---|
| 255 | }
|
---|