1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 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.Core;
|
---|
27 | using System.Xml;
|
---|
28 | using System.Diagnostics;
|
---|
29 | using HeuristicLab.DataAnalysis;
|
---|
30 | using HeuristicLab.Operators;
|
---|
31 | using HeuristicLab.Random;
|
---|
32 | using HeuristicLab.Selection;
|
---|
33 | using HeuristicLab.Logging;
|
---|
34 | using HeuristicLab.Data;
|
---|
35 | using HeuristicLab.Operators.Programmable;
|
---|
36 | using HeuristicLab.Selection.OffspringSelection;
|
---|
37 |
|
---|
38 | namespace HeuristicLab.GP.StructureIdentification {
|
---|
39 | public class OffspringSelectionGP : StandardGP {
|
---|
40 |
|
---|
41 | public virtual int MaxEvaluatedSolutions {
|
---|
42 | get { return GetVariableInjector().GetVariable("MaxEvaluatedSolutions").GetValue<IntData>().Data; }
|
---|
43 | set { GetVariableInjector().GetVariable("MaxEvaluatedSolutions").GetValue<IntData>().Data = value; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | public virtual double SelectionPressureLimit {
|
---|
47 | get { return GetVariableInjector().GetVariable("SelectionPressureLimit").GetValue<DoubleData>().Data; }
|
---|
48 | set { GetVariableInjector().GetVariable("SelectionPressureLimit").GetValue<DoubleData>().Data = value; }
|
---|
49 | }
|
---|
50 |
|
---|
51 | public virtual double ComparisonFactor {
|
---|
52 | get { return GetVariableInjector().GetVariable("ComparisonFactor").GetValue<DoubleData>().Data; }
|
---|
53 | set { GetVariableInjector().GetVariable("ComparisonFactor").GetValue<DoubleData>().Data = value; }
|
---|
54 | }
|
---|
55 |
|
---|
56 | public virtual double SuccessRatioLimit {
|
---|
57 | get { return GetVariableInjector().GetVariable("SuccessRatioLimit").GetValue<DoubleData>().Data; }
|
---|
58 | set { GetVariableInjector().GetVariable("SuccessRatioLimit").GetValue<DoubleData>().Data = value; }
|
---|
59 | }
|
---|
60 |
|
---|
61 | public override int MaxGenerations {
|
---|
62 | get { throw new NotSupportedException(); }
|
---|
63 | set { /* ignore */ }
|
---|
64 | }
|
---|
65 |
|
---|
66 | public override int TournamentSize {
|
---|
67 | get { throw new NotSupportedException(); }
|
---|
68 | set { /* ignore */ }
|
---|
69 | }
|
---|
70 |
|
---|
71 | public OffspringSelectionGP()
|
---|
72 | : base() {
|
---|
73 | PopulationSize = 1000;
|
---|
74 | Parents = 20;
|
---|
75 | MaxEvaluatedSolutions = 1000000;
|
---|
76 | SelectionPressureLimit = 300;
|
---|
77 | ComparisonFactor = 1.0;
|
---|
78 | SuccessRatioLimit = 1.0;
|
---|
79 | }
|
---|
80 |
|
---|
81 | protected internal override IOperator CreateGlobalInjector() {
|
---|
82 | VariableInjector injector = (VariableInjector)base.CreateGlobalInjector();
|
---|
83 | injector.RemoveVariable("TournamentSize");
|
---|
84 | injector.RemoveVariable("MaxGenerations");
|
---|
85 | injector.AddVariable(new HeuristicLab.Core.Variable("MaxEvaluatedSolutions", new IntData()));
|
---|
86 | injector.AddVariable(new HeuristicLab.Core.Variable("ComparisonFactor", new DoubleData()));
|
---|
87 | injector.AddVariable(new HeuristicLab.Core.Variable("SelectionPressureLimit", new DoubleData()));
|
---|
88 | injector.AddVariable(new HeuristicLab.Core.Variable("SuccessRatioLimit", new DoubleData()));
|
---|
89 | return injector;
|
---|
90 | }
|
---|
91 |
|
---|
92 | protected internal override IOperator CreateSelector() {
|
---|
93 | CombinedOperator selector = new CombinedOperator();
|
---|
94 | selector.Name = "Selector";
|
---|
95 | SequentialProcessor seq = new SequentialProcessor();
|
---|
96 | seq.Name = "Selector";
|
---|
97 | EmptyOperator emptyOp = new EmptyOperator();
|
---|
98 | ProportionalSelector femaleSelector = new ProportionalSelector();
|
---|
99 | femaleSelector.GetVariableInfo("Selected").ActualName = "Parents";
|
---|
100 | femaleSelector.GetVariableValue<BoolData>("CopySelected", null, false).Data = true;
|
---|
101 |
|
---|
102 | RandomSelector maleSelector = new RandomSelector();
|
---|
103 | maleSelector.GetVariableInfo("Selected").ActualName = "Parents";
|
---|
104 | maleSelector.GetVariableValue<BoolData>("CopySelected", null, false).Data = true;
|
---|
105 | SequentialSubScopesProcessor seqSubScopesProc = new SequentialSubScopesProcessor();
|
---|
106 | RightChildReducer rightChildReducer = new RightChildReducer();
|
---|
107 | SubScopesMixer mixer = new SubScopesMixer();
|
---|
108 |
|
---|
109 | seqSubScopesProc.AddSubOperator(femaleSelector);
|
---|
110 | seqSubScopesProc.AddSubOperator(emptyOp);
|
---|
111 |
|
---|
112 | seq.AddSubOperator(maleSelector);
|
---|
113 | seq.AddSubOperator(seqSubScopesProc);
|
---|
114 | seq.AddSubOperator(rightChildReducer);
|
---|
115 | seq.AddSubOperator(mixer);
|
---|
116 |
|
---|
117 | selector.OperatorGraph.AddOperator(seq);
|
---|
118 | selector.OperatorGraph.InitialOperator = seq;
|
---|
119 | return selector;
|
---|
120 | }
|
---|
121 |
|
---|
122 | protected internal override IOperator CreateChildCreater() {
|
---|
123 | CombinedOperator childCreater = new CombinedOperator();
|
---|
124 | childCreater.Name = "Create children";
|
---|
125 | SequentialProcessor seq = new SequentialProcessor();
|
---|
126 | SequentialProcessor offspringSelectionSeq = new SequentialProcessor();
|
---|
127 | OperatorExtractor selector = new OperatorExtractor();
|
---|
128 | selector.Name = "Selector (extr.)";
|
---|
129 | selector.GetVariableInfo("Operator").ActualName = "Selector";
|
---|
130 | SequentialSubScopesProcessor seqSubScopesProc = new SequentialSubScopesProcessor();
|
---|
131 | EmptyOperator emptyOp = new EmptyOperator();
|
---|
132 | OffspringSelector offspringSelector = new OffspringSelector();
|
---|
133 | OffspringAnalyzer offspringAnalyzer = new OffspringAnalyzer();
|
---|
134 | SequentialProcessor selectedProc = new SequentialProcessor();
|
---|
135 | OperatorExtractor crossover = new OperatorExtractor();
|
---|
136 | crossover.Name = "Crossover (extr.)";
|
---|
137 | crossover.GetVariableInfo("Operator").ActualName = "Crossover";
|
---|
138 | UniformSequentialSubScopesProcessor individualProc = new UniformSequentialSubScopesProcessor();
|
---|
139 | SequentialProcessor individualSeqProc = new SequentialProcessor();
|
---|
140 | StochasticBranch cond = new StochasticBranch();
|
---|
141 | cond.GetVariableInfo("Probability").ActualName = "MutationRate";
|
---|
142 | OperatorExtractor manipulator = new OperatorExtractor();
|
---|
143 | manipulator.Name = "Manipulator (extr.)";
|
---|
144 | manipulator.GetVariableInfo("Operator").ActualName = "Manipulator";
|
---|
145 | OperatorExtractor evaluator = new OperatorExtractor();
|
---|
146 | evaluator.Name = "Evaluator (extr.)";
|
---|
147 | evaluator.GetVariableInfo("Operator").ActualName = "Evaluator";
|
---|
148 | Counter evalCounter = new Counter();
|
---|
149 | evalCounter.GetVariableInfo("Value").ActualName = "EvaluatedSolutions";
|
---|
150 |
|
---|
151 | Sorter sorter = new Sorter();
|
---|
152 | sorter.GetVariableInfo("Descending").ActualName = "Maximization";
|
---|
153 | sorter.GetVariableInfo("Value").ActualName = "Quality";
|
---|
154 |
|
---|
155 | UniformSequentialSubScopesProcessor validationEvaluator = new UniformSequentialSubScopesProcessor();
|
---|
156 | MeanSquaredErrorEvaluator validationQualityEvaluator = new MeanSquaredErrorEvaluator();
|
---|
157 | validationQualityEvaluator.Name = "ValidationMeanSquaredErrorEvaluator";
|
---|
158 | validationQualityEvaluator.GetVariableInfo("MSE").ActualName = "ValidationQuality";
|
---|
159 | validationQualityEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
|
---|
160 | validationQualityEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
|
---|
161 |
|
---|
162 | seqSubScopesProc.AddSubOperator(emptyOp);
|
---|
163 | seqSubScopesProc.AddSubOperator(offspringAnalyzer);
|
---|
164 |
|
---|
165 | seq.AddSubOperator(offspringSelectionSeq);
|
---|
166 | offspringSelectionSeq.AddSubOperator(selector);
|
---|
167 | offspringSelectionSeq.AddSubOperator(seqSubScopesProc);
|
---|
168 | offspringSelectionSeq.AddSubOperator(offspringSelector);
|
---|
169 | offspringSelector.AddSubOperator(offspringSelectionSeq);
|
---|
170 |
|
---|
171 | offspringAnalyzer.AddSubOperator(selectedProc);
|
---|
172 | selectedProc.AddSubOperator(crossover);
|
---|
173 | selectedProc.AddSubOperator(individualProc);
|
---|
174 | individualProc.AddSubOperator(individualSeqProc);
|
---|
175 | individualSeqProc.AddSubOperator(cond);
|
---|
176 | cond.AddSubOperator(manipulator);
|
---|
177 | individualSeqProc.AddSubOperator(evaluator);
|
---|
178 | individualSeqProc.AddSubOperator(evalCounter);
|
---|
179 |
|
---|
180 | SequentialSubScopesProcessor seqSubScopesProc2 = new SequentialSubScopesProcessor();
|
---|
181 | seq.AddSubOperator(seqSubScopesProc2);
|
---|
182 | seqSubScopesProc2.AddSubOperator(emptyOp);
|
---|
183 |
|
---|
184 | SequentialProcessor newGenProc = new SequentialProcessor();
|
---|
185 | newGenProc.AddSubOperator(sorter);
|
---|
186 | newGenProc.AddSubOperator(validationEvaluator);
|
---|
187 | seqSubScopesProc2.AddSubOperator(newGenProc);
|
---|
188 |
|
---|
189 | validationEvaluator.AddSubOperator(validationQualityEvaluator);
|
---|
190 |
|
---|
191 | childCreater.OperatorGraph.AddOperator(seq);
|
---|
192 | childCreater.OperatorGraph.InitialOperator = seq;
|
---|
193 | return childCreater;
|
---|
194 | }
|
---|
195 |
|
---|
196 | protected internal override IOperator CreateLoopCondition(IOperator loop) {
|
---|
197 | SequentialProcessor seq = new SequentialProcessor();
|
---|
198 | seq.Name = "Loop Condition";
|
---|
199 | LessThanComparator generationsComparator = new LessThanComparator();
|
---|
200 | generationsComparator.GetVariableInfo("LeftSide").ActualName = "EvaluatedSolutions";
|
---|
201 | generationsComparator.GetVariableInfo("RightSide").ActualName = "MaxEvaluatedSolutions";
|
---|
202 | generationsComparator.GetVariableInfo("Result").ActualName = "EvaluatedSolutionsCondition";
|
---|
203 |
|
---|
204 | LessThanComparator selPresComparator = new LessThanComparator();
|
---|
205 | selPresComparator.GetVariableInfo("LeftSide").ActualName = "SelectionPressure";
|
---|
206 | selPresComparator.GetVariableInfo("RightSide").ActualName = "SelectionPressureLimit";
|
---|
207 | selPresComparator.GetVariableInfo("Result").ActualName = "SelectionPressureCondition";
|
---|
208 |
|
---|
209 | ConditionalBranch generationsCond = new ConditionalBranch();
|
---|
210 | generationsCond.GetVariableInfo("Condition").ActualName = "EvaluatedSolutionsCondition";
|
---|
211 |
|
---|
212 | ConditionalBranch selPresCond = new ConditionalBranch();
|
---|
213 | selPresCond.GetVariableInfo("Condition").ActualName = "SelectionPressureCondition";
|
---|
214 |
|
---|
215 | seq.AddSubOperator(generationsComparator);
|
---|
216 | seq.AddSubOperator(selPresComparator);
|
---|
217 | seq.AddSubOperator(generationsCond);
|
---|
218 | generationsCond.AddSubOperator(selPresCond);
|
---|
219 | selPresCond.AddSubOperator(loop);
|
---|
220 |
|
---|
221 | return seq;
|
---|
222 | }
|
---|
223 |
|
---|
224 | protected internal override IOperator CreateLoggingOperator() {
|
---|
225 | CombinedOperator loggingOperator = new CombinedOperator();
|
---|
226 | loggingOperator.Name = "Logging";
|
---|
227 | SequentialProcessor seq = new SequentialProcessor();
|
---|
228 |
|
---|
229 | DataCollector collector = new DataCollector();
|
---|
230 | ItemList<StringData> names = collector.GetVariable("VariableNames").GetValue<ItemList<StringData>>();
|
---|
231 | names.Add(new StringData("BestQuality"));
|
---|
232 | names.Add(new StringData("AverageQuality"));
|
---|
233 | names.Add(new StringData("WorstQuality"));
|
---|
234 | names.Add(new StringData("BestValidationQuality"));
|
---|
235 | names.Add(new StringData("AverageValidationQuality"));
|
---|
236 | names.Add(new StringData("WorstValidationQuality"));
|
---|
237 | names.Add(new StringData("EvaluatedSolutions"));
|
---|
238 | names.Add(new StringData("SelectionPressure"));
|
---|
239 | QualityLogger qualityLogger = new QualityLogger();
|
---|
240 | QualityLogger validationQualityLogger = new QualityLogger();
|
---|
241 | validationQualityLogger.Name = "ValidationQualityLogger";
|
---|
242 | validationQualityLogger.GetVariableInfo("Quality").ActualName = "ValidationQuality";
|
---|
243 | validationQualityLogger.GetVariableInfo("QualityLog").ActualName = "ValidationQualityLog";
|
---|
244 |
|
---|
245 | seq.AddSubOperator(collector);
|
---|
246 | seq.AddSubOperator(qualityLogger);
|
---|
247 | seq.AddSubOperator(validationQualityLogger);
|
---|
248 |
|
---|
249 | loggingOperator.OperatorGraph.AddOperator(seq);
|
---|
250 | loggingOperator.OperatorGraph.InitialOperator = seq;
|
---|
251 | return loggingOperator;
|
---|
252 | }
|
---|
253 |
|
---|
254 |
|
---|
255 | public override IEditor CreateEditor() {
|
---|
256 | return new OffspringSelectionGpEditor(this);
|
---|
257 | }
|
---|
258 |
|
---|
259 | public override IView CreateView() {
|
---|
260 | return new OffspringSelectionGpEditor(this);
|
---|
261 | }
|
---|
262 |
|
---|
263 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
264 | OffspringSelectionGP clone = (OffspringSelectionGP)base.Clone(clonedObjects);
|
---|
265 | clone.SelectionPressureLimit = SelectionPressureLimit;
|
---|
266 | clone.SuccessRatioLimit = SuccessRatioLimit;
|
---|
267 | clone.ComparisonFactor = ComparisonFactor;
|
---|
268 | return clone;
|
---|
269 | }
|
---|
270 | }
|
---|
271 | }
|
---|