source: stable/HeuristicLab.Problems.GeneticProgramming/3.3/BasicSymbolicRegression/Problem.cs @ 17149

Last change on this file since 17149 was 17149, checked in by abeham, 8 weeks ago

#3005: merged to stable (16872, 16873, 16875, 16890)

File size: 8.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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 HEAL.Attic;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Parameters;
30using HeuristicLab.Problems.DataAnalysis;
31using HeuristicLab.Problems.Instances;
32
33
34namespace HeuristicLab.Problems.GeneticProgramming.BasicSymbolicRegression {
35  [Item("Koza-style Symbolic Regression", "An implementation of symbolic regression without bells-and-whistles. Use \"Symbolic Regression Problem (single-objective)\" if you want to use all features.")]
36  [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 900)]
37  [StorableType("72011B73-28C6-4D5E-BEDF-27425BC87B9C")]
38  public sealed class Problem : SymbolicExpressionTreeProblem, IRegressionProblem, IProblemInstanceConsumer<IRegressionProblemData>, IProblemInstanceExporter<IRegressionProblemData> {
39
40    #region parameter names
41    private const string ProblemDataParameterName = "ProblemData";
42    #endregion
43
44    #region Parameter Properties
45    IParameter IDataAnalysisProblem.ProblemDataParameter { get { return ProblemDataParameter; } }
46
47    public IValueParameter<IRegressionProblemData> ProblemDataParameter {
48      get { return (IValueParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName]; }
49    }
50    #endregion
51
52    #region Properties
53    public IRegressionProblemData ProblemData {
54      get { return ProblemDataParameter.Value; }
55      set { ProblemDataParameter.Value = value; }
56    }
57    IDataAnalysisProblemData IDataAnalysisProblem.ProblemData { get { return ProblemData; } }
58    #endregion
59
60    public event EventHandler ProblemDataChanged;
61
62    public override bool Maximization {
63      get { return true; }
64    }
65
66    #region item cloning and persistence
67    // persistence
68    [StorableConstructor]
69    private Problem(StorableConstructorFlag _) : base(_) { }
70    [StorableHook(HookType.AfterDeserialization)]
71    private void AfterDeserialization() {
72      RegisterEventHandlers();
73    }
74
75    // cloning
76    private Problem(Problem original, Cloner cloner)
77      : base(original, cloner) {
78      RegisterEventHandlers();
79    }
80    public override IDeepCloneable Clone(Cloner cloner) { return new Problem(this, cloner); }
81    #endregion
82
83    public Problem()
84      : base() {
85      Parameters.Add(new ValueParameter<IRegressionProblemData>(ProblemDataParameterName, "The data for the regression problem", new RegressionProblemData()));
86
87      var g = new SimpleSymbolicExpressionGrammar(); // empty grammar is replaced in UpdateGrammar()
88      base.Encoding = new SymbolicExpressionTreeEncoding(g, 100, 17);
89      Encoding.GrammarParameter.ReadOnly = true;
90
91      UpdateGrammar();
92      RegisterEventHandlers();
93    }
94
95
96    public override double Evaluate(ISymbolicExpressionTree tree, IRandom random) {
97      // Doesn't use classes from HeuristicLab.Problems.DataAnalysis.Symbolic to make sure that the implementation can be fully understood easily.
98      // HeuristicLab.Problems.DataAnalysis.Symbolic would already provide all the necessary functionality (esp. interpreter) but at a much higher complexity.
99      // Another argument is that we don't need a reference to HeuristicLab.Problems.DataAnalysis.Symbolic
100
101      var problemData = ProblemData;
102      var rows = ProblemData.TrainingIndices.ToArray();
103      var target = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
104      var predicted = Interpret(tree, problemData.Dataset, rows);
105
106      OnlineCalculatorError errorState;
107      var r = OnlinePearsonsRCalculator.Calculate(target, predicted, out errorState);
108      if (errorState != OnlineCalculatorError.None) r = 0;
109      return r * r;
110    }
111
112    private IEnumerable<double> Interpret(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows) {
113      // skip programRoot and startSymbol
114      return InterpretRec(tree.Root.GetSubtree(0).GetSubtree(0), dataset, rows);
115    }
116
117    private IEnumerable<double> InterpretRec(ISymbolicExpressionTreeNode node, IDataset dataset, IEnumerable<int> rows) {
118      Func<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode, Func<double, double, double>, IEnumerable<double>> binaryEval =
119        (left, right, f) => InterpretRec(left, dataset, rows).Zip(InterpretRec(right, dataset, rows), f);
120
121      switch (node.Symbol.Name) {
122        case "+": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x + y);
123        case "*": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x * y);
124        case "-": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x - y);
125        case "%": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => y.IsAlmost(0.0) ? 0.0 : x / y); // protected division
126        default: {
127            double erc;
128            if (double.TryParse(node.Symbol.Name, out erc)) {
129              return rows.Select(_ => erc);
130            } else {
131              // assume that this is a variable name
132              return dataset.GetDoubleValues(node.Symbol.Name, rows);
133            }
134          }
135      }
136    }
137
138
139    #region events
140    private void RegisterEventHandlers() {
141      ProblemDataParameter.ValueChanged += new EventHandler(ProblemDataParameter_ValueChanged);
142      if (ProblemDataParameter.Value != null) ProblemDataParameter.Value.Changed += new EventHandler(ProblemData_Changed);
143    }
144
145    private void ProblemDataParameter_ValueChanged(object sender, EventArgs e) {
146      ProblemDataParameter.Value.Changed += new EventHandler(ProblemData_Changed);
147      OnProblemDataChanged();
148      OnReset();
149    }
150
151    private void ProblemData_Changed(object sender, EventArgs e) {
152      OnReset();
153    }
154
155    private void OnProblemDataChanged() {
156      UpdateGrammar();
157
158      var handler = ProblemDataChanged;
159      if (handler != null) handler(this, EventArgs.Empty);
160    }
161
162    private void UpdateGrammar() {
163      // whenever ProblemData is changed we create a new grammar with the necessary symbols
164      var g = new SimpleSymbolicExpressionGrammar();
165      g.AddSymbols(new[] { "+", "*", "%", "-" }, 2, 2); // % is protected division 1/0 := 0
166
167      foreach (var variableName in ProblemData.AllowedInputVariables)
168        g.AddTerminalSymbol(variableName);
169
170      // generate ephemeral random consts in the range [-10..+10[ (2*number of variables)
171      var rand = new System.Random();
172      for (int i = 0; i < ProblemData.AllowedInputVariables.Count() * 2; i++) {
173        string newErcSy;
174        do {
175          newErcSy = string.Format("{0:F2}", rand.NextDouble() * 20 - 10);
176        } while (g.Symbols.Any(sy => sy.Name == newErcSy)); // it might happen that we generate the same constant twice
177        g.AddTerminalSymbol(newErcSy);
178      }
179
180      Encoding.GrammarParameter.ReadOnly = false;
181      Encoding.Grammar = g;
182      Encoding.GrammarParameter.ReadOnly = true;
183    }
184    #endregion
185
186    #region Import & Export
187    public void Load(IRegressionProblemData data) {
188      Name = data.Name;
189      Description = data.Description;
190      ProblemData = data;
191    }
192
193    public IRegressionProblemData Export() {
194      return ProblemData;
195    }
196    #endregion
197  }
198}
Note: See TracBrowser for help on using the repository browser.