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 int TargetVariable {
|
---|
42 | get { return ProblemInjector.GetVariableValue<IntData>("TargetVariable", null, false).Data; }
|
---|
43 | set { ProblemInjector.GetVariableValue<IntData>("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<int> AllowedVariables {
|
---|
60 | get {
|
---|
61 | ItemList<IntData> allowedVariables = ProblemInjector.GetVariableValue<ItemList<IntData>>("AllowedFeatures", null, false);
|
---|
62 | return allowedVariables.Select(x => x.Data);
|
---|
63 | }
|
---|
64 | set {
|
---|
65 | ItemList<IntData> allowedVariables = ProblemInjector.GetVariableValue<ItemList<IntData>>("AllowedFeatures", null, false);
|
---|
66 | foreach (int x in value) allowedVariables.Add(new IntData(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 | return injector;
|
---|
190 | }
|
---|
191 |
|
---|
192 |
|
---|
193 | protected override IOperator CreateLoggingOperator() {
|
---|
194 | CombinedOperator loggingOperator = new CombinedOperator();
|
---|
195 | loggingOperator.Name = "Logging";
|
---|
196 | SequentialProcessor seq = new SequentialProcessor();
|
---|
197 |
|
---|
198 | DataCollector collector = new DataCollector();
|
---|
199 | ItemList<StringData> names = collector.GetVariable("VariableNames").GetValue<ItemList<StringData>>();
|
---|
200 | names.Add(new StringData("BestQuality"));
|
---|
201 | names.Add(new StringData("AverageQuality"));
|
---|
202 | names.Add(new StringData("WorstQuality"));
|
---|
203 | names.Add(new StringData("BestValidationQuality"));
|
---|
204 | names.Add(new StringData("AverageValidationQuality"));
|
---|
205 | names.Add(new StringData("WorstValidationQuality"));
|
---|
206 | names.Add(new StringData("EvaluatedSolutions"));
|
---|
207 | names.Add(new StringData("SelectionPressure"));
|
---|
208 | QualityLogger qualityLogger = new QualityLogger();
|
---|
209 | QualityLogger validationQualityLogger = new QualityLogger();
|
---|
210 | validationQualityLogger.GetVariableInfo("Quality").ActualName = "ValidationQuality";
|
---|
211 | validationQualityLogger.GetVariableInfo("QualityLog").ActualName = "ValidationQualityLog";
|
---|
212 | seq.AddSubOperator(collector);
|
---|
213 | seq.AddSubOperator(qualityLogger);
|
---|
214 | seq.AddSubOperator(validationQualityLogger);
|
---|
215 |
|
---|
216 | loggingOperator.OperatorGraph.AddOperator(seq);
|
---|
217 | loggingOperator.OperatorGraph.InitialOperator = seq;
|
---|
218 | return loggingOperator;
|
---|
219 | }
|
---|
220 |
|
---|
221 | protected override IOperator CreateTerminationCondition() {
|
---|
222 | CombinedOperator terminationCritertion = new CombinedOperator();
|
---|
223 | terminationCritertion.Name = "TerminationCondition";
|
---|
224 | GreaterThanComparator bestSolutionAge = new GreaterThanComparator();
|
---|
225 | bestSolutionAge.GetVariableInfo("LeftSide").ActualName = "BestValidationSolutionAge";
|
---|
226 | bestSolutionAge.GetVariableInfo("RightSide").ActualName = "MaxBestValidationSolutionAge";
|
---|
227 | bestSolutionAge.GetVariableInfo("Result").ActualName = "TerminationCriterion";
|
---|
228 |
|
---|
229 | IOperator combinedTerminationCriterion = AlgorithmBase.CombineTerminationCriterions(base.CreateTerminationCondition(), bestSolutionAge);
|
---|
230 |
|
---|
231 | terminationCritertion.OperatorGraph.AddOperator(combinedTerminationCriterion);
|
---|
232 | terminationCritertion.OperatorGraph.InitialOperator = combinedTerminationCriterion;
|
---|
233 | return terminationCritertion;
|
---|
234 | }
|
---|
235 |
|
---|
236 | protected override IOperator CreatePostProcessingOperator() {
|
---|
237 | CombinedOperator op = new CombinedOperator();
|
---|
238 | op.Name = "ModelAnalyser";
|
---|
239 | SequentialProcessor seq = new SequentialProcessor();
|
---|
240 | seq.AddSubOperator(DefaultStructureIdentificationOperators.CreatePreparationForPostProcessingOperator());
|
---|
241 |
|
---|
242 | UniformSequentialSubScopesProcessor subScopesProc = new UniformSequentialSubScopesProcessor();
|
---|
243 | SequentialProcessor solutionProc = new SequentialProcessor();
|
---|
244 | solutionProc.AddSubOperator(CreateModelAnalyzerOperator());
|
---|
245 |
|
---|
246 | subScopesProc.AddSubOperator(solutionProc);
|
---|
247 | seq.AddSubOperator(subScopesProc);
|
---|
248 |
|
---|
249 | op.OperatorGraph.AddOperator(seq);
|
---|
250 | op.OperatorGraph.InitialOperator = seq;
|
---|
251 | return op;
|
---|
252 | }
|
---|
253 |
|
---|
254 | protected virtual IOperator CreateModelAnalyzerOperator() {
|
---|
255 | return DefaultRegressionOperators.CreatePostProcessingOperator();
|
---|
256 | }
|
---|
257 |
|
---|
258 | protected CombinedOperator GetProblemInjector() {
|
---|
259 | return (CombinedOperator)GetInitializationOperator().SubOperators[0];
|
---|
260 | }
|
---|
261 |
|
---|
262 | protected virtual IAnalyzerModel CreateGPModel() {
|
---|
263 | IScope bestModelScope = Engine.GlobalScope.SubScopes[0];
|
---|
264 | var model = new AnalyzerModel();
|
---|
265 |
|
---|
266 | model.SetMetaData("SelectionPressure", bestModelScope.GetVariableValue<DoubleData>("SelectionPressure", false).Data);
|
---|
267 | DefaultStructureIdentificationOperators.PopulateAnalyzerModel(bestModelScope, model);
|
---|
268 | DefaultRegressionOperators.PopulateAnalyzerModel(bestModelScope, model);
|
---|
269 |
|
---|
270 | return model;
|
---|
271 | }
|
---|
272 | }
|
---|
273 | }
|
---|