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 |
|
---|
34 | namespace HeuristicLab.GP.StructureIdentification {
|
---|
35 | public class OffspringSelectionGPRegression : HeuristicLab.GP.Algorithms.OffspringSelectionGP, IAlgorithm {
|
---|
36 | public override string Name { get { return "OffspringSelectionGP - StructureIdentification"; } }
|
---|
37 |
|
---|
38 | public virtual int TargetVariable {
|
---|
39 | get { return ProblemInjector.GetVariableValue<IntData>("TargetVariable", null, false).Data; }
|
---|
40 | set { ProblemInjector.GetVariableValue<IntData>("TargetVariable", null, false).Data = value; }
|
---|
41 | }
|
---|
42 |
|
---|
43 | public virtual Dataset Dataset {
|
---|
44 | get { return ProblemInjector.GetVariableValue<Dataset>("Dataset", null, false); }
|
---|
45 | set { ProblemInjector.GetVariable("Dataset").Value = value; }
|
---|
46 | }
|
---|
47 |
|
---|
48 | public virtual IAnalyzerModel Model {
|
---|
49 | get {
|
---|
50 | if (!Engine.Terminated)
|
---|
51 | throw new InvalidOperationException("The algorithm is still running. Wait until the algorithm is terminated to retrieve the result.");
|
---|
52 | else
|
---|
53 | return CreateGPModel();
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | public virtual double PunishmentFactor {
|
---|
58 | get { return GetVariableInjector().GetVariable("PunishmentFactor").GetValue<DoubleData>().Data; }
|
---|
59 | set { GetVariableInjector().GetVariable("PunishmentFactor").GetValue<DoubleData>().Data = value; }
|
---|
60 | }
|
---|
61 |
|
---|
62 | public override IOperator ProblemInjector {
|
---|
63 | get { return GetProblemInjector().OperatorGraph.InitialOperator.SubOperators[0]; }
|
---|
64 | set {
|
---|
65 | value.Name = "ProblemInjector";
|
---|
66 | CombinedOperator problemInjector = GetProblemInjector();
|
---|
67 | problemInjector.OperatorGraph.RemoveOperator(ProblemInjector.Guid);
|
---|
68 | problemInjector.OperatorGraph.AddOperator(value);
|
---|
69 | problemInjector.OperatorGraph.InitialOperator.AddSubOperator(value, 0);
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | public OffspringSelectionGPRegression()
|
---|
74 | : base() {
|
---|
75 | PunishmentFactor = 10.0;
|
---|
76 | }
|
---|
77 |
|
---|
78 | protected override IOperator CreateFunctionLibraryInjector() {
|
---|
79 | return DefaultStructureIdentificationOperators.CreateFunctionLibraryInjector();
|
---|
80 | }
|
---|
81 |
|
---|
82 | protected override IOperator CreateProblemInjector() {
|
---|
83 | return DefaultRegressionOperators.CreateProblemInjector();
|
---|
84 | }
|
---|
85 |
|
---|
86 | protected override IOperator CreateInitialPopulationEvaluator() {
|
---|
87 | return DefaultStructureIdentificationOperators.CreateInitialPopulationEvaluator();
|
---|
88 | }
|
---|
89 |
|
---|
90 | protected override IOperator CreateEvaluationOperator() {
|
---|
91 | return DefaultStructureIdentificationOperators.CreateEvaluator();
|
---|
92 | }
|
---|
93 |
|
---|
94 |
|
---|
95 | protected override IOperator CreateGenerationStepHook() {
|
---|
96 | return DefaultStructureIdentificationOperators.CreateGenerationStepHook();
|
---|
97 | }
|
---|
98 |
|
---|
99 | protected override VariableInjector CreateGlobalInjector() {
|
---|
100 | VariableInjector injector = base.CreateGlobalInjector();
|
---|
101 | injector.AddVariable(new HeuristicLab.Core.Variable("PunishmentFactor", new DoubleData()));
|
---|
102 | return injector;
|
---|
103 | }
|
---|
104 |
|
---|
105 |
|
---|
106 | protected override IOperator CreateLoggingOperator() {
|
---|
107 | CombinedOperator loggingOperator = new CombinedOperator();
|
---|
108 | loggingOperator.Name = "Logging";
|
---|
109 | SequentialProcessor seq = new SequentialProcessor();
|
---|
110 |
|
---|
111 | DataCollector collector = new DataCollector();
|
---|
112 | ItemList<StringData> names = collector.GetVariable("VariableNames").GetValue<ItemList<StringData>>();
|
---|
113 | names.Add(new StringData("BestQuality"));
|
---|
114 | names.Add(new StringData("AverageQuality"));
|
---|
115 | names.Add(new StringData("WorstQuality"));
|
---|
116 | names.Add(new StringData("BestValidationQuality"));
|
---|
117 | names.Add(new StringData("AverageValidationQuality"));
|
---|
118 | names.Add(new StringData("WorstValidationQuality"));
|
---|
119 | names.Add(new StringData("EvaluatedSolutions"));
|
---|
120 | names.Add(new StringData("SelectionPressure"));
|
---|
121 | QualityLogger qualityLogger = new QualityLogger();
|
---|
122 | QualityLogger validationQualityLogger = new QualityLogger();
|
---|
123 | validationQualityLogger.GetVariableInfo("Quality").ActualName = "ValidationQuality";
|
---|
124 | validationQualityLogger.GetVariableInfo("QualityLog").ActualName = "ValidationQualityLog";
|
---|
125 | seq.AddSubOperator(collector);
|
---|
126 | seq.AddSubOperator(qualityLogger);
|
---|
127 | seq.AddSubOperator(validationQualityLogger);
|
---|
128 |
|
---|
129 | loggingOperator.OperatorGraph.AddOperator(seq);
|
---|
130 | loggingOperator.OperatorGraph.InitialOperator = seq;
|
---|
131 | return loggingOperator;
|
---|
132 | }
|
---|
133 |
|
---|
134 | protected override IOperator CreatePostProcessingOperator() {
|
---|
135 | CombinedOperator op = new CombinedOperator();
|
---|
136 | op.Name = "ModelAnalyser";
|
---|
137 | SequentialProcessor seq = new SequentialProcessor();
|
---|
138 | seq.AddSubOperator(DefaultStructureIdentificationOperators.CreatePreparationForPostProcessingOperator());
|
---|
139 |
|
---|
140 | UniformSequentialSubScopesProcessor subScopesProc = new UniformSequentialSubScopesProcessor();
|
---|
141 | SequentialProcessor solutionProc = new SequentialProcessor();
|
---|
142 | solutionProc.AddSubOperator(CreateModelAnalyzerOperator());
|
---|
143 |
|
---|
144 | subScopesProc.AddSubOperator(solutionProc);
|
---|
145 | seq.AddSubOperator(subScopesProc);
|
---|
146 |
|
---|
147 | op.OperatorGraph.AddOperator(seq);
|
---|
148 | op.OperatorGraph.InitialOperator = seq;
|
---|
149 | return op;
|
---|
150 | }
|
---|
151 |
|
---|
152 | protected virtual IOperator CreateModelAnalyzerOperator() {
|
---|
153 | return DefaultRegressionOperators.CreatePostProcessingOperator();
|
---|
154 | }
|
---|
155 |
|
---|
156 | protected CombinedOperator GetProblemInjector() {
|
---|
157 | return (CombinedOperator)GetInitializationOperator().SubOperators[0];
|
---|
158 | }
|
---|
159 |
|
---|
160 | protected virtual IAnalyzerModel CreateGPModel() {
|
---|
161 | IScope bestModelScope = Engine.GlobalScope.SubScopes[0];
|
---|
162 | var model = new AnalyzerModel();
|
---|
163 |
|
---|
164 | model.SetMetaData("SelectionPressure", bestModelScope.GetVariableValue<DoubleData>("SelectionPressure", false).Data);
|
---|
165 | DefaultStructureIdentificationOperators.PopulateAnalyzerModel(bestModelScope, model);
|
---|
166 | DefaultRegressionOperators.PopulateAnalyzerModel(bestModelScope, model);
|
---|
167 |
|
---|
168 | return model;
|
---|
169 | }
|
---|
170 | }
|
---|
171 | }
|
---|