Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/ParticleSwarmOptimizationMainLoop.cs @ 3832

Last change on this file since 3832 was 3742, checked in by gkronber, 15 years ago

Fixed GPL license headers and deleted files which are not referenced by projects. #893

File size: 8.1 KB
RevLine 
[3742]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
22using System;
[3348]23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Operators;
[3376]27using HeuristicLab.Common;
[3348]28using HeuristicLab.Core;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Parameters;
31using HeuristicLab.Data;
[3682]32using HeuristicLab.Analysis;
33using HeuristicLab.Optimization.Operators;
34using HeuristicLab.Encodings.RealVectorEncoding;
[3719]35using HeuristicLab.Optimization;
[3348]36
37namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
38  /// <summary>
39  /// An operator which represents the main loop of a PSO.
40  /// </summary>
41  [Item("ParticleSwarmOptimizationMainLoop", "An operator which represents the main loop of a PSO.")]
42  [StorableClass]
43  public class ParticleSwarmOptimizationMainLoop : AlgorithmOperator {
44    #region Parameter properties
45    public ValueLookupParameter<VariableCollection> ResultsParameter {
46      get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
47    }
[3719]48    public ValueLookupParameter<IRealVectorEncoder> EncoderParameter {
49      get { return (ValueLookupParameter<IRealVectorEncoder>)Parameters["Encoder"]; }
[3348]50    }
[3682]51    public ValueLookupParameter<IOperator> DecoderParameter {
52      get { return (ValueLookupParameter<IOperator>)Parameters["Decoder"]; }
53    }
54    public ValueLookupParameter<BoolValue> MaximizationParameter {
55      get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
56    }
57    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
58      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
59    }
60    public ValueLookupParameter<IntValue> MaximumGenerationsParameter {
61      get { return (ValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
62    }
63    public ValueLookupParameter<IOperator> EvaluatorParameter {
64      get { return (ValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
65    }
66    public ValueLookupParameter<IOperator> AnalyzerParameter {
67      get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
68    }
[3348]69    #endregion
70
[3719]71    [Storable]
72    private ParticleUpdater velocityUpdater;
73
[3348]74    [StorableConstructor]
75    private ParticleSwarmOptimizationMainLoop(bool deserializing) : base() { }
76    public ParticleSwarmOptimizationMainLoop()
77      : base() {
78      Initialize();
79    }
80
81    private void Initialize() {
82      #region Create parameters
83      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
[3719]84      Parameters.Add(new ValueLookupParameter<IRealVectorEncoder>("Encoder", "The encoding operator that maps a solution to a position vector."));
[3682]85      Parameters.Add(new ValueLookupParameter<IOperator>("Decoder", "The decoding operator that maps a position vector to a solution."));
86      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
87      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
88      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
89      Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator", "The operator used to evaluate solutions."));
90      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
[3348]91      #endregion
92
[3719]93      EncoderParameter.ActualNameChanged += new EventHandler(EncoderParameter_ActualNameChanged);
94
[3348]95      #region Create operators
96      VariableCreator variableCreator = new VariableCreator();
[3682]97      ResultsCollector resultsCollector1 = new ResultsCollector();
98      IntCounter intCounter = new IntCounter();
99      ConditionalBranch conditionalBranch = new ConditionalBranch();
[3719]100      velocityUpdater = new ParticleUpdater();
[3348]101      UniformSubScopesProcessor uniformSubScopesProcessor = new UniformSubScopesProcessor();
[3682]102      UniformSubScopesProcessor uniformSubScopesProcessor2 = new UniformSubScopesProcessor();
103      Placeholder encPlaceholder = new Placeholder();
104      Placeholder decPlaceholder = new Placeholder();
[3358]105      Placeholder evaluator = new Placeholder();
[3682]106      Comparator comparator = new Comparator();
107      SwarmUpdater swarmUpdater = new SwarmUpdater();
108      Placeholder analyzer1 = new Placeholder();
[3348]109
[3682]110      analyzer1.Name = "Analyzer (placeholder)";
111      analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
[3358]112
[3719]113      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Initial generation already built
[3682]114
115      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
116      resultsCollector1.ResultsParameter.ActualName = "Results";
117
118      intCounter.Increment = new IntValue(1);
119      intCounter.ValueParameter.ActualName = "Generations";
120
121      comparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
122      comparator.LeftSideParameter.ActualName = "Generations";
123      comparator.ResultParameter.ActualName = "Terminate";
124      comparator.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
125
126      conditionalBranch.ConditionParameter.ActualName = "Terminate";
127
128      velocityUpdater.BestGlobalParameter.ActualName = "CurrentBestPosition";
129      velocityUpdater.BestLocalParameter.ActualName = "BestPosition";
130      velocityUpdater.CurrentPositionParameter.ActualName = "Position";
131      velocityUpdater.VelocityParameter.ActualName = "Velocity";
[3719]132      //
133      // ToDo: Add correctly
[3682]134
135      encPlaceholder.OperatorParameter.ActualName = EncoderParameter.ActualName;
136      decPlaceholder.OperatorParameter.ActualName = DecoderParameter.ActualName;
137
138      evaluator.Name = "Evaluator (placeholder)";
139      evaluator.OperatorParameter.ActualName = EvaluatorParameter.Name;
140
141      swarmUpdater.CurrentPositionParameter.ActualName = "Position";
142      swarmUpdater.CurrentQualityParameter.ActualName = "Quality";
143      swarmUpdater.BestGlobalParameter.ActualName = "CurrentBestPosition";
144      swarmUpdater.BestLocalParameter.ActualName = "BestPosition";
145      swarmUpdater.LocalBestQualityParameter.ActualName = "BestQuality";
146      swarmUpdater.GlobalBestQualityParameter.ActualName = "CurrentBestBestQuality";
[3348]147      #endregion
148
149      #region Create operator graph
150      OperatorGraph.InitialOperator = variableCreator;
[3682]151      variableCreator.Successor = comparator;
152      comparator.Successor = conditionalBranch;
153      conditionalBranch.FalseBranch = uniformSubScopesProcessor;
154      uniformSubScopesProcessor.Operator = velocityUpdater;
[3719]155      uniformSubScopesProcessor.Successor = intCounter;
[3682]156      velocityUpdater.Successor = decPlaceholder;
[3719]157      decPlaceholder.Successor = evaluator;
[3682]158      evaluator.Successor = swarmUpdater;
[3719]159      swarmUpdater.Successor = null;
[3682]160      intCounter.Successor = resultsCollector1;
[3719]161      resultsCollector1.Successor = analyzer1;
162      analyzer1.Successor = comparator;
[3348]163      #endregion
164    }
[3719]165
166    private void EncoderParameter_ActualNameChanged(object sender, EventArgs e) {
167      velocityUpdater.BoundsParameter.ActualName = EncoderParameter.ActualValue.BoundsParameter.ActualName;
168    }
[3348]169  }
170}
Note: See TracBrowser for help on using the repository browser.