Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProblemInstancesRegressionAndClassification/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSingleObjectiveProblem.cs @ 7770

Last change on this file since 7770 was 7770, checked in by sforsten, 12 years ago

#1784:

  • added some regions for readability
  • added import and export methods in DataAnalysisProblem and SymbolicDataAnalysisProblem to reduce code duplication
  • added a recursive and an iterative approach without many linq expression to generate all combinations of list elements in ValueGenerator
File size: 6.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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
22using System.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Parameters;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Problems.Instances;
28
29namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
30  [Item("Symbolic Regression Problem (single objective)", "Represents a single objective symbolic regression problem.")]
31  [StorableClass]
32  [Creatable("Problems")]
33  public class SymbolicRegressionSingleObjectiveProblem : SymbolicDataAnalysisSingleObjectiveProblem<IRegressionProblemData, ISymbolicRegressionSingleObjectiveEvaluator, ISymbolicDataAnalysisSolutionCreator>, IRegressionProblem,
34    IProblemInstanceConsumer<RegressionProblemData>, IProblemInstanceExporter<RegressionProblemData> {
35    private const double PunishmentFactor = 10;
36    private const int InitialMaximumTreeDepth = 8;
37    private const int InitialMaximumTreeLength = 25;
38    private const string EstimationLimitsParameterName = "EstimationLimits";
39    private const string EstimationLimitsParameterDescription = "The limits for the estimated value that can be returned by the symbolic regression model.";
40
41    #region parameter properties
42    public IFixedValueParameter<DoubleLimit> EstimationLimitsParameter {
43      get { return (IFixedValueParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
44    }
45    #endregion
46    #region properties
47    public DoubleLimit EstimationLimits {
48      get { return EstimationLimitsParameter.Value; }
49    }
50    #endregion
51    [StorableConstructor]
52    protected SymbolicRegressionSingleObjectiveProblem(bool deserializing) : base(deserializing) { }
53    protected SymbolicRegressionSingleObjectiveProblem(SymbolicRegressionSingleObjectiveProblem original, Cloner cloner) : base(original, cloner) { }
54    public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicRegressionSingleObjectiveProblem(this, cloner); }
55
56    public SymbolicRegressionSingleObjectiveProblem()
57      : base(new RegressionProblemData(), new SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator(), new SymbolicDataAnalysisExpressionTreeCreator()) {
58      Parameters.Add(new FixedValueParameter<DoubleLimit>(EstimationLimitsParameterName, EstimationLimitsParameterDescription));
59
60      EstimationLimitsParameter.Hidden = true;
61
62      Maximization.Value = true;
63      MaximumSymbolicExpressionTreeDepth.Value = InitialMaximumTreeDepth;
64      MaximumSymbolicExpressionTreeLength.Value = InitialMaximumTreeLength;
65
66      SymbolicExpressionTreeGrammarParameter.ValueChanged += (o, e) => ConfigureGrammarSymbols();
67
68      ConfigureGrammarSymbols();
69      InitializeOperators();
70      UpdateEstimationLimits();
71    }
72
73    private void ConfigureGrammarSymbols() {
74      var grammar = SymbolicExpressionTreeGrammar as TypeCoherentExpressionGrammar;
75      if (grammar != null) grammar.ConfigureAsDefaultRegressionGrammar();
76    }
77
78    private void InitializeOperators() {
79      Operators.Add(new SymbolicRegressionSingleObjectiveTrainingBestSolutionAnalyzer());
80      Operators.Add(new SymbolicRegressionSingleObjectiveValidationBestSolutionAnalyzer());
81      Operators.Add(new SymbolicRegressionSingleObjectiveOverfittingAnalyzer());
82      Operators.Add(new SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer());
83      Operators.Add(new SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer());
84
85      ParameterizeOperators();
86    }
87
88    private void UpdateEstimationLimits() {
89      if (ProblemData.TrainingIndizes.Any()) {
90        var targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes).ToList();
91        var mean = targetValues.Average();
92        var range = targetValues.Max() - targetValues.Min();
93        EstimationLimits.Upper = mean + PunishmentFactor * range;
94        EstimationLimits.Lower = mean - PunishmentFactor * range;
95      } else {
96        EstimationLimits.Upper = double.MaxValue;
97        EstimationLimits.Lower = double.MinValue;
98      }
99    }
100
101    protected override void OnProblemDataChanged() {
102      base.OnProblemDataChanged();
103      UpdateEstimationLimits();
104    }
105
106    protected override void ParameterizeOperators() {
107      base.ParameterizeOperators();
108      if (Parameters.ContainsKey(EstimationLimitsParameterName)) {
109        var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
110        foreach (var op in operators.OfType<ISymbolicDataAnalysisBoundedOperator>()) {
111          op.EstimationLimitsParameter.ActualName = EstimationLimitsParameter.Name;
112        }
113      }
114    }
115
116    #region Import & Export
117    public void Load(RegressionProblemData data) {
118      base.Load(data);
119    }
120
121    public RegressionProblemData Export() {
122      if (ProblemData is RegressionProblemData)
123        return (RegressionProblemData)ProblemData;
124      else {
125        RegressionProblemData regData = new RegressionProblemData(ProblemData.Dataset, ProblemData.AllowedInputVariables, ProblemData.TargetVariable);
126        return (RegressionProblemData)base.Export(regData);
127      }
128    }
129    #endregion
130  }
131}
Note: See TracBrowser for help on using the repository browser.