Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/New Implementation/SymbolicRegressionProblem.cs @ 17748

Last change on this file since 17748 was 17748, checked in by abeham, 3 years ago

#2521: fixed code so that unit test runs

File size: 7.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Threading;
24using HEAL.Attic;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31
32namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
33  [Item("Symbolic Regression Problem", "Represents a symbolic regression problem (single-objective).")]
34  [StorableType("01A2E13B-30F4-42DA-A57D-0D5B01A3FDF8")]
35  [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 100)]
36  public class SymbolicRegressionProblem : SymbolicExpressionTreeProblem, IRegressionProblem {
37    #region Parameter properties
38    //TODO remove private setter to transform to readonly auto properties
39    [Storable] public IValueParameter<IRegressionProblemData> ProblemDataParameter { get; private set; }
40    [Storable] public IValueParameter<ISymbolicDataAnalysisGrammar> GrammarParameter { get; private set; }
41    [Storable] public IValueParameter<IntValue> MaximumTreeLengthParameter { get; private set; }
42    [Storable] public IValueParameter<IntValue> MaximumTreeDepthParameter { get; private set; }
43    #endregion
44
45    #region Properties
46    IDataAnalysisProblemData IDataAnalysisProblem.ProblemData {
47      get { return ProblemData; }
48    }
49    public IRegressionProblemData ProblemData {
50      get { return ProblemDataParameter.Value; }
51      set { ProblemDataParameter.Value = value; }
52    }
53    public ISymbolicDataAnalysisGrammar Grammar {
54      get { return GrammarParameter.Value; }
55      set { GrammarParameter.Value = value; }
56    }
57    public int MaximumTreeLength {
58      get { return MaximumTreeLengthParameter.Value.Value; }
59      set { MaximumTreeLengthParameter.Value.Value = value; }
60    }
61
62    public int MaximumTreeDepth {
63      get { return MaximumTreeDepthParameter.Value.Value; }
64      set { MaximumTreeDepthParameter.Value.Value = value; }
65    }
66    #endregion
67
68
69    #region Serialization
70    [StorableConstructor]
71    protected SymbolicRegressionProblem(StorableConstructorFlag _) : base(_) { }
72    [StorableHook(HookType.AfterDeserialization)]
73    private void AfterDeserialization() {
74      RegisterEventHandlers();
75    }
76    #endregion
77
78    #region Cloning
79    protected SymbolicRegressionProblem(SymbolicRegressionProblem original, Cloner cloner) : base(original, cloner) {
80      ProblemDataParameter = cloner.Clone(original.ProblemDataParameter);
81      GrammarParameter = cloner.Clone(original.GrammarParameter);
82      MaximumTreeLengthParameter = cloner.Clone(original.MaximumTreeLengthParameter);
83      MaximumTreeDepthParameter = cloner.Clone(original.MaximumTreeDepthParameter);
84      RegisterEventHandlers();
85
86    }
87    public override IDeepCloneable Clone(Cloner cloner) {
88      return new SymbolicRegressionProblem(this, cloner);
89    }
90    #endregion
91
92    public SymbolicRegressionProblem() : base() {
93      ProblemDataParameter = new ValueParameter<IRegressionProblemData>("ProblemData", "The data set, target variable and input variables of the regression problem.", new RegressionProblemData());
94      GrammarParameter = new ReferenceParameter<ISymbolicDataAnalysisGrammar, ISymbolicExpressionGrammar>("Grammar", "The grammar that should be used for symbolic expression tree.", Encoding.GrammarParameter);
95      MaximumTreeLengthParameter = new ReferenceParameter<IntValue>("Maximum Tree Length", "Maximal length of the symbolic expression.", Encoding.TreeLengthParameter);
96      MaximumTreeDepthParameter = new ReferenceParameter<IntValue>("Maximum Tree Depth", "Maximal depth of the symbolic expression.", Encoding.TreeDepthParameter);
97
98      Encoding.GrammarParameter.ReadOnly = false;
99      var grammar = new TypeCoherentExpressionGrammar();
100      grammar.ConfigureAsDefaultRegressionGrammar();
101      Grammar = grammar;
102
103      //Parameters.Add(GrammarParameter);
104      Parameters.Add(ProblemDataParameter);
105      Parameters.Add(MaximumTreeLengthParameter);
106      Parameters.Add(MaximumTreeDepthParameter);
107
108      RegisterEventHandlers();
109    }
110
111
112    public override ISingleObjectiveEvaluationResult Evaluate(ISymbolicExpressionTree solution, IRandom random, CancellationToken cancellationToken) {
113      var quality = 0.0;
114
115      //IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows);
116      //IEnumerable<double> targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, rows);
117
118      //if (LinearScaling) estimatedValues = LinearScaling.ScaleEstimatedValues(estimatedValues, targetValues, lowerEstimationLimit, upperEstimationLimit, ProblemData.Dataset.Rows);
119
120      //quality = OnlineMeanSquaredErrorCalculator.Calculate(targetValues, estimatedValues, out OnlineCalculatorError errorState);
121      //if (errorState != OnlineCalculatorError.None) quality = double.NaN;
122
123      var result = new SingleObjectiveEvaluationResult(quality);
124      return result;
125    }
126
127    public override void Analyze(ISingleObjectiveSolutionContext<ISymbolicExpressionTree>[] solutionContexts, IRandom random) {
128
129    }
130
131
132    private void RegisterEventHandlers() {
133      ProblemDataParameter.ValueChanged += new EventHandler(ProblemDataParameter_ValueChanged);
134      if (ProblemDataParameter.Value != null) ProblemDataParameter.Value.Changed += new EventHandler(ProblemData_Changed);
135    }
136
137    private void ProblemDataParameter_ValueChanged(object sender, EventArgs e) {
138      ProblemDataParameter.Value.Changed += new EventHandler(ProblemData_Changed);
139      OnProblemDataChanged();
140      OnReset();
141    }
142
143    private void ProblemData_Changed(object sender, EventArgs e) {
144      OnReset();
145    }
146
147    public event EventHandler ProblemDataChanged;
148    protected virtual void OnProblemDataChanged() {
149      ProblemDataChanged?.Invoke(this, EventArgs.Empty);
150    }
151
152    #region Import & Export
153    public void Load(IRegressionProblemData data) {
154      Name = data.Name;
155      Description = data.Description;
156      ProblemData = data;
157    }
158
159    public IRegressionProblemData Export() {
160      return ProblemData;
161    }
162    #endregion
163  }
164}
Note: See TracBrowser for help on using the repository browser.