Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/DataAnalysisSolution.cs @ 3452

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

Included tracking of best of run solution (based on validation set) and calculation of MSE, R² and rel. Error on training and test sets. #938 (Data types and operators for regression problems)

File size: 5.5 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 {
32  /// <summary>
33  /// Represents a solution for a data analysis problem which can be visualized in the GUI.
34  /// </summary>
35  [Item("DataAnalysisSolution", "Represents a solution for a data analysis problem which can be visualized in the GUI.")]
36  [StorableClass]
37  public class DataAnalysisSolution : Item {
38    [Storable]
39    private IModel model;
40    public IModel Model {
41      get { return model; }
42      set {
43        if (model != value) {
44          model = value;
45          OnModelChanged();
46        }
47      }
48    }
49
50    [Storable]
51    private DataAnalysisProblemData problemData;
52    public DataAnalysisProblemData ProblemData {
53      get { return problemData; }
54      set {
55        if (problemData != value) {
56          if (value == null) throw new ArgumentNullException();
57          if (problemData != null) DeregisterProblemDataEvents();
58          problemData = value;
59          RegisterProblemDataEvents();
60          OnProblemDataChanged();
61        }
62      }
63    }
64
65    private List<double> estimatedValues;
66    public IEnumerable<double> EstimatedValues {
67      get {
68        return estimatedValues;
69      }
70    }
71
72    private List<double> estimatedTrainingValues;
73    public IEnumerable<double> EstimatedTrainingValues {
74      get {
75        return estimatedTrainingValues;
76      }
77    }
78
79    private List<double> estimatedTestValues;
80    public IEnumerable<double> EstimatedTestValues {
81      get {
82        return estimatedTestValues;
83      }
84    }
85
86    public DataAnalysisSolution() : base() { }
87    public DataAnalysisSolution(DataAnalysisProblemData problemData, IModel model)
88      : this() {
89      this.problemData = problemData;
90      this.model = model;
91      Initialize();
92    }
93
94    [StorableConstructor]
95    private DataAnalysisSolution(bool deserializing) : base(deserializing) { }
96
97    [StorableHook(HookType.AfterDeserialization)]
98    private void Initialize() {
99      if (problemData != null) RegisterProblemDataEvents();
100      if (problemData != null && model != null) RecalculateEstimatedValues();
101    }
102
103    private void RecalculateEstimatedValues() {
104      estimatedValues = GetEstimatedValues(0, problemData.Dataset.Rows).ToList();
105      int nTrainingValues = problemData.TrainingSamplesEnd.Value - problemData.TrainingSamplesStart.Value;
106      estimatedTrainingValues = estimatedValues.Skip(problemData.TrainingSamplesStart.Value).Take(nTrainingValues).ToList();
107      int nTestValues = problemData.TestSamplesEnd.Value - problemData.TestSamplesStart.Value;
108      estimatedTestValues = estimatedValues.Skip(problemData.TestSamplesStart.Value).Take(nTestValues).ToList();
109    }
110
111    private IEnumerable<double> GetEstimatedValues(int start, int end) {
112      double[] xs = new double[ProblemData.InputVariables.Count];
113      for (int row = 0; row < ProblemData.Dataset.Rows; row++) {
114        for (int i = 0; i < xs.Length; i++) {
115          var variableIndex = ProblemData.Dataset.GetVariableIndex(ProblemData.InputVariables[i].Value);
116          xs[i] = ProblemData.Dataset[row, variableIndex];
117        }
118        yield return model.GetValue(xs);
119      }
120    }
121
122    public override IDeepCloneable Clone(Cloner cloner) {
123      DataAnalysisSolution clone = new DataAnalysisSolution();
124      cloner.RegisterClonedObject(this, clone);
125      clone.model = (IModel)cloner.Clone(model);
126      clone.problemData = problemData;
127      clone.Initialize();
128      return clone;
129    }
130
131    #region Events
132    public event EventHandler ModelChanged;
133    private void OnModelChanged() {
134      RecalculateEstimatedValues();
135      var changed = ModelChanged;
136      if (changed != null)
137        changed(this, EventArgs.Empty);
138    }
139    public event EventHandler ProblemDataChanged;
140    private void OnProblemDataChanged() {
141      RecalculateEstimatedValues();
142      var changed = ProblemDataChanged;
143      if (changed != null)
144        changed(this, EventArgs.Empty);
145    }
146
147    private void RegisterProblemDataEvents() {
148      ProblemData.ProblemDataChanged += new EventHandler(ProblemData_Changed);
149    }
150    private void DeregisterProblemDataEvents() {
151      ProblemData.ProblemDataChanged += new EventHandler(ProblemData_Changed);
152    }
153
154    private void ProblemData_Changed(object sender, EventArgs e) {
155      OnProblemDataChanged();
156    }
157    #endregion
158  }
159}
Note: See TracBrowser for help on using the repository browser.