Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1784:

  • merged Problems.DataAnalysis r7273:7748 from trunk
  • prepared SymbolicClassificationSingleObjectiveProblem and SymbolicRegressionSingleObjectiveProblem to load and export problem instances
File size: 7.5 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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Problems.Instances;
30
31namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
32  [Item("Symbolic Regression Problem (single objective)", "Represents a single objective symbolic regression problem.")]
33  [StorableClass]
34  [Creatable("Problems")]
35  public class SymbolicRegressionSingleObjectiveProblem : SymbolicDataAnalysisSingleObjectiveProblem<IRegressionProblemData, ISymbolicRegressionSingleObjectiveEvaluator, ISymbolicDataAnalysisSolutionCreator>, IRegressionProblem,
36    IProblemInstanceConsumer<RegressionData>, IProblemInstanceExporter<RegressionData> {
37    private const double PunishmentFactor = 10;
38    private const int InitialMaximumTreeDepth = 8;
39    private const int InitialMaximumTreeLength = 25;
40    private const string EstimationLimitsParameterName = "EstimationLimits";
41    private const string EstimationLimitsParameterDescription = "The limits for the estimated value that can be returned by the symbolic regression model.";
42
43    #region parameter properties
44    public IFixedValueParameter<DoubleLimit> EstimationLimitsParameter {
45      get { return (IFixedValueParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
46    }
47    #endregion
48    #region properties
49    public DoubleLimit EstimationLimits {
50      get { return EstimationLimitsParameter.Value; }
51    }
52    #endregion
53    [StorableConstructor]
54    protected SymbolicRegressionSingleObjectiveProblem(bool deserializing) : base(deserializing) { }
55    protected SymbolicRegressionSingleObjectiveProblem(SymbolicRegressionSingleObjectiveProblem original, Cloner cloner) : base(original, cloner) { }
56    public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicRegressionSingleObjectiveProblem(this, cloner); }
57
58    public SymbolicRegressionSingleObjectiveProblem()
59      : base(new RegressionProblemData(), new SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator(), new SymbolicDataAnalysisExpressionTreeCreator()) {
60      Parameters.Add(new FixedValueParameter<DoubleLimit>(EstimationLimitsParameterName, EstimationLimitsParameterDescription));
61
62      EstimationLimitsParameter.Hidden = true;
63
64      Maximization.Value = true;
65      MaximumSymbolicExpressionTreeDepth.Value = InitialMaximumTreeDepth;
66      MaximumSymbolicExpressionTreeLength.Value = InitialMaximumTreeLength;
67
68      SymbolicExpressionTreeGrammarParameter.ValueChanged += (o, e) => ConfigureGrammarSymbols();
69
70      ConfigureGrammarSymbols();
71      InitializeOperators();
72      UpdateEstimationLimits();
73    }
74
75    private void ConfigureGrammarSymbols() {
76      var grammar = SymbolicExpressionTreeGrammar as TypeCoherentExpressionGrammar;
77      if (grammar != null) grammar.ConfigureAsDefaultRegressionGrammar();
78    }
79
80    private void InitializeOperators() {
81      Operators.Add(new SymbolicRegressionSingleObjectiveTrainingBestSolutionAnalyzer());
82      Operators.Add(new SymbolicRegressionSingleObjectiveValidationBestSolutionAnalyzer());
83      Operators.Add(new SymbolicRegressionSingleObjectiveOverfittingAnalyzer());
84      Operators.Add(new SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer());
85      Operators.Add(new SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer());
86
87      ParameterizeOperators();
88    }
89
90    private void UpdateEstimationLimits() {
91      if (ProblemData.TrainingIndizes.Any()) {
92        var targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes).ToList();
93        var mean = targetValues.Average();
94        var range = targetValues.Max() - targetValues.Min();
95        EstimationLimits.Upper = mean + PunishmentFactor * range;
96        EstimationLimits.Lower = mean - PunishmentFactor * range;
97      } else {
98        EstimationLimits.Upper = double.MaxValue;
99        EstimationLimits.Lower = double.MinValue;
100      }
101    }
102
103    protected override void OnProblemDataChanged() {
104      base.OnProblemDataChanged();
105      UpdateEstimationLimits();
106    }
107
108    protected override void ParameterizeOperators() {
109      base.ParameterizeOperators();
110      if (Parameters.ContainsKey(EstimationLimitsParameterName)) {
111        var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
112        foreach (var op in operators.OfType<ISymbolicDataAnalysisBoundedOperator>()) {
113          op.EstimationLimitsParameter.ActualName = EstimationLimitsParameter.Name;
114        }
115      }
116    }
117
118    public override void ImportProblemDataFromFile(string fileName) {
119      RegressionProblemData problemData = RegressionProblemData.ImportFromFile(fileName);
120      ProblemData = problemData;
121    }
122
123    public void Load(RegressionData data) {
124      Name = data.Name;
125      Description = data.Description;
126      Dataset dataset = new Dataset(data.InputVariables, data.Values);
127      ProblemData = new RegressionProblemData(dataset, data.AllowedInputVariables, data.TargetVariable);
128      ProblemData.TrainingPartition.Start = data.TrainingPartitionStart;
129      ProblemData.TrainingPartition.End = data.TrainingPartitionEnd;
130      ProblemData.TestPartition.Start = data.TestPartitionStart;
131      ProblemData.TestPartition.End = data.TestPartitionEnd;
132      OnReset();
133    }
134
135    public RegressionData Export() {
136      if (!ProblemData.InputVariables.Count.Equals(ProblemData.Dataset.DoubleVariables.Count()))
137        throw new ArgumentException("Not all input variables are double variables! (Export only works with double variables)");
138
139      RegressionData regData = new RegressionData();
140      regData.Name = Name;
141      regData.Description = Description;
142      regData.TargetVariable = ProblemData.TargetVariable;
143      regData.InputVariables = ProblemData.InputVariables.Select(x => x.Value).ToArray();
144      regData.AllowedInputVariables = ProblemData.AllowedInputVariables.ToArray();
145      regData.TrainingPartitionStart = ProblemData.TrainingPartition.Start;
146      regData.TrainingPartitionEnd = ProblemData.TrainingPartition.End;
147      regData.TestPartitionStart = ProblemData.TestPartition.Start;
148      regData.TestPartitionEnd = ProblemData.TestPartition.End;
149
150      List<List<double>> data = new List<List<double>>();
151      foreach (var variable in ProblemData.Dataset.DoubleVariables) {
152        data.Add(ProblemData.Dataset.GetDoubleValues(variable).ToList());
153      }
154      regData.Values = Transformer.Transformation(data);
155
156      return regData;
157    }
158  }
159}
Note: See TracBrowser for help on using the repository browser.