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 HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Evolutionary;
|
---|
27 | using HeuristicLab.Logging;
|
---|
28 | using HeuristicLab.Operators;
|
---|
29 | using HeuristicLab.Selection;
|
---|
30 | using HeuristicLab.Selection.OffspringSelection;
|
---|
31 | using HeuristicLab.Modeling;
|
---|
32 | using HeuristicLab.DataAnalysis;
|
---|
33 | using HeuristicLab.Operators.Programmable;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.GP.StructureIdentification {
|
---|
36 | public class OffspringSelectionGPRegression : HeuristicLab.GP.Algorithms.OffspringSelectionGP, IAlgorithm {
|
---|
37 | public override string Name { get { return "OffspringSelectionGP - StructureIdentification"; } }
|
---|
38 |
|
---|
39 | public virtual int TargetVariable {
|
---|
40 | get { return ProblemInjector.GetVariableValue<IntData>("TargetVariable", null, false).Data; }
|
---|
41 | set { ProblemInjector.GetVariableValue<IntData>("TargetVariable", null, false).Data = value; }
|
---|
42 | }
|
---|
43 |
|
---|
44 | public virtual Dataset Dataset {
|
---|
45 | get { return ProblemInjector.GetVariableValue<Dataset>("Dataset", null, false); }
|
---|
46 | set { ProblemInjector.GetVariable("Dataset").Value = value; }
|
---|
47 | }
|
---|
48 |
|
---|
49 | public virtual IAnalyzerModel Model {
|
---|
50 | get {
|
---|
51 | if (!Engine.Terminated)
|
---|
52 | throw new InvalidOperationException("The algorithm is still running. Wait until the algorithm is terminated to retrieve the result.");
|
---|
53 | else
|
---|
54 | return CreateGPModel();
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | public virtual double PunishmentFactor {
|
---|
59 | get { return GetVariableInjector().GetVariable("PunishmentFactor").GetValue<DoubleData>().Data; }
|
---|
60 | set { GetVariableInjector().GetVariable("PunishmentFactor").GetValue<DoubleData>().Data = value; }
|
---|
61 | }
|
---|
62 |
|
---|
63 | public override IOperator ProblemInjector {
|
---|
64 | get { return GetProblemInjector().OperatorGraph.InitialOperator.SubOperators[0]; }
|
---|
65 | set {
|
---|
66 | value.Name = "ProblemInjector";
|
---|
67 | CombinedOperator problemInjector = GetProblemInjector();
|
---|
68 | problemInjector.OperatorGraph.RemoveOperator(ProblemInjector.Guid);
|
---|
69 | problemInjector.OperatorGraph.AddOperator(value);
|
---|
70 | problemInjector.OperatorGraph.InitialOperator.AddSubOperator(value, 0);
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | public OffspringSelectionGPRegression()
|
---|
75 | : base() {
|
---|
76 | PunishmentFactor = 10.0;
|
---|
77 | }
|
---|
78 |
|
---|
79 | protected override IOperator CreateFunctionLibraryInjector() {
|
---|
80 | return DefaultStructureIdentificationOperators.CreateFunctionLibraryInjector();
|
---|
81 | }
|
---|
82 |
|
---|
83 | protected override IOperator CreateProblemInjector() {
|
---|
84 | return DefaultRegressionOperators.CreateProblemInjector();
|
---|
85 | }
|
---|
86 |
|
---|
87 | protected override IOperator CreateInitialPopulationEvaluator() {
|
---|
88 | return DefaultStructureIdentificationOperators.CreateInitialPopulationEvaluator();
|
---|
89 | }
|
---|
90 |
|
---|
91 | protected override IOperator CreateEvaluationOperator() {
|
---|
92 | return DefaultStructureIdentificationOperators.CreateEvaluator();
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | protected override IOperator CreateGenerationStepHook() {
|
---|
97 | IOperator hook = DefaultStructureIdentificationOperators.CreateGenerationStepHook();
|
---|
98 | hook.AddSubOperator(CreateBestSolutionProcessor());
|
---|
99 | return hook;
|
---|
100 | }
|
---|
101 |
|
---|
102 | private IOperator CreateBestSolutionProcessor() {
|
---|
103 | CombinedOperator op = new CombinedOperator();
|
---|
104 | op.Name = "BestSolutionProcessor";
|
---|
105 | SequentialProcessor seq = new SequentialProcessor();
|
---|
106 |
|
---|
107 | ProgrammableOperator variableStorer = new ProgrammableOperator();
|
---|
108 | variableStorer.RemoveVariableInfo("Result");
|
---|
109 | variableStorer.AddVariableInfo(new VariableInfo("Input", "Value to copy", typeof(ObjectData), VariableKind.In));
|
---|
110 | variableStorer.AddVariableInfo(new VariableInfo("Output", "Value to write", typeof(ObjectData), VariableKind.Out));
|
---|
111 | variableStorer.Code = "scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(\"Output\"), (ObjectData)Input.Clone()));";
|
---|
112 |
|
---|
113 | IOperator evaluatedSolutionsStorer = (IOperator)variableStorer.Clone();
|
---|
114 | evaluatedSolutionsStorer.GetVariableInfo("Input").ActualName = "EvaluatedSolutions";
|
---|
115 | evaluatedSolutionsStorer.GetVariableInfo("Output").ActualName = "EvaluatedSolutions";
|
---|
116 |
|
---|
117 | IOperator selectionPressureStorer = (IOperator)variableStorer.Clone();
|
---|
118 | selectionPressureStorer.GetVariableInfo("Input").ActualName = "SelectionPressure";
|
---|
119 | selectionPressureStorer.GetVariableInfo("Output").ActualName = "SelectionPressure";
|
---|
120 |
|
---|
121 | seq.AddSubOperator(evaluatedSolutionsStorer);
|
---|
122 | seq.AddSubOperator(selectionPressureStorer);
|
---|
123 |
|
---|
124 | op.OperatorGraph.AddOperator(seq);
|
---|
125 | op.OperatorGraph.InitialOperator = seq;
|
---|
126 | return op;
|
---|
127 | }
|
---|
128 |
|
---|
129 | protected override VariableInjector CreateGlobalInjector() {
|
---|
130 | VariableInjector injector = base.CreateGlobalInjector();
|
---|
131 | injector.AddVariable(new HeuristicLab.Core.Variable("PunishmentFactor", new DoubleData()));
|
---|
132 | return injector;
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | protected override IOperator CreateLoggingOperator() {
|
---|
137 | CombinedOperator loggingOperator = new CombinedOperator();
|
---|
138 | loggingOperator.Name = "Logging";
|
---|
139 | SequentialProcessor seq = new SequentialProcessor();
|
---|
140 |
|
---|
141 | DataCollector collector = new DataCollector();
|
---|
142 | ItemList<StringData> names = collector.GetVariable("VariableNames").GetValue<ItemList<StringData>>();
|
---|
143 | names.Add(new StringData("BestQuality"));
|
---|
144 | names.Add(new StringData("AverageQuality"));
|
---|
145 | names.Add(new StringData("WorstQuality"));
|
---|
146 | names.Add(new StringData("BestValidationQuality"));
|
---|
147 | names.Add(new StringData("AverageValidationQuality"));
|
---|
148 | names.Add(new StringData("WorstValidationQuality"));
|
---|
149 | names.Add(new StringData("EvaluatedSolutions"));
|
---|
150 | names.Add(new StringData("SelectionPressure"));
|
---|
151 | QualityLogger qualityLogger = new QualityLogger();
|
---|
152 | QualityLogger validationQualityLogger = new QualityLogger();
|
---|
153 | validationQualityLogger.GetVariableInfo("Quality").ActualName = "ValidationQuality";
|
---|
154 | validationQualityLogger.GetVariableInfo("QualityLog").ActualName = "ValidationQualityLog";
|
---|
155 | seq.AddSubOperator(collector);
|
---|
156 | seq.AddSubOperator(qualityLogger);
|
---|
157 | seq.AddSubOperator(validationQualityLogger);
|
---|
158 |
|
---|
159 | loggingOperator.OperatorGraph.AddOperator(seq);
|
---|
160 | loggingOperator.OperatorGraph.InitialOperator = seq;
|
---|
161 | return loggingOperator;
|
---|
162 | }
|
---|
163 |
|
---|
164 | protected override IOperator CreatePostProcessingOperator() {
|
---|
165 | CombinedOperator op = new CombinedOperator();
|
---|
166 | op.Name = "ModelAnalyser";
|
---|
167 | SequentialProcessor seq = new SequentialProcessor();
|
---|
168 | seq.AddSubOperator(DefaultStructureIdentificationOperators.CreatePreparationForPostProcessingOperator());
|
---|
169 |
|
---|
170 | UniformSequentialSubScopesProcessor subScopesProc = new UniformSequentialSubScopesProcessor();
|
---|
171 | SequentialProcessor solutionProc = new SequentialProcessor();
|
---|
172 | solutionProc.AddSubOperator(CreateModelAnalyzerOperator());
|
---|
173 |
|
---|
174 | subScopesProc.AddSubOperator(solutionProc);
|
---|
175 | seq.AddSubOperator(subScopesProc);
|
---|
176 |
|
---|
177 | op.OperatorGraph.AddOperator(seq);
|
---|
178 | op.OperatorGraph.InitialOperator = seq;
|
---|
179 | return op;
|
---|
180 | }
|
---|
181 |
|
---|
182 | protected virtual IOperator CreateModelAnalyzerOperator() {
|
---|
183 | return DefaultRegressionOperators.CreatePostProcessingOperator();
|
---|
184 | }
|
---|
185 |
|
---|
186 | protected CombinedOperator GetProblemInjector() {
|
---|
187 | return (CombinedOperator)GetInitializationOperator().SubOperators[0];
|
---|
188 | }
|
---|
189 |
|
---|
190 | protected virtual IAnalyzerModel CreateGPModel() {
|
---|
191 | IScope bestModelScope = Engine.GlobalScope.SubScopes[0];
|
---|
192 | var model = new AnalyzerModel();
|
---|
193 |
|
---|
194 | model.SetMetaData("SelectionPressure", bestModelScope.GetVariableValue<DoubleData>("SelectionPressure", false).Data);
|
---|
195 | DefaultStructureIdentificationOperators.PopulateAnalyzerModel(bestModelScope, model);
|
---|
196 | DefaultRegressionOperators.PopulateAnalyzerModel(bestModelScope, model);
|
---|
197 |
|
---|
198 | return model;
|
---|
199 | }
|
---|
200 | }
|
---|
201 | }
|
---|