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 HeuristicLab.Analysis;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Data;
|
---|
25 | using HeuristicLab.Evolutionary;
|
---|
26 | using HeuristicLab.Operators;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Selection;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Algorithms.SGA {
|
---|
31 | /// <summary>
|
---|
32 | /// An operator which represents a Standard Genetic Algorithm.
|
---|
33 | /// </summary>
|
---|
34 | [Item("SGAOperator", "An operator which represents a Standard Genetic Algorithm.")]
|
---|
35 | [Creatable("Test")]
|
---|
36 | public class SGAOperator : AlgorithmOperator {
|
---|
37 | #region Parameter properties
|
---|
38 | public ValueLookupParameter<IRandom> RandomParameter {
|
---|
39 | get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
|
---|
40 | }
|
---|
41 | public ValueLookupParameter<BoolData> MaximizationParameter {
|
---|
42 | get { return (ValueLookupParameter<BoolData>)Parameters["Maximization"]; }
|
---|
43 | }
|
---|
44 | public SubScopesLookupParameter<DoubleData> QualityParameter {
|
---|
45 | get { return (SubScopesLookupParameter<DoubleData>)Parameters["Quality"]; }
|
---|
46 | }
|
---|
47 | public ValueLookupParameter<IOperator> SelectorParameter {
|
---|
48 | get { return (ValueLookupParameter<IOperator>)Parameters["Selector"]; }
|
---|
49 | }
|
---|
50 | public ValueLookupParameter<IOperator> CrossoverParameter {
|
---|
51 | get { return (ValueLookupParameter<IOperator>)Parameters["Crossover"]; }
|
---|
52 | }
|
---|
53 | public ValueLookupParameter<DoubleData> MutationProbabilityParameter {
|
---|
54 | get { return (ValueLookupParameter<DoubleData>)Parameters["MutationProbability"]; }
|
---|
55 | }
|
---|
56 | public ValueLookupParameter<IOperator> MutatorParameter {
|
---|
57 | get { return (ValueLookupParameter<IOperator>)Parameters["Mutator"]; }
|
---|
58 | }
|
---|
59 | public ValueLookupParameter<IOperator> EvaluatorParameter {
|
---|
60 | get { return (ValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
|
---|
61 | }
|
---|
62 | public ValueLookupParameter<IntData> ElitesParameter {
|
---|
63 | get { return (ValueLookupParameter<IntData>)Parameters["Elites"]; }
|
---|
64 | }
|
---|
65 | public ValueLookupParameter<IntData> MaximumGenerationsParameter {
|
---|
66 | get { return (ValueLookupParameter<IntData>)Parameters["MaximumGenerations"]; }
|
---|
67 | }
|
---|
68 | public ValueLookupParameter<VariableCollection> ResultsParameter {
|
---|
69 | get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
|
---|
70 | }
|
---|
71 | private ScopeParameter CurrentScopeParameter {
|
---|
72 | get { return (ScopeParameter)Parameters["CurrentScope"]; }
|
---|
73 | }
|
---|
74 |
|
---|
75 | public IScope CurrentScope {
|
---|
76 | get { return CurrentScopeParameter.ActualValue; }
|
---|
77 | }
|
---|
78 | #endregion
|
---|
79 |
|
---|
80 | public SGAOperator()
|
---|
81 | : base() {
|
---|
82 | #region Create parameters
|
---|
83 | Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
|
---|
84 | Parameters.Add(new ValueLookupParameter<BoolData>("Maximization", "True if the problem is a maximization problem, otherwise false."));
|
---|
85 | Parameters.Add(new SubScopesLookupParameter<DoubleData>("Quality", "The value which represents the quality of a solution."));
|
---|
86 | Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
|
---|
87 | Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
|
---|
88 | Parameters.Add(new ValueLookupParameter<DoubleData>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
|
---|
89 | Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
|
---|
90 | Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator", "The operator used to evaluate solutions."));
|
---|
91 | Parameters.Add(new ValueLookupParameter<IntData>("Elites", "The numer of elite solutions which are kept in each generation."));
|
---|
92 | Parameters.Add(new ValueLookupParameter<IntData>("MaximumGenerations", "The maximum number of generations which should be processed."));
|
---|
93 | Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
|
---|
94 | Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the SGA should be applied."));
|
---|
95 | #endregion
|
---|
96 |
|
---|
97 | #region Create operator graph
|
---|
98 | VariableCreator variableCreator = new VariableCreator();
|
---|
99 | ResultsCollector resultsCollector1 = new ResultsCollector();
|
---|
100 | SubScopesSorter subScopesSorter1 = new SubScopesSorter();
|
---|
101 | Placeholder selector = new Placeholder();
|
---|
102 | SequentialSubScopesProcessor sequentialSubScopesProcessor1 = new SequentialSubScopesProcessor();
|
---|
103 | ChildrenCreator childrenCreator = new ChildrenCreator();
|
---|
104 | UniformSequentialSubScopesProcessor uniformSequentialSubScopesProcessor = new UniformSequentialSubScopesProcessor();
|
---|
105 | Placeholder crossover = new Placeholder();
|
---|
106 | StochasticBranch stochasticBranch = new StochasticBranch();
|
---|
107 | Placeholder mutator = new Placeholder();
|
---|
108 | Placeholder evaluator = new Placeholder();
|
---|
109 | SubScopesRemover subScopesRemover = new SubScopesRemover();
|
---|
110 | SubScopesSorter subScopesSorter2 = new SubScopesSorter();
|
---|
111 | SequentialSubScopesProcessor sequentialSubScopesProcessor2 = new SequentialSubScopesProcessor();
|
---|
112 | LeftSelector leftSelector = new LeftSelector();
|
---|
113 | RightReducer rightReducer = new RightReducer();
|
---|
114 | MergingReducer mergingReducer = new MergingReducer();
|
---|
115 | IntCounter intCounter = new IntCounter();
|
---|
116 | Comparator comparator = new Comparator();
|
---|
117 | BestAverageWorstQualityCalculator bestAverageWorstQualityCalculator = new BestAverageWorstQualityCalculator();
|
---|
118 | ResultsCollector resultsCollector2 = new ResultsCollector();
|
---|
119 | DataTableValuesCollector dataTableValuesCollector = new DataTableValuesCollector();
|
---|
120 | ConditionalBranch conditionalBranch = new ConditionalBranch();
|
---|
121 |
|
---|
122 | OperatorGraph.InitialOperator = variableCreator;
|
---|
123 |
|
---|
124 | variableCreator.CollectedValues.Add(new ValueParameter<DataTable>("Qualities", new DataTable("Qualities")));
|
---|
125 | variableCreator.Successor = resultsCollector1;
|
---|
126 |
|
---|
127 | LookupParameter<DataTable> qualities = new LookupParameter<DataTable>("Qualities");
|
---|
128 | qualities.ActualName = "Qualities";
|
---|
129 | resultsCollector1.CollectedValues.Add(qualities);
|
---|
130 | resultsCollector1.ResultsParameter.ActualName = "Results";
|
---|
131 | resultsCollector1.Successor = subScopesSorter1;
|
---|
132 |
|
---|
133 | subScopesSorter1.DescendingParameter.ActualName = "Maximization";
|
---|
134 | subScopesSorter1.ValueParameter.ActualName = "Quality";
|
---|
135 | subScopesSorter1.Successor = selector;
|
---|
136 |
|
---|
137 | selector.Name = "Selector";
|
---|
138 | selector.OperatorParameter.ActualName = "Selector";
|
---|
139 | selector.Successor = sequentialSubScopesProcessor1;
|
---|
140 |
|
---|
141 | sequentialSubScopesProcessor1.Operators.Add(new EmptyOperator());
|
---|
142 | sequentialSubScopesProcessor1.Operators.Add(childrenCreator);
|
---|
143 | sequentialSubScopesProcessor1.Successor = sequentialSubScopesProcessor2;
|
---|
144 |
|
---|
145 | childrenCreator.ParentsPerChild = new IntData(2);
|
---|
146 | childrenCreator.Successor = uniformSequentialSubScopesProcessor;
|
---|
147 |
|
---|
148 | uniformSequentialSubScopesProcessor.Operator = crossover;
|
---|
149 | uniformSequentialSubScopesProcessor.Successor = subScopesSorter2;
|
---|
150 |
|
---|
151 | crossover.Name = "Crossover";
|
---|
152 | crossover.OperatorParameter.ActualName = "Crossover";
|
---|
153 | crossover.Successor = stochasticBranch;
|
---|
154 |
|
---|
155 | stochasticBranch.FirstBranch = mutator;
|
---|
156 | stochasticBranch.ProbabilityParameter.ActualName = "MutationProbability";
|
---|
157 | stochasticBranch.RandomParameter.ActualName = "Random";
|
---|
158 | stochasticBranch.SecondBranch = null;
|
---|
159 | stochasticBranch.Successor = evaluator;
|
---|
160 |
|
---|
161 | mutator.Name = "Mutator";
|
---|
162 | mutator.OperatorParameter.ActualName = "Mutator";
|
---|
163 | mutator.Successor = null;
|
---|
164 |
|
---|
165 | evaluator.Name = "Evaluator";
|
---|
166 | evaluator.OperatorParameter.ActualName = "Evaluator";
|
---|
167 | evaluator.Successor = subScopesRemover;
|
---|
168 |
|
---|
169 | subScopesRemover.RemoveAllSubScopes = true;
|
---|
170 | subScopesRemover.Successor = null;
|
---|
171 |
|
---|
172 | subScopesSorter2.DescendingParameter.ActualName = "Maximization";
|
---|
173 | subScopesSorter2.ValueParameter.ActualName = "Quality";
|
---|
174 | subScopesSorter2.Successor = null;
|
---|
175 |
|
---|
176 | sequentialSubScopesProcessor2.Operators.Add(leftSelector);
|
---|
177 | sequentialSubScopesProcessor2.Operators.Add(new EmptyOperator());
|
---|
178 | sequentialSubScopesProcessor2.Successor = mergingReducer;
|
---|
179 |
|
---|
180 | leftSelector.CopySelected = new BoolData(false);
|
---|
181 | leftSelector.NumberOfSelectedSubScopesParameter.ActualName = "Elites";
|
---|
182 | leftSelector.Successor = rightReducer;
|
---|
183 |
|
---|
184 | rightReducer.Successor = null;
|
---|
185 |
|
---|
186 | mergingReducer.Successor = intCounter;
|
---|
187 |
|
---|
188 | intCounter.Increment = new IntData(1);
|
---|
189 | intCounter.ValueParameter.ActualName = "Generations";
|
---|
190 | intCounter.Successor = comparator;
|
---|
191 |
|
---|
192 | comparator.Comparison = new ComparisonData(Comparison.GreaterOrEqual);
|
---|
193 | comparator.LeftSideParameter.ActualName = "Generations";
|
---|
194 | comparator.ResultParameter.ActualName = "Terminate";
|
---|
195 | comparator.RightSideParameter.ActualName = "MaximumGenerations";
|
---|
196 | comparator.Successor = bestAverageWorstQualityCalculator;
|
---|
197 |
|
---|
198 | bestAverageWorstQualityCalculator.AverageQualityParameter.ActualName = "AverageQuality";
|
---|
199 | bestAverageWorstQualityCalculator.BestQualityParameter.ActualName = "BestQuality";
|
---|
200 | bestAverageWorstQualityCalculator.MaximizationParameter.ActualName = "Maximization";
|
---|
201 | bestAverageWorstQualityCalculator.QualityParameter.ActualName = "Quality";
|
---|
202 | bestAverageWorstQualityCalculator.WorstQualityParameter.ActualName = "WorstQuality";
|
---|
203 | bestAverageWorstQualityCalculator.Successor = dataTableValuesCollector;
|
---|
204 |
|
---|
205 | LookupParameter<DoubleData> bestQuality = new LookupParameter<DoubleData>("BestQuality");
|
---|
206 | bestQuality.ActualName = "BestQuality";
|
---|
207 | dataTableValuesCollector.CollectedValues.Add(bestQuality);
|
---|
208 | LookupParameter<DoubleData> averageQuality = new LookupParameter<DoubleData>("AverageQuality");
|
---|
209 | averageQuality.ActualName = "AverageQuality";
|
---|
210 | dataTableValuesCollector.CollectedValues.Add(averageQuality);
|
---|
211 | LookupParameter<DoubleData> worstQuality = new LookupParameter<DoubleData>("WorstQuality");
|
---|
212 | worstQuality.ActualName = "WorstQuality";
|
---|
213 | dataTableValuesCollector.CollectedValues.Add(worstQuality);
|
---|
214 | dataTableValuesCollector.DataTableParameter.ActualName = "Qualities";
|
---|
215 | dataTableValuesCollector.Successor = resultsCollector2;
|
---|
216 |
|
---|
217 | bestQuality = new LookupParameter<DoubleData>("BestQuality");
|
---|
218 | bestQuality.ActualName = "BestQuality";
|
---|
219 | resultsCollector2.CollectedValues.Add(bestQuality);
|
---|
220 | averageQuality = new LookupParameter<DoubleData>("AverageQuality");
|
---|
221 | averageQuality.ActualName = "AverageQuality";
|
---|
222 | resultsCollector2.CollectedValues.Add(averageQuality);
|
---|
223 | worstQuality = new LookupParameter<DoubleData>("WorstQuality");
|
---|
224 | worstQuality.ActualName = "WorstQuality";
|
---|
225 | resultsCollector2.CollectedValues.Add(worstQuality);
|
---|
226 | resultsCollector2.ResultsParameter.ActualName = "Results";
|
---|
227 | resultsCollector2.Successor = conditionalBranch;
|
---|
228 |
|
---|
229 | conditionalBranch.ConditionParameter.ActualName = "Terminate";
|
---|
230 | conditionalBranch.FalseBranch = subScopesSorter1;
|
---|
231 | conditionalBranch.TrueBranch = null;
|
---|
232 | conditionalBranch.Successor = null;
|
---|
233 | #endregion
|
---|
234 | }
|
---|
235 | }
|
---|
236 | }
|
---|