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.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Operators;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Data;
|
---|
32 | using HeuristicLab.Analysis;
|
---|
33 | using HeuristicLab.Optimization.Operators;
|
---|
34 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
35 | using HeuristicLab.Optimization;
|
---|
36 |
|
---|
37 | namespace 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 | }
|
---|
48 | public ValueLookupParameter<IRealVectorEncoder> EncoderParameter {
|
---|
49 | get { return (ValueLookupParameter<IRealVectorEncoder>)Parameters["Encoder"]; }
|
---|
50 | }
|
---|
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 | }
|
---|
69 | #endregion
|
---|
70 |
|
---|
71 | [Storable]
|
---|
72 | private ParticleUpdater velocityUpdater;
|
---|
73 |
|
---|
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."));
|
---|
84 | Parameters.Add(new ValueLookupParameter<IRealVectorEncoder>("Encoder", "The encoding operator that maps a solution to a position vector."));
|
---|
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."));
|
---|
91 | #endregion
|
---|
92 |
|
---|
93 | EncoderParameter.ActualNameChanged += new EventHandler(EncoderParameter_ActualNameChanged);
|
---|
94 |
|
---|
95 | #region Create operators
|
---|
96 | VariableCreator variableCreator = new VariableCreator();
|
---|
97 | ResultsCollector resultsCollector1 = new ResultsCollector();
|
---|
98 | IntCounter intCounter = new IntCounter();
|
---|
99 | ConditionalBranch conditionalBranch = new ConditionalBranch();
|
---|
100 | velocityUpdater = new ParticleUpdater();
|
---|
101 | UniformSubScopesProcessor uniformSubScopesProcessor = new UniformSubScopesProcessor();
|
---|
102 | UniformSubScopesProcessor uniformSubScopesProcessor2 = new UniformSubScopesProcessor();
|
---|
103 | Placeholder encPlaceholder = new Placeholder();
|
---|
104 | Placeholder decPlaceholder = new Placeholder();
|
---|
105 | Placeholder evaluator = new Placeholder();
|
---|
106 | Comparator comparator = new Comparator();
|
---|
107 | SwarmUpdater swarmUpdater = new SwarmUpdater();
|
---|
108 | Placeholder analyzer1 = new Placeholder();
|
---|
109 |
|
---|
110 | analyzer1.Name = "Analyzer (placeholder)";
|
---|
111 | analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
112 |
|
---|
113 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Initial generation already built
|
---|
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";
|
---|
132 | //
|
---|
133 | // ToDo: Add correctly
|
---|
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";
|
---|
147 | #endregion
|
---|
148 |
|
---|
149 | #region Create operator graph
|
---|
150 | OperatorGraph.InitialOperator = variableCreator;
|
---|
151 | variableCreator.Successor = comparator;
|
---|
152 | comparator.Successor = conditionalBranch;
|
---|
153 | conditionalBranch.FalseBranch = uniformSubScopesProcessor;
|
---|
154 | uniformSubScopesProcessor.Operator = velocityUpdater;
|
---|
155 | uniformSubScopesProcessor.Successor = intCounter;
|
---|
156 | velocityUpdater.Successor = decPlaceholder;
|
---|
157 | decPlaceholder.Successor = evaluator;
|
---|
158 | evaluator.Successor = swarmUpdater;
|
---|
159 | swarmUpdater.Successor = null;
|
---|
160 | intCounter.Successor = resultsCollector1;
|
---|
161 | resultsCollector1.Successor = analyzer1;
|
---|
162 | analyzer1.Successor = comparator;
|
---|
163 | #endregion
|
---|
164 | }
|
---|
165 |
|
---|
166 | private void EncoderParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
167 | velocityUpdater.BoundsParameter.ActualName = EncoderParameter.ActualValue.BoundsParameter.ActualName;
|
---|
168 | }
|
---|
169 | }
|
---|
170 | }
|
---|