[5560] | 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;
|
---|
[5568] | 25 | using HeuristicLab.Operators;
|
---|
[5560] | 26 | using HeuristicLab.Optimization;
|
---|
| 27 | using HeuristicLab.Optimization.Operators;
|
---|
[5568] | 28 | using HeuristicLab.Parameters;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[5560] | 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 {
|
---|
[5592] | 35 |
|
---|
[5560] | 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 {
|
---|
[5645] | 50 | get { return (IValueLookupParameter<DoubleValue>)Parameters["CurrentInertia"]; }
|
---|
[5560] | 51 | }
|
---|
| 52 | public IValueLookupParameter<DoubleValue> PersonalBestAttractionParameter {
|
---|
| 53 | get { return (IValueLookupParameter<DoubleValue>)Parameters["PersonalBestAttraction"]; }
|
---|
| 54 | }
|
---|
[5568] | 55 | public IValueLookupParameter<DoubleValue> NeighborBestAttractionParameter {
|
---|
| 56 | get { return (IValueLookupParameter<DoubleValue>)Parameters["NeighborBestAttraction"]; }
|
---|
[5560] | 57 | }
|
---|
| 58 | public IValueLookupParameter<DoubleMatrix> VelocityBoundsParameter {
|
---|
| 59 | get { return (IValueLookupParameter<DoubleMatrix>)Parameters["VelocityBounds"]; }
|
---|
[5568] | 60 | }
|
---|
[5560] | 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 | }
|
---|
[5568] | 70 | public IValueLookupParameter<ResultCollection> ResultsParameter {
|
---|
| 71 | get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
[5560] | 72 | }
|
---|
[5643] | 73 | public LookupParameter<IntValue> EvaluatedSolutionsParameter {
|
---|
| 74 | get { return (LookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
|
---|
| 75 | }
|
---|
[5560] | 76 | public IValueLookupParameter<IOperator> EvaluatorParameter {
|
---|
| 77 | get { return (IValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
|
---|
| 78 | }
|
---|
[5568] | 79 | public ValueLookupParameter<ISwarmUpdater> SwarmUpdaterParameter {
|
---|
| 80 | get { return (ValueLookupParameter<ISwarmUpdater>)Parameters["SwarmUpdater"]; }
|
---|
| 81 | }
|
---|
[5560] | 82 | #endregion
|
---|
[5568] | 83 |
|
---|
[5560] | 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."));
|
---|
[5592] | 102 | Parameters.Add(new ValueLookupParameter<IntValue>("SwarmSize", "Size of the particle swarm."));
|
---|
| 103 | Parameters.Add(new ValueLookupParameter<IntValue>("MaxIterations", "Maximal number of iterations."));
|
---|
[5568] | 104 |
|
---|
[5560] | 105 | Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
|
---|
| 106 |
|
---|
[5645] | 107 | Parameters.Add(new ValueLookupParameter<DoubleValue>("CurrentInertia", "Inertia weight on a particle's movement (omega)."));
|
---|
[5560] | 108 | Parameters.Add(new ValueLookupParameter<DoubleValue>("PersonalBestAttraction", "Weight for particle's pull towards its personal best soution (phi_p)."));
|
---|
[5568] | 109 | 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)."));
|
---|
| 110 |
|
---|
[5560] | 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 |
|
---|
[5568] | 116 | Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The variable collection where results should be stored."));
|
---|
[5643] | 117 | Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of times solutions have been evaluated."));
|
---|
[5568] | 118 |
|
---|
| 119 | Parameters.Add(new ValueLookupParameter<ISwarmUpdater>("SwarmUpdater", "The encoding-specific swarm updater."));
|
---|
[5560] | 120 | #endregion
|
---|
| 121 |
|
---|
| 122 | #region Create operators
|
---|
[5568] | 123 | ResultsCollector resultsCollector = new ResultsCollector();
|
---|
| 124 | Placeholder swarmUpdaterPlaceholer1 = new Placeholder();
|
---|
[5560] | 125 | Placeholder evaluatorPlaceholder = new Placeholder();
|
---|
| 126 | Placeholder analyzerPlaceholder = new Placeholder();
|
---|
| 127 | UniformSubScopesProcessor uniformSubScopeProcessor = new UniformSubScopesProcessor();
|
---|
| 128 | Placeholder particleUpdaterPlaceholder = new Placeholder();
|
---|
| 129 | Placeholder topologyUpdaterPlaceholder = new Placeholder();
|
---|
| 130 | UniformSubScopesProcessor uniformSubscopesProcessor2 = new UniformSubScopesProcessor();
|
---|
| 131 | UniformSubScopesProcessor evaluationProcessor = new UniformSubScopesProcessor();
|
---|
[5566] | 132 | Placeholder swarmUpdater = new Placeholder();
|
---|
[5560] | 133 | IntCounter currentIterationCounter = new IntCounter();
|
---|
| 134 | Comparator currentIterationComparator = new Comparator();
|
---|
| 135 | ConditionalBranch conditionalBranch = new ConditionalBranch();
|
---|
| 136 | Placeholder velocityBoundsUpdaterPlaceholder = new Placeholder();
|
---|
[5566] | 137 | Placeholder inertiaUpdaterPlaceholder = new Placeholder();
|
---|
[5643] | 138 | SubScopesCounter subScopesCounter = new SubScopesCounter();
|
---|
[5560] | 139 | #endregion
|
---|
| 140 |
|
---|
| 141 | #region Create operator graph
|
---|
[5568] | 142 | OperatorGraph.InitialOperator = resultsCollector;
|
---|
[5645] | 143 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
|
---|
| 144 | resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("CurrentInertia"));
|
---|
[5643] | 145 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
|
---|
[5568] | 146 | resultsCollector.ResultsParameter.ActualName = "Results";
|
---|
| 147 | resultsCollector.Successor = swarmUpdaterPlaceholer1;
|
---|
[5560] | 148 |
|
---|
[5568] | 149 | swarmUpdaterPlaceholer1.Name = "(Swarm Updater)";
|
---|
| 150 | swarmUpdaterPlaceholer1.OperatorParameter.ActualName = SwarmUpdaterParameter.ActualName;
|
---|
| 151 | swarmUpdaterPlaceholer1.Successor = analyzerPlaceholder;
|
---|
| 152 |
|
---|
[5566] | 153 | analyzerPlaceholder.Name = "(Analyzer)";
|
---|
[5568] | 154 | analyzerPlaceholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
[5566] | 155 | analyzerPlaceholder.Successor = uniformSubScopeProcessor;
|
---|
[5560] | 156 |
|
---|
[5566] | 157 | uniformSubScopeProcessor.Operator = particleUpdaterPlaceholder;
|
---|
| 158 | uniformSubScopeProcessor.Successor = evaluationProcessor;
|
---|
[5560] | 159 |
|
---|
[5566] | 160 | particleUpdaterPlaceholder.Name = "(ParticleUpdater)";
|
---|
[5568] | 161 | particleUpdaterPlaceholder.OperatorParameter.ActualName = ParticleUpdaterParameter.Name;
|
---|
[5560] | 162 |
|
---|
[5566] | 163 | evaluationProcessor.Parallel = new BoolValue(true);
|
---|
| 164 | evaluationProcessor.Operator = evaluatorPlaceholder;
|
---|
[5643] | 165 | evaluationProcessor.Successor = subScopesCounter;
|
---|
[5560] | 166 |
|
---|
[5566] | 167 | evaluatorPlaceholder.Name = "(Evaluator)";
|
---|
[5568] | 168 | evaluatorPlaceholder.OperatorParameter.ActualName = EvaluatorParameter.Name;
|
---|
[5560] | 169 |
|
---|
[5643] | 170 | subScopesCounter.Name = "Increment EvaluatedSolutions";
|
---|
| 171 | subScopesCounter.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
|
---|
[5645] | 172 | subScopesCounter.Successor = topologyUpdaterPlaceholder;
|
---|
[5643] | 173 |
|
---|
[5566] | 174 | topologyUpdaterPlaceholder.Name = "(TopologyUpdater)";
|
---|
[5568] | 175 | topologyUpdaterPlaceholder.OperatorParameter.ActualName = TopologyUpdaterParameter.Name;
|
---|
[5566] | 176 | topologyUpdaterPlaceholder.Successor = swarmUpdater;
|
---|
[5560] | 177 |
|
---|
[5566] | 178 | swarmUpdater.Name = "Swarm Updater";
|
---|
[5568] | 179 | swarmUpdater.OperatorParameter.ActualName = SwarmUpdaterParameter.ActualName;
|
---|
| 180 | swarmUpdater.Successor = inertiaUpdaterPlaceholder;
|
---|
[5560] | 181 |
|
---|
[5568] | 182 | inertiaUpdaterPlaceholder.Name = "(Inertia Updater)";
|
---|
| 183 | inertiaUpdaterPlaceholder.OperatorParameter.ActualName = InertiaUpdaterParameter.ActualName;
|
---|
| 184 | inertiaUpdaterPlaceholder.Successor = currentIterationCounter;
|
---|
| 185 |
|
---|
[5566] | 186 | currentIterationCounter.Name = "CurrentIteration++";
|
---|
| 187 | currentIterationCounter.ValueParameter.ActualName = "CurrentIteration";
|
---|
[5568] | 188 | currentIterationCounter.Successor = currentIterationComparator;
|
---|
[5560] | 189 |
|
---|
[5566] | 190 | currentIterationComparator.LeftSideParameter.ActualName = "CurrentIteration";
|
---|
| 191 | currentIterationComparator.Comparison = new Comparison(ComparisonType.Less);
|
---|
| 192 | currentIterationComparator.RightSideParameter.ActualName = "MaxIterations";
|
---|
| 193 | currentIterationComparator.ResultParameter.ActualName = "ContinueIteration";
|
---|
| 194 | currentIterationComparator.Successor = conditionalBranch;
|
---|
[5560] | 195 |
|
---|
[5566] | 196 | conditionalBranch.Name = "ContinueIteration?";
|
---|
| 197 | conditionalBranch.ConditionParameter.ActualName = "ContinueIteration";
|
---|
[5581] | 198 | conditionalBranch.TrueBranch = analyzerPlaceholder;
|
---|
[5560] | 199 | #endregion
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | public override IOperation Apply() {
|
---|
| 203 | if (this.ParticleUpdaterParameter.ActualValue == null)
|
---|
| 204 | return null;
|
---|
| 205 | return base.Apply();
|
---|
| 206 | }
|
---|
| 207 | }
|
---|
| 208 | }
|
---|