Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PSO/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/ParticleSwarmOptimizationMainLoop.cs @ 5760

Last change on this file since 5760 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

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