1 | using HEAL.Attic;
|
---|
2 | using HeuristicLab.Algorithms.OESRALPS.Analyzers.Regression;
|
---|
3 | using HeuristicLab.Algorithms.OESRALPS.Evaluators;
|
---|
4 | using HeuristicLab.Common;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Optimization;
|
---|
7 | using HeuristicLab.Parameters;
|
---|
8 | using HeuristicLab.Problems.DataAnalysis;
|
---|
9 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
10 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
|
---|
11 | using System;
|
---|
12 | using System.Collections.Generic;
|
---|
13 | using System.Linq;
|
---|
14 | using System.Text;
|
---|
15 | using System.Threading.Tasks;
|
---|
16 |
|
---|
17 | namespace HeuristicLab.Algorithms.OESRALPS.Problems
|
---|
18 | {
|
---|
19 | [Item("Symbolic Regression Sliding Window MSE Problem (single-objective)", "Represents a single objective symbolic regression problem.")]
|
---|
20 | [StorableType("7DDCF683-92FC-4A70-BF4F-FE3A0B0DE110")]
|
---|
21 | [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 101)]
|
---|
22 | class SymbolicRegressionSingleObjectiveSlidingWindowMSEProblem : SymbolicDataAnalysisSingleObjectiveProblem<IRegressionProblemData, ISymbolicRegressionSingleObjectiveEvaluator, ISymbolicDataAnalysisSolutionCreator>, IRegressionProblem
|
---|
23 | {
|
---|
24 | private const double PunishmentFactor = 10;
|
---|
25 | private const int InitialMaximumTreeDepth = 12;
|
---|
26 | private const int InitialMaximumTreeLength = 50;
|
---|
27 | private const string EstimationLimitsParameterName = "EstimationLimits";
|
---|
28 | private const string EstimationLimitsParameterDescription = "The limits for the estimated value that can be returned by the symbolic regression model.";
|
---|
29 |
|
---|
30 | #region parameter properties
|
---|
31 | public IFixedValueParameter<DoubleLimit> EstimationLimitsParameter {
|
---|
32 | get { return (IFixedValueParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
|
---|
33 | }
|
---|
34 | #endregion
|
---|
35 | #region properties
|
---|
36 | public DoubleLimit EstimationLimits {
|
---|
37 | get { return EstimationLimitsParameter.Value; }
|
---|
38 | }
|
---|
39 | #endregion
|
---|
40 | [StorableConstructor]
|
---|
41 | protected SymbolicRegressionSingleObjectiveSlidingWindowMSEProblem(StorableConstructorFlag _) : base(_) { }
|
---|
42 | protected SymbolicRegressionSingleObjectiveSlidingWindowMSEProblem(SymbolicRegressionSingleObjectiveSlidingWindowMSEProblem original, Cloner cloner)
|
---|
43 | : base(original, cloner)
|
---|
44 | {
|
---|
45 | RegisterEventHandlers();
|
---|
46 | }
|
---|
47 | public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicRegressionSingleObjectiveSlidingWindowMSEProblem(this, cloner); }
|
---|
48 |
|
---|
49 | public SymbolicRegressionSingleObjectiveSlidingWindowMSEProblem()
|
---|
50 | : base(new RegressionProblemData(), new SymbolicRegressionSingleObjectiveMeanSquaredErrorSlidingWindowEvaluator(), new SymbolicDataAnalysisExpressionTreeCreator())
|
---|
51 | {
|
---|
52 | Parameters.Add(new FixedValueParameter<DoubleLimit>(EstimationLimitsParameterName, EstimationLimitsParameterDescription));
|
---|
53 |
|
---|
54 | EstimationLimitsParameter.Hidden = true;
|
---|
55 |
|
---|
56 |
|
---|
57 | ApplyLinearScalingParameter.Value.Value = true;
|
---|
58 | Maximization.Value = false;
|
---|
59 | MaximumSymbolicExpressionTreeDepth.Value = InitialMaximumTreeDepth;
|
---|
60 | MaximumSymbolicExpressionTreeLength.Value = InitialMaximumTreeLength;
|
---|
61 |
|
---|
62 | RegisterEventHandlers();
|
---|
63 | ConfigureGrammarSymbols();
|
---|
64 | InitializeOperators();
|
---|
65 | UpdateEstimationLimits();
|
---|
66 | }
|
---|
67 |
|
---|
68 | [StorableHook(HookType.AfterDeserialization)]
|
---|
69 | private void AfterDeserialization()
|
---|
70 | {
|
---|
71 | RegisterEventHandlers();
|
---|
72 | // compatibility
|
---|
73 | bool changed = false;
|
---|
74 | if (!Operators.OfType<SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer>().Any())
|
---|
75 | {
|
---|
76 | Operators.Add(new SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer());
|
---|
77 | changed = true;
|
---|
78 | }
|
---|
79 | if (!Operators.OfType<SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer>().Any())
|
---|
80 | {
|
---|
81 | Operators.Add(new SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer());
|
---|
82 | changed = true;
|
---|
83 | }
|
---|
84 | if (!Operators.OfType<SymbolicRegressionSolutionsAnalyzer>().Any())
|
---|
85 | {
|
---|
86 | Operators.Add(new SymbolicRegressionSolutionsAnalyzer());
|
---|
87 | changed = true;
|
---|
88 | }
|
---|
89 | if (changed)
|
---|
90 | {
|
---|
91 | ParameterizeOperators();
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | private void RegisterEventHandlers()
|
---|
96 | {
|
---|
97 | SymbolicExpressionTreeGrammarParameter.ValueChanged += (o, e) => ConfigureGrammarSymbols();
|
---|
98 | }
|
---|
99 |
|
---|
100 | private void ConfigureGrammarSymbols()
|
---|
101 | {
|
---|
102 | var grammar = SymbolicExpressionTreeGrammar as TypeCoherentExpressionGrammar;
|
---|
103 | if (grammar != null) grammar.ConfigureAsDefaultRegressionGrammar();
|
---|
104 | }
|
---|
105 |
|
---|
106 | private void InitializeOperators()
|
---|
107 | {
|
---|
108 | Operators.Add(new SymbolicRegressionSingleObjectiveValidationLayerBestSolutionSlidingWindowAnalyzer());
|
---|
109 | Operators.Add(new SymbolicRegressionSingleObjectiveTrainingBestSolutionAnalyzer());
|
---|
110 | Operators.Add(new SymbolicRegressionSingleObjectiveValidationBestSolutionAnalyzer());
|
---|
111 | Operators.Add(new SymbolicRegressionSingleObjectiveOverfittingAnalyzer());
|
---|
112 | Operators.Add(new SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer());
|
---|
113 | Operators.Add(new SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer());
|
---|
114 | Operators.Add(new SymbolicRegressionSolutionsAnalyzer());
|
---|
115 | Operators.Add(new SymbolicExpressionTreePhenotypicSimilarityCalculator());
|
---|
116 | Operators.Add(new SymbolicRegressionPhenotypicDiversityAnalyzer(Operators.OfType<SymbolicExpressionTreePhenotypicSimilarityCalculator>()) { DiversityResultName = "Phenotypic Diversity" });
|
---|
117 | ParameterizeOperators();
|
---|
118 | }
|
---|
119 |
|
---|
120 | private void UpdateEstimationLimits()
|
---|
121 | {
|
---|
122 | if (ProblemData.TrainingIndices.Any())
|
---|
123 | {
|
---|
124 | var targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).ToList();
|
---|
125 | var mean = targetValues.Average();
|
---|
126 | var range = targetValues.Max() - targetValues.Min();
|
---|
127 | EstimationLimits.Upper = mean + PunishmentFactor * range;
|
---|
128 | EstimationLimits.Lower = mean - PunishmentFactor * range;
|
---|
129 | }
|
---|
130 | else
|
---|
131 | {
|
---|
132 | EstimationLimits.Upper = double.MaxValue;
|
---|
133 | EstimationLimits.Lower = double.MinValue;
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | protected override void OnProblemDataChanged()
|
---|
138 | {
|
---|
139 | base.OnProblemDataChanged();
|
---|
140 | UpdateEstimationLimits();
|
---|
141 | }
|
---|
142 |
|
---|
143 | protected override void ParameterizeOperators()
|
---|
144 | {
|
---|
145 | base.ParameterizeOperators();
|
---|
146 | if (Parameters.ContainsKey(EstimationLimitsParameterName))
|
---|
147 | {
|
---|
148 | var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
|
---|
149 | foreach (var op in operators.OfType<ISymbolicDataAnalysisBoundedOperator>())
|
---|
150 | {
|
---|
151 | op.EstimationLimitsParameter.ActualName = EstimationLimitsParameter.Name;
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | foreach (var op in Operators.OfType<ISolutionSimilarityCalculator>())
|
---|
156 | {
|
---|
157 | op.SolutionVariableName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
158 | op.QualityVariableName = Evaluator.QualityParameter.ActualName;
|
---|
159 |
|
---|
160 | if (op is SymbolicExpressionTreePhenotypicSimilarityCalculator)
|
---|
161 | {
|
---|
162 | var phenotypicSimilarityCalculator = (SymbolicExpressionTreePhenotypicSimilarityCalculator)op;
|
---|
163 | phenotypicSimilarityCalculator.ProblemData = ProblemData;
|
---|
164 | phenotypicSimilarityCalculator.Interpreter = SymbolicExpressionTreeInterpreter;
|
---|
165 | }
|
---|
166 | }
|
---|
167 | }
|
---|
168 | }
|
---|
169 | } |
---|