Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3710 was 3710, checked in by gkronber, 14 years ago

Implemented reviewer comments. #893 (HeuristicLab 3.3.0 application review)

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