Free cookie consent management tool by TermsFeed Policy Generator

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

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

Bug fixes in cloning and persistence code. #938

File size: 3.8 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)
53      : base(problemData) {
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 = model.GetEstimatedValues(ProblemData.Dataset, 0, ProblemData.Dataset.Rows).ToList();
71      OnEstimatedValuesChanged(EventArgs.Empty);
72    }
73
74    private List<double> estimatedValues;
75    public override IEnumerable<double> EstimatedValues {
76      get {
77        if (estimatedValues == null) RecalculateEstimatedValues();
78        return estimatedValues.AsEnumerable();
79      }
80    }
81
82    public override IEnumerable<double> EstimatedTrainingValues {
83      get {
84        if (estimatedValues == null) RecalculateEstimatedValues();
85        int start = ProblemData.TrainingSamplesStart.Value;
86        int n = ProblemData.TrainingSamplesEnd.Value - start;
87        return estimatedValues.Skip(start).Take(n).ToList();
88      }
89    }
90
91    public override IEnumerable<double> EstimatedTestValues {
92      get {
93        if (estimatedValues == null) RecalculateEstimatedValues();
94        int start = ProblemData.TestSamplesStart.Value;
95        int n = ProblemData.TestSamplesEnd.Value - start;
96        return estimatedValues.Skip(start).Take(n).ToList();
97      }
98    }
99
100    public override IDeepCloneable Clone(Cloner cloner) {
101      SymbolicRegressionSolution clone = (SymbolicRegressionSolution)base.Clone(cloner);
102      clone.model = (SymbolicRegressionModel)model.Clone(cloner);
103      return clone;
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.