Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/SymbolicRegressionSolution.cs @ 3683

Last change on this file since 3683 was 3513, checked in by gkronber, 15 years ago

Added upper and lower estimation limits. #938 (Data types and operators for regression problems)

File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using System.Collections.Generic;
29using System.Linq;
30
31namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic {
32  /// <summary>
33  /// Represents a solution for a symbolic regression problem which can be visualized in the GUI.
34  /// </summary>
35  [Item("SymbolicRegressionSolution", "Represents a solution for a symbolic regression problem which can be visualized in the GUI.")]
36  [StorableClass]
37  public sealed class SymbolicRegressionSolution : DataAnalysisSolution {
38    [Storable]
39    private SymbolicRegressionModel model;
40    public SymbolicRegressionModel Model {
41      get { return model; }
42      set {
43        if (model != value) {
44          if (value == null) throw new ArgumentNullException();
45          model = value;
46          OnModelChanged(EventArgs.Empty);
47        }
48      }
49    }
50
51    public SymbolicRegressionSolution() : base() { }
52    public SymbolicRegressionSolution(DataAnalysisProblemData problemData, SymbolicRegressionModel model, double lowerEstimationLimit, double upperEstimationLimit)
53      : base(problemData, lowerEstimationLimit, upperEstimationLimit) {
54      this.model = model;
55    }
56
57    public event EventHandler ModelChanged;
58    private void OnModelChanged(EventArgs e) {
59      RecalculateEstimatedValues();
60      var listeners = ModelChanged;
61      if (listeners != null)
62        listeners(this, e);
63    }
64
65    protected override void OnProblemDataChanged(EventArgs e) {
66      RecalculateEstimatedValues();
67    }
68
69    private void RecalculateEstimatedValues() {
70      estimatedValues = (from x in model.GetEstimatedValues(ProblemData.Dataset, 0, ProblemData.Dataset.Rows)
71                         let boundedX = Math.Min(UpperEstimationLimit, Math.Max(LowerEstimationLimit, x))
72                         select double.IsNaN(boundedX) ? UpperEstimationLimit : boundedX).ToList();
73      OnEstimatedValuesChanged(EventArgs.Empty);
74    }
75
76    private List<double> estimatedValues;
77    public override IEnumerable<double> EstimatedValues {
78      get {
79        if (estimatedValues == null) RecalculateEstimatedValues();
80        return estimatedValues.AsEnumerable();
81      }
82    }
83
84    public override IEnumerable<double> EstimatedTrainingValues {
85      get {
86        if (estimatedValues == null) RecalculateEstimatedValues();
87        int start = ProblemData.TrainingSamplesStart.Value;
88        int n = ProblemData.TrainingSamplesEnd.Value - start;
89        return estimatedValues.Skip(start).Take(n).ToList();
90      }
91    }
92
93    public override IEnumerable<double> EstimatedTestValues {
94      get {
95        if (estimatedValues == null) RecalculateEstimatedValues();
96        int start = ProblemData.TestSamplesStart.Value;
97        int n = ProblemData.TestSamplesEnd.Value - start;
98        return estimatedValues.Skip(start).Take(n).ToList();
99      }
100    }
101
102    public override IDeepCloneable Clone(Cloner cloner) {
103      SymbolicRegressionSolution clone = (SymbolicRegressionSolution)base.Clone(cloner);
104      clone.model = (SymbolicRegressionModel)cloner.Clone(model);
105      return clone;
106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.