1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2013 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.Linq;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
26 | using HeuristicLab.Parameters;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 | using HeuristicLab.Problems.DataAnalysis;
|
---|
29 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
30 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Problems.GrammaticalEvolution {
|
---|
33 | [Item("Grammatical Evolution Symbolic Regression Problem (single objective)", "Represents a single objective symbolic regression problem, implemented in Grammatical Evolution.")]
|
---|
34 | [StorableClass]
|
---|
35 | [Creatable("Problems")]
|
---|
36 | public class GESymbolicRegressionSingleObjectiveProblem : GESymbolicDataAnalysisSingleObjectiveProblem<IRegressionProblemData, IGESymbolicRegressionSingleObjectiveEvaluator, IIntegerVectorCreator>,
|
---|
37 | IRegressionProblem {
|
---|
38 | private const double PunishmentFactor = 10;
|
---|
39 | //private const int InitialMaximumTreeDepth = 8;
|
---|
40 | private const int InitialMaximumTreeLength = 25;
|
---|
41 | private const string EstimationLimitsParameterName = "EstimationLimits";
|
---|
42 | private const string EstimationLimitsParameterDescription = "The limits for the estimated value that can be returned by the symbolic regression model.";
|
---|
43 |
|
---|
44 | #region parameter properties
|
---|
45 | public IFixedValueParameter<DoubleLimit> EstimationLimitsParameter {
|
---|
46 | get { return (IFixedValueParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
|
---|
47 | }
|
---|
48 | #endregion
|
---|
49 | #region properties
|
---|
50 | public DoubleLimit EstimationLimits {
|
---|
51 | get { return EstimationLimitsParameter.Value; }
|
---|
52 | }
|
---|
53 | #endregion
|
---|
54 | [StorableConstructor]
|
---|
55 | protected GESymbolicRegressionSingleObjectiveProblem(bool deserializing) : base(deserializing) { }
|
---|
56 | protected GESymbolicRegressionSingleObjectiveProblem(GESymbolicRegressionSingleObjectiveProblem original, Cloner cloner)
|
---|
57 | : base(original, cloner) {
|
---|
58 | RegisterEventHandlers();
|
---|
59 | }
|
---|
60 | public override IDeepCloneable Clone(Cloner cloner) { return new GESymbolicRegressionSingleObjectiveProblem(this, cloner); }
|
---|
61 |
|
---|
62 | public GESymbolicRegressionSingleObjectiveProblem()
|
---|
63 | : base(new RegressionProblemData(), new GESymbolicRegressionSingleObjectivePearsonRSquaredEvaluator(), new UniformRandomIntegerVectorCreator()) {
|
---|
64 | Parameters.Add(new FixedValueParameter<DoubleLimit>(EstimationLimitsParameterName, EstimationLimitsParameterDescription));
|
---|
65 |
|
---|
66 | EstimationLimitsParameter.Hidden = true;
|
---|
67 |
|
---|
68 |
|
---|
69 | ApplyLinearScalingParameter.Value.Value = true;
|
---|
70 | Maximization.Value = true;
|
---|
71 | //MaximumSymbolicExpressionTreeDepth.Value = InitialMaximumTreeDepth;
|
---|
72 | MaximumSymbolicExpressionTreeLength.Value = InitialMaximumTreeLength;
|
---|
73 |
|
---|
74 | RegisterEventHandlers();
|
---|
75 | ConfigureGrammarSymbols();
|
---|
76 | InitializeOperators();
|
---|
77 | UpdateEstimationLimits();
|
---|
78 | }
|
---|
79 |
|
---|
80 | [StorableHook(HookType.AfterDeserialization)]
|
---|
81 | private void AfterDeserialization() {
|
---|
82 | RegisterEventHandlers();
|
---|
83 | // compatibility
|
---|
84 | bool changed = false;
|
---|
85 | if (!Operators.OfType<SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer>().Any()) {
|
---|
86 | Operators.Add(new SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer());
|
---|
87 | changed = true;
|
---|
88 | }
|
---|
89 | if (!Operators.OfType<SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer>().Any()) {
|
---|
90 | Operators.Add(new SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer());
|
---|
91 | changed = true;
|
---|
92 | }
|
---|
93 | if (changed) {
|
---|
94 | ParameterizeOperators();
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | private void RegisterEventHandlers() {
|
---|
99 | SymbolicExpressionTreeGrammarParameter.ValueChanged += (o, e) => ConfigureGrammarSymbols();
|
---|
100 | }
|
---|
101 |
|
---|
102 | private void ConfigureGrammarSymbols() {
|
---|
103 | var grammar = SymbolicExpressionTreeGrammar as TypeCoherentExpressionGrammar;
|
---|
104 | if (grammar != null) grammar.ConfigureAsDefaultRegressionGrammar();
|
---|
105 | }
|
---|
106 |
|
---|
107 | private void InitializeOperators() {
|
---|
108 | Operators.Add(new SymbolicRegressionSingleObjectiveTrainingBestSolutionAnalyzer());
|
---|
109 | Operators.Add(new SymbolicRegressionSingleObjectiveValidationBestSolutionAnalyzer());
|
---|
110 | Operators.Add(new SymbolicRegressionSingleObjectiveOverfittingAnalyzer());
|
---|
111 | Operators.Add(new SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer());
|
---|
112 | Operators.Add(new SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer());
|
---|
113 |
|
---|
114 | ParameterizeOperators();
|
---|
115 | }
|
---|
116 |
|
---|
117 | private void UpdateEstimationLimits() {
|
---|
118 | if (ProblemData.TrainingIndices.Any()) {
|
---|
119 | var targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).ToList();
|
---|
120 | var mean = targetValues.Average();
|
---|
121 | var range = targetValues.Max() - targetValues.Min();
|
---|
122 | EstimationLimits.Upper = mean + PunishmentFactor * range;
|
---|
123 | EstimationLimits.Lower = mean - PunishmentFactor * range;
|
---|
124 | } else {
|
---|
125 | EstimationLimits.Upper = double.MaxValue;
|
---|
126 | EstimationLimits.Lower = double.MinValue;
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | protected override void OnProblemDataChanged() {
|
---|
131 | base.OnProblemDataChanged();
|
---|
132 | UpdateEstimationLimits();
|
---|
133 | }
|
---|
134 |
|
---|
135 | protected override void ParameterizeOperators() {
|
---|
136 | base.ParameterizeOperators();
|
---|
137 | if (Parameters.ContainsKey(EstimationLimitsParameterName)) {
|
---|
138 | var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
|
---|
139 | foreach (var op in operators.OfType<ISymbolicDataAnalysisBoundedOperator>()) {
|
---|
140 | op.EstimationLimitsParameter.ActualName = EstimationLimitsParameter.Name;
|
---|
141 | }
|
---|
142 | }
|
---|
143 | }
|
---|
144 | }
|
---|
145 | }
|
---|