[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;
|
---|
[5209] | 34 | using HeuristicLab.PluginInfrastructure;
|
---|
[3368] | 35 | using HeuristicLab.Random;
|
---|
[5410] | 36 | using System.Collections.Generic;
|
---|
[3348] | 37 |
|
---|
| 38 | namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
|
---|
[5033] | 39 |
|
---|
[5209] | 40 | [Item("Particle Swarm Optimization", "A particle swarm optimization algorithm based on the description in Pedersen, M.E.H. (2010). PhD thesis. University of Southampton.")]
|
---|
[3348] | 41 | [Creatable("Algorithms")]
|
---|
| 42 | [StorableClass]
|
---|
[5316] | 43 | public class ParticleSwarmOptimization : EngineAlgorithm, IStorableContent {
|
---|
[5033] | 44 |
|
---|
[3348] | 45 | #region Problem Properties
|
---|
| 46 | public override Type ProblemType {
|
---|
| 47 | get { return typeof(ISingleObjectiveProblem); }
|
---|
| 48 | }
|
---|
| 49 | public new ISingleObjectiveProblem Problem {
|
---|
| 50 | get { return (ISingleObjectiveProblem)base.Problem; }
|
---|
| 51 | set { base.Problem = value; }
|
---|
| 52 | }
|
---|
[3682] | 53 | public MultiAnalyzer Analyzer {
|
---|
| 54 | get { return AnalyzerParameter.Value; }
|
---|
| 55 | set { AnalyzerParameter.Value = value; }
|
---|
| 56 | }
|
---|
[5311] | 57 | public IDiscreteDoubleValueModifier OmegaUpdater {
|
---|
[5225] | 58 | get { return OmegaUpdaterParameter.Value; }
|
---|
| 59 | set { OmegaUpdaterParameter.Value = value; }
|
---|
| 60 | }
|
---|
[5311] | 61 | public IDiscreteDoubleMatrixModifier VelocityBoundsUpdater {
|
---|
[5225] | 62 | get { return VelocityBoundsUpdaterParameter.Value; }
|
---|
| 63 | set { VelocityBoundsUpdaterParameter.Value = value; }
|
---|
| 64 | }
|
---|
[4068] | 65 | #endregion
|
---|
[3348] | 66 |
|
---|
| 67 | #region Parameter Properties
|
---|
[5410] | 68 | public IValueParameter<IntValue> SeedParameter {
|
---|
| 69 | get { return (IValueParameter<IntValue>)Parameters["Seed"]; }
|
---|
[3348] | 70 | }
|
---|
[5410] | 71 | public IValueParameter<BoolValue> SetSeedRandomlyParameter {
|
---|
| 72 | get { return (IValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
|
---|
[3348] | 73 | }
|
---|
[5410] | 74 | public IValueParameter<IntValue> SwarmSizeParameter {
|
---|
| 75 | get { return (IValueParameter<IntValue>)Parameters["SwarmSize"]; }
|
---|
[3348] | 76 | }
|
---|
[5410] | 77 | public IValueParameter<IntValue> MaxIterationsParameter {
|
---|
| 78 | get { return (IValueParameter<IntValue>)Parameters["MaxIterations"]; }
|
---|
[3348] | 79 | }
|
---|
[5410] | 80 | public IValueParameter<DoubleValue> OmegaParameter {
|
---|
| 81 | get { return (IValueParameter<DoubleValue>)Parameters["Omega"]; }
|
---|
[3348] | 82 | }
|
---|
[5410] | 83 | public IValueParameter<DoubleValue> Phi_PParameter {
|
---|
| 84 | get { return (IValueParameter<DoubleValue>)Parameters["Phi_P"]; }
|
---|
[5033] | 85 | }
|
---|
[5410] | 86 | public IValueParameter<DoubleValue> Phi_GParameter {
|
---|
| 87 | get { return (IValueParameter<DoubleValue>)Parameters["Phi_G"]; }
|
---|
[5033] | 88 | }
|
---|
[5410] | 89 | public IValueParameter<MultiAnalyzer> AnalyzerParameter {
|
---|
| 90 | get { return (IValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
|
---|
[3682] | 91 | }
|
---|
[5410] | 92 | public IValueLookupParameter<DoubleMatrix> VelocityBoundsParameter {
|
---|
| 93 | get { return (IValueLookupParameter<DoubleMatrix>)Parameters["VelocityBounds"]; }
|
---|
[5033] | 94 | }
|
---|
[5209] | 95 | public ConstrainedValueParameter<IParticleUpdater> ParticleUpdaterParameter {
|
---|
| 96 | get { return (ConstrainedValueParameter<IParticleUpdater>)Parameters["ParticleUpdater"]; }
|
---|
| 97 | }
|
---|
[5410] | 98 | public OptionalConstrainedValueParameter<ITopologyInitializer> TopologyInitializerParameter {
|
---|
| 99 | get { return (OptionalConstrainedValueParameter<ITopologyInitializer>)Parameters["TopologyInitializer"]; }
|
---|
[5209] | 100 | }
|
---|
[5410] | 101 | public OptionalConstrainedValueParameter<ITopologyUpdater> TopologyUpdaterParameter {
|
---|
| 102 | get { return (OptionalConstrainedValueParameter<ITopologyUpdater>)Parameters["TopologyUpdater"]; }
|
---|
[5209] | 103 | }
|
---|
[5311] | 104 | public OptionalConstrainedValueParameter<IDiscreteDoubleMatrixModifier> VelocityBoundsUpdaterParameter {
|
---|
| 105 | get { return (OptionalConstrainedValueParameter<IDiscreteDoubleMatrixModifier>)Parameters["VelocityBoundsUpdater"]; }
|
---|
[5225] | 106 | }
|
---|
[5311] | 107 | public OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier> OmegaUpdaterParameter {
|
---|
| 108 | get { return (OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>)Parameters["OmegaUpdater"]; }
|
---|
[5225] | 109 | }
|
---|
[3348] | 110 | #endregion
|
---|
| 111 |
|
---|
| 112 | #region Properties
|
---|
[5316] | 113 |
|
---|
| 114 | public string Filename { get; set; }
|
---|
| 115 |
|
---|
[3719] | 116 | [Storable]
|
---|
[3682] | 117 | private BestAverageWorstQualityAnalyzer qualityAnalyzer;
|
---|
[5342] | 118 |
|
---|
| 119 | public ITopologyInitializer TopologyInitializer {
|
---|
| 120 | get { return TopologyInitializerParameter.Value; }
|
---|
| 121 | set { TopologyInitializerParameter.Value = value; }
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | public ITopologyUpdater TopologyUpdater {
|
---|
| 125 | get { return TopologyUpdaterParameter.Value; }
|
---|
| 126 | set { TopologyUpdaterParameter.Value = value; }
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | public IParticleUpdater ParticleUpdater {
|
---|
| 130 | get { return ParticleUpdaterParameter.Value; }
|
---|
| 131 | set { ParticleUpdaterParameter.Value = value; }
|
---|
| 132 | }
|
---|
| 133 |
|
---|
[3348] | 134 | #endregion
|
---|
| 135 |
|
---|
[5033] | 136 | [StorableConstructor]
|
---|
[5410] | 137 | protected ParticleSwarmOptimization(bool deserializing)
|
---|
| 138 | : base(deserializing) {
|
---|
[5342] | 139 | }
|
---|
[5033] | 140 | protected ParticleSwarmOptimization(ParticleSwarmOptimization original, Cloner cloner)
|
---|
| 141 | : base(original, cloner) {
|
---|
| 142 | qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
|
---|
[5342] | 143 | Initialize();
|
---|
[5033] | 144 | }
|
---|
| 145 |
|
---|
[5410] | 146 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 147 | private void AfterDeserialization() {
|
---|
| 148 | Initialize();
|
---|
| 149 | }
|
---|
| 150 |
|
---|
[3348] | 151 | public ParticleSwarmOptimization()
|
---|
| 152 | : base() {
|
---|
| 153 | Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
| 154 | Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
[3724] | 155 | Parameters.Add(new ValueParameter<IntValue>("SwarmSize", "Size of the particle swarm.", new IntValue(10)));
|
---|
[3348] | 156 | Parameters.Add(new ValueParameter<IntValue>("MaxIterations", "Maximal number of iterations.", new IntValue(1000)));
|
---|
[3682] | 157 | Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each generation.", new MultiAnalyzer()));
|
---|
[5033] | 158 | Parameters.Add(new ValueParameter<DoubleValue>("Omega", "Weight for particle's velocity vector.", new DoubleValue(-0.2)));
|
---|
| 159 | Parameters.Add(new ValueParameter<DoubleValue>("Phi_P", "Weight for particle's personal best position.", new DoubleValue(-0.01)));
|
---|
| 160 | Parameters.Add(new ValueParameter<DoubleValue>("Phi_G", "Weight for global best position.", new DoubleValue(3.7)));
|
---|
| 161 | Parameters.Add(new ValueLookupParameter<DoubleMatrix>("VelocityBounds", "Maximum Velocity in every dimension", new DoubleMatrix(new double[,] { { -1, 1 } })));
|
---|
[5410] | 162 | Parameters.Add(new ConstrainedValueParameter<IParticleUpdater>("ParticleUpdater", "Operator that calculates new position and velocity of a particle"));
|
---|
| 163 | Parameters.Add(new OptionalConstrainedValueParameter<ITopologyInitializer>("TopologyInitializer", "Creates neighborhood description vectors"));
|
---|
| 164 | Parameters.Add(new OptionalConstrainedValueParameter<ITopologyUpdater>("TopologyUpdater", "Updates the neighborhood description vectors"));
|
---|
[5311] | 165 | Parameters.Add(new OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>("OmegaUpdater", "Updates the omega parameter"));
|
---|
| 166 | Parameters.Add(new OptionalConstrainedValueParameter<IDiscreteDoubleMatrixModifier>("VelocityBoundsUpdater", "Adjusts the velocity bounds."));
|
---|
[5209] | 167 | ParticleUpdaterParameter.ActualValue = ParticleUpdaterParameter.ValidValues.SingleOrDefault(v => v.GetType() == typeof(TotallyConnectedParticleUpdater));
|
---|
[5033] | 168 |
|
---|
[3348] | 169 | RandomCreator randomCreator = new RandomCreator();
|
---|
[5033] | 170 | VariableCreator variableCreator = new VariableCreator();
|
---|
[3348] | 171 | SolutionsCreator solutionsCreator = new SolutionsCreator();
|
---|
[5033] | 172 | CombinedOperator particleCreator = new CombinedOperator();
|
---|
| 173 | Placeholder evaluatorPlaceholder = new Placeholder();
|
---|
| 174 | Assigner bestPersonalQualityAssigner = new Assigner();
|
---|
| 175 | BestPointInitializer bestPositionInitializer = new BestPointInitializer();
|
---|
[5209] | 176 | Placeholder topologyInitializerPlaceholder = new Placeholder();
|
---|
| 177 | NeighborUpdater neighborUpdater = new NeighborUpdater();
|
---|
[5033] | 178 | Placeholder analyzerPlaceholder = new Placeholder();
|
---|
| 179 | UniformSubScopesProcessor uniformSubScopeProcessor = new UniformSubScopesProcessor();
|
---|
[5209] | 180 | Placeholder particleUpdaterPlaceholder = new Placeholder();
|
---|
| 181 | Placeholder topologyUpdaterPlaceholder = new Placeholder();
|
---|
| 182 | UniformSubScopesProcessor uniformSubscopesProcessor2 = new UniformSubScopesProcessor();
|
---|
[5418] | 183 | UniformSubScopesProcessor evaluationProcessor = new UniformSubScopesProcessor();
|
---|
[5209] | 184 | NeighborUpdater neighborUpdater2 = new NeighborUpdater();
|
---|
[5033] | 185 | Placeholder evaluatorPlaceholder2 = new Placeholder();
|
---|
| 186 | SwarmUpdater swarmUpdater = new SwarmUpdater();
|
---|
| 187 | Placeholder analyzerPlaceholder2 = new Placeholder();
|
---|
| 188 | IntCounter currentIterationCounter = new IntCounter();
|
---|
| 189 | Comparator currentIterationComparator = new Comparator();
|
---|
| 190 | ConditionalBranch conditionalBranch = new ConditionalBranch();
|
---|
[5225] | 191 | Placeholder velocityBoundsUpdaterPlaceholder = new Placeholder();
|
---|
| 192 | Placeholder omegaUpdaterPlaceholder = new Placeholder();
|
---|
[3348] | 193 |
|
---|
[5033] | 194 | OperatorGraph.InitialOperator = randomCreator;
|
---|
[3682] | 195 |
|
---|
[5033] | 196 | randomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
[3348] | 197 | randomCreator.SeedParameter.Value = null;
|
---|
[5033] | 198 | randomCreator.Successor = variableCreator;
|
---|
[4068] | 199 |
|
---|
[5033] | 200 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("CurrentIteration", new IntValue(0)));
|
---|
| 201 | variableCreator.Successor = solutionsCreator;
|
---|
[4068] | 202 |
|
---|
[5033] | 203 | solutionsCreator.NumberOfSolutionsParameter.ActualName = "SwarmSize";
|
---|
| 204 | solutionsCreator.EvaluatorParameter.Value = evaluatorPlaceholder;
|
---|
| 205 | solutionsCreator.SolutionCreatorParameter.Value = particleCreator;
|
---|
| 206 | solutionsCreator.Successor = bestPositionInitializer;
|
---|
[4068] | 207 |
|
---|
[5033] | 208 | InitializeParticleCreator(particleCreator);
|
---|
[3348] | 209 |
|
---|
[5033] | 210 | evaluatorPlaceholder.Name = "(Evaluator)";
|
---|
| 211 | evaluatorPlaceholder.OperatorParameter.ActualName = "Evaluator";
|
---|
| 212 | evaluatorPlaceholder.Successor = bestPersonalQualityAssigner;
|
---|
[3348] | 213 |
|
---|
[5033] | 214 | bestPersonalQualityAssigner.LeftSideParameter.ActualName = "PersonalBestQuality";
|
---|
| 215 | bestPersonalQualityAssigner.RightSideParameter.ActualName = "Quality";
|
---|
[3682] | 216 |
|
---|
[5209] | 217 | bestPositionInitializer.Successor = topologyInitializerPlaceholder;
|
---|
[3682] | 218 |
|
---|
[5209] | 219 | topologyInitializerPlaceholder.Name = "(TopologyInitializer)";
|
---|
| 220 | topologyInitializerPlaceholder.OperatorParameter.ActualName = "TopologyInitializer";
|
---|
| 221 | topologyInitializerPlaceholder.Successor = neighborUpdater;
|
---|
| 222 |
|
---|
| 223 | neighborUpdater.Successor = analyzerPlaceholder;
|
---|
| 224 |
|
---|
[5033] | 225 | analyzerPlaceholder.Name = "(Analyzer)";
|
---|
| 226 | analyzerPlaceholder.OperatorParameter.ActualName = "Analyzer";
|
---|
| 227 | analyzerPlaceholder.Successor = uniformSubScopeProcessor;
|
---|
[3682] | 228 |
|
---|
[5209] | 229 | uniformSubScopeProcessor.Operator = particleUpdaterPlaceholder;
|
---|
[5418] | 230 | uniformSubScopeProcessor.Successor = evaluationProcessor;
|
---|
[3682] | 231 |
|
---|
[5209] | 232 | particleUpdaterPlaceholder.Name = "(ParticleUpdater)";
|
---|
| 233 | particleUpdaterPlaceholder.OperatorParameter.ActualName = "ParticleUpdater";
|
---|
[3682] | 234 |
|
---|
[5418] | 235 | evaluationProcessor.Parallel = new BoolValue(true);
|
---|
| 236 | evaluationProcessor.Operator = evaluatorPlaceholder2;
|
---|
| 237 | evaluationProcessor.Successor = topologyUpdaterPlaceholder;
|
---|
| 238 |
|
---|
[5033] | 239 | evaluatorPlaceholder2.Name = "(Evaluator)";
|
---|
| 240 | evaluatorPlaceholder2.OperatorParameter.ActualName = "Evaluator";
|
---|
[3682] | 241 |
|
---|
[5209] | 242 | topologyUpdaterPlaceholder.Name = "(TopologyUpdater)";
|
---|
| 243 | topologyUpdaterPlaceholder.OperatorParameter.ActualName = "TopologyUpdater";
|
---|
| 244 | topologyUpdaterPlaceholder.Successor = neighborUpdater2;
|
---|
| 245 |
|
---|
| 246 | neighborUpdater2.Successor = uniformSubscopesProcessor2;
|
---|
| 247 |
|
---|
| 248 | uniformSubscopesProcessor2.Operator = swarmUpdater;
|
---|
| 249 | uniformSubscopesProcessor2.Successor = analyzerPlaceholder2;
|
---|
| 250 |
|
---|
[5033] | 251 | analyzerPlaceholder2.Name = "(Analyzer)";
|
---|
| 252 | analyzerPlaceholder2.OperatorParameter.ActualName = "Analyzer";
|
---|
| 253 | analyzerPlaceholder2.Successor = currentIterationCounter;
|
---|
| 254 |
|
---|
| 255 | currentIterationCounter.Name = "CurrentIteration++";
|
---|
| 256 | currentIterationCounter.ValueParameter.ActualName = "CurrentIteration";
|
---|
[5225] | 257 | currentIterationCounter.Successor = omegaUpdaterPlaceholder;
|
---|
[5033] | 258 |
|
---|
[5225] | 259 | omegaUpdaterPlaceholder.Name = "(Omega Updater)";
|
---|
| 260 | omegaUpdaterPlaceholder.OperatorParameter.ActualName = "OmegaUpdater";
|
---|
| 261 | omegaUpdaterPlaceholder.Successor = velocityBoundsUpdaterPlaceholder;
|
---|
| 262 |
|
---|
| 263 | velocityBoundsUpdaterPlaceholder.Name = "(Velocity Bounds Updater)";
|
---|
| 264 | velocityBoundsUpdaterPlaceholder.OperatorParameter.ActualName = "VelocityBoundsUpdater";
|
---|
| 265 | velocityBoundsUpdaterPlaceholder.Successor = currentIterationComparator;
|
---|
| 266 |
|
---|
[5033] | 267 | currentIterationComparator.LeftSideParameter.ActualName = "CurrentIteration";
|
---|
| 268 | currentIterationComparator.Comparison = new Comparison(ComparisonType.Less);
|
---|
| 269 | currentIterationComparator.RightSideParameter.ActualName = "MaxIterations";
|
---|
| 270 | currentIterationComparator.ResultParameter.ActualName = "ContinueIteration";
|
---|
| 271 | currentIterationComparator.Successor = conditionalBranch;
|
---|
| 272 |
|
---|
| 273 | conditionalBranch.Name = "ContinueIteration?";
|
---|
| 274 | conditionalBranch.ConditionParameter.ActualName = "ContinueIteration";
|
---|
| 275 | conditionalBranch.TrueBranch = uniformSubScopeProcessor;
|
---|
| 276 |
|
---|
[3719] | 277 | InitializeAnalyzers();
|
---|
[5311] | 278 | InitVelocityBoundsUpdater();
|
---|
[3719] | 279 | UpdateAnalyzers();
|
---|
[5311] | 280 | UpdateOmegaUpdater();
|
---|
| 281 | InitOmegaUpdater();
|
---|
[5410] | 282 | UpdateTopologyInitializer();
|
---|
[5342] | 283 | Initialize();
|
---|
[3348] | 284 | }
|
---|
| 285 |
|
---|
[5410] | 286 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 287 | return new ParticleSwarmOptimization(this, cloner);
|
---|
[5342] | 288 | }
|
---|
| 289 |
|
---|
[5410] | 290 | public override void Prepare() {
|
---|
| 291 | if (Problem != null) {
|
---|
| 292 | base.Prepare();
|
---|
| 293 | if (OmegaUpdater != null && OmegaUpdater.StartValueParameter.Value != null) {
|
---|
| 294 | this.OmegaParameter.ActualValue = new DoubleValue(OmegaUpdaterParameter.Value.StartValueParameter.Value.Value);
|
---|
[5342] | 295 | }
|
---|
[5410] | 296 | if (VelocityBoundsUpdater != null && VelocityBoundsUpdater.StartValueParameter.Value != null && VelocityBoundsParameter.Value != null) {
|
---|
| 297 | DoubleMatrix matrix = VelocityBoundsParameter.Value;
|
---|
| 298 | for (int i = 0; i < matrix.Rows; i++) {
|
---|
| 299 | matrix[i, 0] = -VelocityBoundsUpdater.StartValueParameter.Value.Value;
|
---|
| 300 | matrix[i, 1] = VelocityBoundsUpdater.StartValueParameter.Value.Value;
|
---|
| 301 | }
|
---|
| 302 | }
|
---|
[5342] | 303 | }
|
---|
| 304 | }
|
---|
| 305 |
|
---|
[5410] | 306 | #region Events
|
---|
| 307 | protected override void OnProblemChanged() {
|
---|
| 308 | UpdateAnalyzers();
|
---|
| 309 | ParameterizeAnalyzers();
|
---|
| 310 | base.OnProblemChanged();
|
---|
[5342] | 311 | }
|
---|
| 312 |
|
---|
| 313 | void TopologyInitializerParameter_ValueChanged(object sender, EventArgs e) {
|
---|
[5410] | 314 | this.UpdateTopologyParameters();
|
---|
| 315 | }
|
---|
| 316 |
|
---|
| 317 | void VelocityBoundsUpdaterParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 318 | if (VelocityBoundsParameter.Value != null) {
|
---|
| 319 | foreach (IDiscreteDoubleMatrixModifier matrixOp in VelocityBoundsUpdaterParameter.Value.ScalingOperatorParameter.ValidValues) {
|
---|
| 320 | matrixOp.ValueParameter.ActualName = VelocityBoundsUpdater.ScaleParameter.Name;
|
---|
| 321 | matrixOp.StartValueParameter.Value = new DoubleValue(VelocityBoundsUpdater.ScaleParameter.ActualValue.Value);
|
---|
[5342] | 322 | }
|
---|
| 323 | }
|
---|
| 324 | }
|
---|
[5410] | 325 | #endregion
|
---|
[5342] | 326 |
|
---|
[5410] | 327 | #region Helpers
|
---|
| 328 | private void Initialize() {
|
---|
| 329 | TopologyInitializerParameter.ValueChanged += new EventHandler(TopologyInitializerParameter_ValueChanged);
|
---|
| 330 | }
|
---|
| 331 |
|
---|
[5033] | 332 | private static void InitializeParticleCreator(CombinedOperator particleCreator) {
|
---|
| 333 | Placeholder positionCreator = new Placeholder();
|
---|
| 334 | Assigner personalBestPositionAssigner = new Assigner();
|
---|
| 335 | UniformRandomRealVectorCreator velocityCreator = new UniformRandomRealVectorCreator();
|
---|
| 336 |
|
---|
| 337 | particleCreator.Name = "Particle Creator";
|
---|
| 338 | particleCreator.OperatorGraph.InitialOperator = positionCreator;
|
---|
| 339 |
|
---|
| 340 | positionCreator.Name = "(SolutionCreator)";
|
---|
| 341 | positionCreator.OperatorParameter.ActualName = "SolutionCreator";
|
---|
| 342 | positionCreator.Successor = personalBestPositionAssigner;
|
---|
| 343 |
|
---|
| 344 | personalBestPositionAssigner.LeftSideParameter.ActualName = "PersonalBestPoint";
|
---|
| 345 | personalBestPositionAssigner.RightSideParameter.ActualName = "Point";
|
---|
| 346 | personalBestPositionAssigner.Successor = velocityCreator;
|
---|
| 347 |
|
---|
| 348 | velocityCreator.LengthParameter.ActualName = "ProblemSize";
|
---|
| 349 | velocityCreator.BoundsParameter.ActualName = "VelocityBounds";
|
---|
| 350 | velocityCreator.RealVectorParameter.ActualName = "Velocity";
|
---|
[3348] | 351 | }
|
---|
| 352 |
|
---|
[5410] | 353 | private void InitializeAnalyzers() {
|
---|
| 354 | qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
| 355 | ParameterizeAnalyzers();
|
---|
[3348] | 356 | }
|
---|
| 357 |
|
---|
[5410] | 358 | private void ParameterizeAnalyzers() {
|
---|
[5312] | 359 | if (Problem != null) {
|
---|
[5410] | 360 | qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 361 | qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
| 362 | qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
[5312] | 363 | }
|
---|
[3348] | 364 | }
|
---|
| 365 |
|
---|
[5410] | 366 | private void UpdateAnalyzers() {
|
---|
| 367 | Analyzer.Operators.Clear();
|
---|
| 368 | if (Problem != null) {
|
---|
| 369 | foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>())
|
---|
| 370 | Analyzer.Operators.Add(analyzer);
|
---|
| 371 | }
|
---|
| 372 | Analyzer.Operators.Add(qualityAnalyzer);
|
---|
[3348] | 373 | }
|
---|
[3415] | 374 |
|
---|
[5311] | 375 | private void InitVelocityBoundsUpdater() {
|
---|
| 376 | foreach (IDiscreteDoubleMatrixModifier matrixOp in ApplicationManager.Manager.GetInstances<IDiscreteDoubleMatrixModifier>()) {
|
---|
| 377 | VelocityBoundsUpdaterParameter.ValidValues.Add(matrixOp);
|
---|
| 378 | matrixOp.ValueParameter.ActualName = VelocityBoundsParameter.Name;
|
---|
| 379 | matrixOp.EndIndexParameter.ActualName = MaxIterationsParameter.Name;
|
---|
| 380 | matrixOp.StartIndexParameter.Value = new IntValue(0);
|
---|
| 381 | matrixOp.IndexParameter.ActualName = "CurrentIteration";
|
---|
| 382 | matrixOp.EndValueParameter.Value = new DoubleValue(0);
|
---|
[5225] | 383 | }
|
---|
[5311] | 384 | VelocityBoundsUpdaterParameter.ValueChanged += new EventHandler(VelocityBoundsUpdaterParameter_ValueChanged);
|
---|
| 385 | }
|
---|
| 386 |
|
---|
| 387 | private void InitOmegaUpdater() {
|
---|
| 388 | foreach (IDiscreteDoubleValueModifier updater in OmegaUpdaterParameter.ValidValues) {
|
---|
| 389 | updater.EndIndexParameter.ActualName = MaxIterationsParameter.Name;
|
---|
| 390 | updater.StartIndexParameter.Value = new IntValue(0);
|
---|
| 391 | updater.IndexParameter.ActualName = "CurrentIteration";
|
---|
| 392 | updater.ValueParameter.ActualName = OmegaParameter.Name;
|
---|
| 393 | updater.StartValueParameter.Value = new DoubleValue(1);
|
---|
| 394 | updater.EndValueParameter.Value = new DoubleValue(0);
|
---|
| 395 | }
|
---|
| 396 | }
|
---|
| 397 |
|
---|
| 398 | private void UpdateOmegaUpdater() {
|
---|
| 399 | IDiscreteDoubleValueModifier oldOmegaUpdater = OmegaUpdater;
|
---|
| 400 | OmegaUpdaterParameter.ValidValues.Clear();
|
---|
| 401 | foreach (IDiscreteDoubleValueModifier updater in ApplicationManager.Manager.GetInstances<IDiscreteDoubleValueModifier>().OrderBy(x => x.Name)) {
|
---|
| 402 | OmegaUpdaterParameter.ValidValues.Add(updater);
|
---|
| 403 | }
|
---|
| 404 | if (oldOmegaUpdater != null) {
|
---|
| 405 | IDiscreteDoubleValueModifier updater = OmegaUpdaterParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldOmegaUpdater.GetType());
|
---|
| 406 | if (updater != null) OmegaUpdaterParameter.Value = updater;
|
---|
| 407 | }
|
---|
| 408 | }
|
---|
| 409 |
|
---|
[5410] | 410 | private void UpdateTopologyInitializer() {
|
---|
| 411 | ITopologyInitializer oldTopologyInitializer = TopologyInitializer;
|
---|
| 412 | TopologyInitializerParameter.ValidValues.Clear();
|
---|
| 413 | foreach (ITopologyInitializer topologyInitializer in ApplicationManager.Manager.GetInstances<ITopologyInitializer>().OrderBy(x => x.Name)) {
|
---|
| 414 | TopologyInitializerParameter.ValidValues.Add(topologyInitializer);
|
---|
[3348] | 415 | }
|
---|
[5410] | 416 | if (oldTopologyInitializer != null && TopologyInitializerParameter.ValidValues.Any(x => x.GetType() == oldTopologyInitializer.GetType()))
|
---|
| 417 | TopologyInitializer = TopologyInitializerParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldTopologyInitializer.GetType());
|
---|
| 418 | UpdateTopologyParameters();
|
---|
[3348] | 419 | }
|
---|
[3415] | 420 |
|
---|
[5410] | 421 | private void UpdateTopologyParameters() {
|
---|
| 422 | ITopologyUpdater oldTopologyUpdater = TopologyUpdater;
|
---|
| 423 | IParticleUpdater oldParticleUpdater = ParticleUpdater;
|
---|
| 424 | ClearTopologyParameters();
|
---|
| 425 | if (TopologyInitializer != null) {
|
---|
| 426 | foreach (ITopologyUpdater topologyUpdater in ApplicationManager.Manager.GetInstances<ITopologyUpdater>())
|
---|
| 427 | TopologyUpdaterParameter.ValidValues.Add(topologyUpdater);
|
---|
| 428 | foreach (IParticleUpdater particleUpdater in ApplicationManager.Manager.GetInstances<ILocalParticleUpdater>())
|
---|
| 429 | ParticleUpdaterParameter.ValidValues.Add(particleUpdater);
|
---|
| 430 | } else {
|
---|
| 431 | foreach (IParticleUpdater particleUpdater in ApplicationManager.Manager.GetInstances<IGlobalParticleUpdater>())
|
---|
| 432 | ParticleUpdaterParameter.ValidValues.Add(particleUpdater);
|
---|
[3682] | 433 | }
|
---|
[5410] | 434 | if (oldTopologyUpdater != null) {
|
---|
| 435 | ITopologyUpdater newTopologyUpdater = TopologyUpdaterParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldParticleUpdater.GetType());
|
---|
| 436 | if (newTopologyUpdater != null) TopologyUpdater = newTopologyUpdater;
|
---|
| 437 | }
|
---|
| 438 | if (oldParticleUpdater != null) {
|
---|
| 439 | IParticleUpdater newParticleUpdater = ParticleUpdaterParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldParticleUpdater.GetType());
|
---|
| 440 | if (newParticleUpdater != null) ParticleUpdater = newParticleUpdater;
|
---|
| 441 | }
|
---|
[3682] | 442 | }
|
---|
| 443 |
|
---|
[5410] | 444 | private void ClearTopologyParameters() {
|
---|
| 445 | TopologyUpdaterParameter.ValidValues.Clear();
|
---|
| 446 | ParticleUpdaterParameter.ValidValues.Clear();
|
---|
| 447 | }
|
---|
[3348] | 448 | #endregion
|
---|
[5410] | 449 |
|
---|
[3348] | 450 | }
|
---|
| 451 | }
|
---|