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)",
|
---|
34 | "Represents a single objective symbolic regression problem, implemented in Grammatical Evolution.")]
|
---|
35 | [StorableClass]
|
---|
36 | [Creatable("Problems")]
|
---|
37 | public class GESymbolicRegressionSingleObjectiveProblem : GESymbolicDataAnalysisSingleObjectiveProblem<IRegressionProblemData, IGESymbolicRegressionSingleObjectiveEvaluator, IIntegerVectorCreator>,
|
---|
38 | IRegressionProblem {
|
---|
39 | private const double PunishmentFactor = 10;
|
---|
40 | private const int InitialMaximumTreeLength = 30;
|
---|
41 | private const string EstimationLimitsParameterName = "EstimationLimits";
|
---|
42 | private const string EstimationLimitsParameterDescription
|
---|
43 | = "The limits for the estimated value that can be returned by the symbolic regression model.";
|
---|
44 |
|
---|
45 | #region parameter properties
|
---|
46 | public IFixedValueParameter<DoubleLimit> EstimationLimitsParameter {
|
---|
47 | get { return (IFixedValueParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
|
---|
48 | }
|
---|
49 | #endregion
|
---|
50 | #region properties
|
---|
51 | public DoubleLimit EstimationLimits {
|
---|
52 | get { return EstimationLimitsParameter.Value; }
|
---|
53 | }
|
---|
54 | #endregion
|
---|
55 | [StorableConstructor]
|
---|
56 | protected GESymbolicRegressionSingleObjectiveProblem(bool deserializing) : base(deserializing) { }
|
---|
57 | protected GESymbolicRegressionSingleObjectiveProblem(GESymbolicRegressionSingleObjectiveProblem original, Cloner cloner)
|
---|
58 | : base(original, cloner) {
|
---|
59 | RegisterEventHandlers();
|
---|
60 | }
|
---|
61 | public override IDeepCloneable Clone(Cloner cloner) { return new GESymbolicRegressionSingleObjectiveProblem(this, cloner); }
|
---|
62 |
|
---|
63 | public GESymbolicRegressionSingleObjectiveProblem()
|
---|
64 | : base(new RegressionProblemData(), new GESymbolicRegressionSingleObjectiveEvaluator(), new UniformRandomIntegerVectorCreator()) {
|
---|
65 | Parameters.Add(new FixedValueParameter<DoubleLimit>(EstimationLimitsParameterName, EstimationLimitsParameterDescription));
|
---|
66 |
|
---|
67 | EstimationLimitsParameter.Hidden = true;
|
---|
68 |
|
---|
69 |
|
---|
70 | ApplyLinearScalingParameter.Value.Value = true;
|
---|
71 | Maximization.Value = true;
|
---|
72 | MaximumSymbolicExpressionTreeLength.Value = InitialMaximumTreeLength;
|
---|
73 |
|
---|
74 | RegisterEventHandlers();
|
---|
75 | InitializeOperators();
|
---|
76 | UpdateEstimationLimits();
|
---|
77 | }
|
---|
78 |
|
---|
79 | [StorableHook(HookType.AfterDeserialization)]
|
---|
80 | private void AfterDeserialization() {
|
---|
81 | RegisterEventHandlers();
|
---|
82 | }
|
---|
83 |
|
---|
84 | private void RegisterEventHandlers() {
|
---|
85 | // nothing to do
|
---|
86 | }
|
---|
87 |
|
---|
88 |
|
---|
89 | private void InitializeOperators() {
|
---|
90 | Operators.Add(new SymbolicRegressionSingleObjectiveTrainingBestSolutionAnalyzer());
|
---|
91 | Operators.Add(new SymbolicRegressionSingleObjectiveValidationBestSolutionAnalyzer());
|
---|
92 | Operators.Add(new SymbolicRegressionSingleObjectiveOverfittingAnalyzer());
|
---|
93 | Operators.Add(new SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer());
|
---|
94 | Operators.Add(new SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer());
|
---|
95 |
|
---|
96 | ParameterizeOperators();
|
---|
97 | }
|
---|
98 |
|
---|
99 | private void UpdateEstimationLimits() {
|
---|
100 | if (ProblemData.TrainingIndices.Any()) {
|
---|
101 | var targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).ToList();
|
---|
102 | var mean = targetValues.Average();
|
---|
103 | var range = targetValues.Max() - targetValues.Min();
|
---|
104 | EstimationLimits.Upper = mean + PunishmentFactor * range;
|
---|
105 | EstimationLimits.Lower = mean - PunishmentFactor * range;
|
---|
106 | } else {
|
---|
107 | EstimationLimits.Upper = double.MaxValue;
|
---|
108 | EstimationLimits.Lower = double.MinValue;
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | protected override void OnProblemDataChanged() {
|
---|
113 | base.OnProblemDataChanged();
|
---|
114 | UpdateEstimationLimits();
|
---|
115 | }
|
---|
116 |
|
---|
117 | protected override void ParameterizeOperators() {
|
---|
118 | base.ParameterizeOperators();
|
---|
119 | if (Parameters.ContainsKey(EstimationLimitsParameterName)) {
|
---|
120 | var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
|
---|
121 | foreach (var op in operators.OfType<ISymbolicDataAnalysisBoundedOperator>()) {
|
---|
122 | op.EstimationLimitsParameter.ActualName = EstimationLimitsParameter.Name;
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|
127 | }
|
---|