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.Linq;
|
---|
24 | using System.Collections.Generic;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Evolutionary;
|
---|
28 | using HeuristicLab.Logging;
|
---|
29 | using HeuristicLab.Operators;
|
---|
30 | using HeuristicLab.Selection;
|
---|
31 | using HeuristicLab.Selection.OffspringSelection;
|
---|
32 | using HeuristicLab.Modeling;
|
---|
33 | using HeuristicLab.DataAnalysis;
|
---|
34 | using HeuristicLab.Operators.Programmable;
|
---|
35 | using HeuristicLab.GP.Algorithms;
|
---|
36 |
|
---|
37 | namespace HeuristicLab.GP.StructureIdentification {
|
---|
38 | public class OffspringSelectionGPRegression : HeuristicLab.GP.Algorithms.OffspringSelectionGP, IStochasticAlgorithm {
|
---|
39 | public override string Name { get { return "OffspringSelectionGP - StructureIdentification"; } }
|
---|
40 |
|
---|
41 | public virtual string TargetVariable {
|
---|
42 | get { return ProblemInjector.GetVariableValue<StringData>("TargetVariable", null, false).Data; }
|
---|
43 | set { ProblemInjector.GetVariableValue<StringData>("TargetVariable", null, false).Data = value; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | public virtual Dataset Dataset {
|
---|
47 | get { return ProblemInjector.GetVariableValue<Dataset>("Dataset", null, false); }
|
---|
48 | set { ProblemInjector.GetVariable("Dataset").Value = value; }
|
---|
49 | }
|
---|
50 |
|
---|
51 | public virtual IAnalyzerModel Model {
|
---|
52 | get {
|
---|
53 | if (!Engine.Terminated)
|
---|
54 | throw new InvalidOperationException("The algorithm is still running. Wait until the algorithm is terminated to retrieve the result.");
|
---|
55 | else
|
---|
56 | return CreateGPModel();
|
---|
57 | }
|
---|
58 | }
|
---|
59 | public IEnumerable<string> AllowedVariables {
|
---|
60 | get {
|
---|
61 | ItemList<StringData> allowedVariables = ProblemInjector.GetVariableValue<ItemList<StringData>>("AllowedFeatures", null, false);
|
---|
62 | return allowedVariables.Select(x => x.Data);
|
---|
63 | }
|
---|
64 | set {
|
---|
65 | ItemList<StringData> allowedVariables = ProblemInjector.GetVariableValue<ItemList<StringData>>("AllowedFeatures", null, false);
|
---|
66 | foreach (string x in value) allowedVariables.Add(new StringData(x));
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | public int TrainingSamplesStart {
|
---|
71 | get { return ProblemInjector.GetVariableValue<IntData>("TrainingSamplesStart", null, false).Data; }
|
---|
72 | set { ProblemInjector.GetVariableValue<IntData>("TrainingSamplesStart", null, false).Data = value; }
|
---|
73 | }
|
---|
74 |
|
---|
75 | public int TrainingSamplesEnd {
|
---|
76 | get { return ProblemInjector.GetVariableValue<IntData>("TrainingSamplesEnd", null, false).Data; }
|
---|
77 | set { ProblemInjector.GetVariableValue<IntData>("TrainingSamplesEnd", null, false).Data = value; }
|
---|
78 | }
|
---|
79 |
|
---|
80 | public int ValidationSamplesStart {
|
---|
81 | get { return ProblemInjector.GetVariableValue<IntData>("ValidationSamplesStart", null, false).Data; }
|
---|
82 | set { ProblemInjector.GetVariableValue<IntData>("ValidationSamplesStart", null, false).Data = value; }
|
---|
83 | }
|
---|
84 |
|
---|
85 | public int ValidationSamplesEnd {
|
---|
86 | get { return ProblemInjector.GetVariableValue<IntData>("ValidationSamplesEnd", null, false).Data; }
|
---|
87 | set { ProblemInjector.GetVariableValue<IntData>("ValidationSamplesEnd", null, false).Data = value; }
|
---|
88 | }
|
---|
89 |
|
---|
90 | public int TestSamplesStart {
|
---|
91 | get { return ProblemInjector.GetVariableValue<IntData>("TestSamplesStart", null, false).Data; }
|
---|
92 | set { ProblemInjector.GetVariableValue<IntData>("TestSamplesStart", null, false).Data = value; }
|
---|
93 | }
|
---|
94 |
|
---|
95 | public int TestSamplesEnd {
|
---|
96 | get { return ProblemInjector.GetVariableValue<IntData>("TestSamplesEnd", null, false).Data; }
|
---|
97 | set { ProblemInjector.GetVariableValue<IntData>("TestSamplesEnd", null, false).Data = value; }
|
---|
98 | }
|
---|
99 |
|
---|
100 | public virtual double PunishmentFactor {
|
---|
101 | get { return GetVariableInjector().GetVariable("PunishmentFactor").GetValue<DoubleData>().Data; }
|
---|
102 | set { GetVariableInjector().GetVariable("PunishmentFactor").GetValue<DoubleData>().Data = value; }
|
---|
103 | }
|
---|
104 |
|
---|
105 | public virtual int MaxBestValidationSolutionAge {
|
---|
106 | get { return GetVariableInjector().GetVariable("MaxBestValidationSolutionAge").GetValue<IntData>().Data; }
|
---|
107 | set { GetVariableInjector().GetVariable("MaxBestValidationSolutionAge").GetValue<IntData>().Data = value; }
|
---|
108 | }
|
---|
109 |
|
---|
110 | public override IOperator ProblemInjector {
|
---|
111 | get { return GetProblemInjector().OperatorGraph.InitialOperator.SubOperators[0]; }
|
---|
112 | set {
|
---|
113 | value.Name = "ProblemInjector";
|
---|
114 | CombinedOperator problemInjector = GetProblemInjector();
|
---|
115 | problemInjector.OperatorGraph.RemoveOperator(ProblemInjector.Guid);
|
---|
116 | problemInjector.OperatorGraph.AddOperator(value);
|
---|
117 | problemInjector.OperatorGraph.InitialOperator.AddSubOperator(value, 0);
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | public OffspringSelectionGPRegression()
|
---|
122 | : base() {
|
---|
123 | PunishmentFactor = 10.0;
|
---|
124 | MaxBestValidationSolutionAge = 20;
|
---|
125 | }
|
---|
126 |
|
---|
127 | protected override IOperator CreateFunctionLibraryInjector() {
|
---|
128 | return DefaultStructureIdentificationOperators.CreateFunctionLibraryInjector();
|
---|
129 | }
|
---|
130 |
|
---|
131 | protected override IOperator CreateProblemInjector() {
|
---|
132 | return DefaultRegressionOperators.CreateProblemInjector();
|
---|
133 | }
|
---|
134 |
|
---|
135 | protected override IOperator CreateInitialPopulationEvaluator() {
|
---|
136 | return DefaultStructureIdentificationOperators.CreateInitialPopulationEvaluator();
|
---|
137 | }
|
---|
138 |
|
---|
139 | protected override IOperator CreateEvaluationOperator() {
|
---|
140 | return DefaultStructureIdentificationOperators.CreateEvaluator();
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | protected override IOperator CreateGenerationStepHook() {
|
---|
145 | IOperator hook = DefaultStructureIdentificationOperators.CreateGenerationStepHook();
|
---|
146 | hook.AddSubOperator(CreateBestSolutionProcessor());
|
---|
147 | return hook;
|
---|
148 | }
|
---|
149 |
|
---|
150 | private IOperator CreateBestSolutionProcessor() {
|
---|
151 | CombinedOperator op = new CombinedOperator();
|
---|
152 | op.Name = "BestSolutionProcessor";
|
---|
153 | SequentialProcessor seq = new SequentialProcessor();
|
---|
154 |
|
---|
155 | ProgrammableOperator variableStorer = new ProgrammableOperator();
|
---|
156 | variableStorer.RemoveVariableInfo("Result");
|
---|
157 | variableStorer.AddVariableInfo(new VariableInfo("Input", "Value to copy", typeof(ObjectData), VariableKind.In));
|
---|
158 | variableStorer.AddVariableInfo(new VariableInfo("Output", "Value to write", typeof(ObjectData), VariableKind.Out));
|
---|
159 | variableStorer.Code = "scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(\"Output\"), (ObjectData)Input.Clone()));";
|
---|
160 |
|
---|
161 | IOperator evaluatedSolutionsStorer = (IOperator)variableStorer.Clone();
|
---|
162 | evaluatedSolutionsStorer.GetVariableInfo("Input").ActualName = "EvaluatedSolutions";
|
---|
163 | evaluatedSolutionsStorer.GetVariableInfo("Output").ActualName = "EvaluatedSolutions";
|
---|
164 |
|
---|
165 | IOperator selectionPressureStorer = (IOperator)variableStorer.Clone();
|
---|
166 | selectionPressureStorer.GetVariableInfo("Input").ActualName = "SelectionPressure";
|
---|
167 | selectionPressureStorer.GetVariableInfo("Output").ActualName = "SelectionPressure";
|
---|
168 |
|
---|
169 | ProgrammableOperator resetBestSolutionAge = new ProgrammableOperator();
|
---|
170 | resetBestSolutionAge.RemoveVariable("Result");
|
---|
171 | resetBestSolutionAge.AddVariableInfo(
|
---|
172 | new VariableInfo("BestValidationSolutionAge", "Age of best validation solution", typeof(IntData), VariableKind.In | VariableKind.Out));
|
---|
173 | resetBestSolutionAge.Code = "BestValidationSolutionAge.Data = 0;";
|
---|
174 |
|
---|
175 | seq.AddSubOperator(evaluatedSolutionsStorer);
|
---|
176 | seq.AddSubOperator(selectionPressureStorer);
|
---|
177 | seq.AddSubOperator(resetBestSolutionAge);
|
---|
178 |
|
---|
179 | op.OperatorGraph.AddOperator(seq);
|
---|
180 | op.OperatorGraph.InitialOperator = seq;
|
---|
181 | return op;
|
---|
182 | }
|
---|
183 |
|
---|
184 | protected override VariableInjector CreateGlobalInjector() {
|
---|
185 | VariableInjector injector = base.CreateGlobalInjector();
|
---|
186 | injector.AddVariable(new HeuristicLab.Core.Variable("PunishmentFactor", new DoubleData()));
|
---|
187 | injector.AddVariable(new HeuristicLab.Core.Variable("BestValidationSolutionAge", new IntData()));
|
---|
188 | injector.AddVariable(new HeuristicLab.Core.Variable("MaxBestValidationSolutionAge", new IntData()));
|
---|
189 | injector.AddVariable(new HeuristicLab.Core.Variable("MaxNumberOfTrainingSamples", new IntData(4000)));
|
---|
190 | return injector;
|
---|
191 | }
|
---|
192 |
|
---|
193 |
|
---|
194 | protected override IOperator CreateLoggingOperator() {
|
---|
195 | CombinedOperator loggingOperator = new CombinedOperator();
|
---|
196 | loggingOperator.Name = "Logging";
|
---|
197 | SequentialProcessor seq = new SequentialProcessor();
|
---|
198 |
|
---|
199 | DataCollector collector = new DataCollector();
|
---|
200 | ItemList<StringData> names = collector.GetVariable("VariableNames").GetValue<ItemList<StringData>>();
|
---|
201 | names.Add(new StringData("BestQuality"));
|
---|
202 | names.Add(new StringData("AverageQuality"));
|
---|
203 | names.Add(new StringData("WorstQuality"));
|
---|
204 | names.Add(new StringData("BestValidationQuality"));
|
---|
205 | names.Add(new StringData("AverageValidationQuality"));
|
---|
206 | names.Add(new StringData("WorstValidationQuality"));
|
---|
207 | names.Add(new StringData("EvaluatedSolutions"));
|
---|
208 | names.Add(new StringData("SelectionPressure"));
|
---|
209 | QualityLogger qualityLogger = new QualityLogger();
|
---|
210 | QualityLogger validationQualityLogger = new QualityLogger();
|
---|
211 | validationQualityLogger.GetVariableInfo("Quality").ActualName = "ValidationQuality";
|
---|
212 | validationQualityLogger.GetVariableInfo("QualityLog").ActualName = "ValidationQualityLog";
|
---|
213 | seq.AddSubOperator(collector);
|
---|
214 | seq.AddSubOperator(qualityLogger);
|
---|
215 | seq.AddSubOperator(validationQualityLogger);
|
---|
216 |
|
---|
217 | loggingOperator.OperatorGraph.AddOperator(seq);
|
---|
218 | loggingOperator.OperatorGraph.InitialOperator = seq;
|
---|
219 | return loggingOperator;
|
---|
220 | }
|
---|
221 |
|
---|
222 | protected override IOperator CreateTerminationCondition() {
|
---|
223 | CombinedOperator terminationCritertion = new CombinedOperator();
|
---|
224 | terminationCritertion.Name = "TerminationCondition";
|
---|
225 | GreaterThanComparator bestSolutionAge = new GreaterThanComparator();
|
---|
226 | bestSolutionAge.GetVariableInfo("LeftSide").ActualName = "BestValidationSolutionAge";
|
---|
227 | bestSolutionAge.GetVariableInfo("RightSide").ActualName = "MaxBestValidationSolutionAge";
|
---|
228 | bestSolutionAge.GetVariableInfo("Result").ActualName = "TerminationCriterion";
|
---|
229 |
|
---|
230 | IOperator combinedTerminationCriterion = AlgorithmBase.CombineTerminationCriterions(base.CreateTerminationCondition(), bestSolutionAge);
|
---|
231 |
|
---|
232 | terminationCritertion.OperatorGraph.AddOperator(combinedTerminationCriterion);
|
---|
233 | terminationCritertion.OperatorGraph.InitialOperator = combinedTerminationCriterion;
|
---|
234 | return terminationCritertion;
|
---|
235 | }
|
---|
236 |
|
---|
237 | protected override IOperator CreatePostProcessingOperator() {
|
---|
238 | CombinedOperator op = new CombinedOperator();
|
---|
239 | op.Name = "ModelAnalyser";
|
---|
240 | SequentialProcessor seq = new SequentialProcessor();
|
---|
241 | seq.AddSubOperator(DefaultStructureIdentificationOperators.CreatePreparationForPostProcessingOperator());
|
---|
242 |
|
---|
243 | UniformSequentialSubScopesProcessor subScopesProc = new UniformSequentialSubScopesProcessor();
|
---|
244 | SequentialProcessor solutionProc = new SequentialProcessor();
|
---|
245 | solutionProc.AddSubOperator(CreateModelAnalyzerOperator());
|
---|
246 |
|
---|
247 | subScopesProc.AddSubOperator(solutionProc);
|
---|
248 | seq.AddSubOperator(subScopesProc);
|
---|
249 |
|
---|
250 | op.OperatorGraph.AddOperator(seq);
|
---|
251 | op.OperatorGraph.InitialOperator = seq;
|
---|
252 | return op;
|
---|
253 | }
|
---|
254 |
|
---|
255 | protected virtual IOperator CreateModelAnalyzerOperator() {
|
---|
256 | return DefaultRegressionOperators.CreatePostProcessingOperator();
|
---|
257 | }
|
---|
258 |
|
---|
259 | protected CombinedOperator GetProblemInjector() {
|
---|
260 | return (CombinedOperator)GetInitializationOperator().SubOperators[0];
|
---|
261 | }
|
---|
262 |
|
---|
263 | protected virtual IAnalyzerModel CreateGPModel() {
|
---|
264 | IScope bestModelScope = Engine.GlobalScope.SubScopes[0];
|
---|
265 | var model = new AnalyzerModel();
|
---|
266 |
|
---|
267 | model.SetMetaData("SelectionPressure", bestModelScope.GetVariableValue<DoubleData>("SelectionPressure", false).Data);
|
---|
268 | DefaultStructureIdentificationOperators.PopulateAnalyzerModel(bestModelScope, model);
|
---|
269 | DefaultRegressionOperators.PopulateAnalyzerModel(bestModelScope, model);
|
---|
270 |
|
---|
271 | return model;
|
---|
272 | }
|
---|
273 | }
|
---|
274 | }
|
---|