Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3916 was 3916, checked in by mkommend, 14 years ago

added ResultsView for DataAnalysisSolutions and implemented IStringConvertibleMatrix in DataAnalysisSolution (ticket #1020)

File size: 8.4 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 HeuristicLab.Problems.DataAnalysis.Evaluators;
31
32namespace HeuristicLab.Problems.DataAnalysis {
33  /// <summary>
34  /// Represents a solution for a data analysis problem which can be visualized in the GUI.
35  /// </summary>
36  [Item("DataAnalysisSolution", "Represents a solution for a data analysis problem which can be visualized in the GUI.")]
37  [StorableClass]
38  public abstract class DataAnalysisSolution : NamedItem, IStringConvertibleMatrix {
39    protected DataAnalysisSolution()
40      : base() { }
41    protected DataAnalysisSolution(DataAnalysisProblemData problemData) : this(problemData, double.NegativeInfinity, double.PositiveInfinity) { }
42    protected DataAnalysisSolution(DataAnalysisProblemData problemData, double lowerEstimationLimit, double upperEstimationLimit)
43      : this() {
44      this.problemData = problemData;
45      this.lowerEstimationLimit = lowerEstimationLimit;
46      this.upperEstimationLimit = upperEstimationLimit;
47      Initialize();
48    }
49
50    [StorableConstructor]
51    private DataAnalysisSolution(bool deserializing) : base(deserializing) { }
52    [StorableHook(HookType.AfterDeserialization)]
53    private void Initialize() {
54      if (problemData != null) RegisterProblemDataEvents();
55    }
56
57    [Storable]
58    private DataAnalysisProblemData problemData;
59    public DataAnalysisProblemData ProblemData {
60      get { return problemData; }
61      set {
62        if (problemData != value) {
63          if (value == null) throw new ArgumentNullException();
64          if (model != null && problemData != null && !problemData.InputVariables.Select(c => c.Value).SequenceEqual(
65            value.InputVariables.Select(c => c.Value)))
66            throw new ArgumentException("Could not set new problem data with different structure");
67
68          if (problemData != null) DeregisterProblemDataEvents();
69          problemData = value;
70          RegisterProblemDataEvents();
71          OnProblemDataChanged();
72          RecalculateEstimatedValues();
73        }
74      }
75    }
76
77    [Storable]
78    private IDataAnalysisModel model;
79    public IDataAnalysisModel Model {
80      get { return model; }
81      set {
82        if (model != value) {
83          if (value == null) throw new ArgumentNullException();
84          model = value;
85          OnModelChanged();
86          RecalculateEstimatedValues();
87        }
88      }
89    }
90
91    [Storable]
92    private double lowerEstimationLimit;
93    public double LowerEstimationLimit {
94      get { return lowerEstimationLimit; }
95      set {
96        if (lowerEstimationLimit != value) {
97          lowerEstimationLimit = value;
98          RecalculateEstimatedValues();
99        }
100      }
101    }
102
103    [Storable]
104    private double upperEstimationLimit;
105    public double UpperEstimationLimit {
106      get { return upperEstimationLimit; }
107      set {
108        if (upperEstimationLimit != value) {
109          upperEstimationLimit = value;
110          RecalculateEstimatedValues();
111        }
112      }
113    }
114
115    public abstract IEnumerable<double> EstimatedValues { get; }
116    public abstract IEnumerable<double> EstimatedTrainingValues { get; }
117    public abstract IEnumerable<double> EstimatedTestValues { get; }
118    protected abstract void RecalculateEstimatedValues();
119
120    #region Events
121    protected virtual void RegisterProblemDataEvents() {
122      ProblemData.ProblemDataChanged += new EventHandler(ProblemData_Changed);
123    }
124    protected virtual void DeregisterProblemDataEvents() {
125      ProblemData.ProblemDataChanged += new EventHandler(ProblemData_Changed);
126    }
127    private void ProblemData_Changed(object sender, EventArgs e) {
128      OnProblemDataChanged();
129    }
130
131    public event EventHandler ProblemDataChanged;
132    protected virtual void OnProblemDataChanged() {
133      var listeners = ProblemDataChanged;
134      if (listeners != null)
135        listeners(this, EventArgs.Empty);
136    }
137
138    public event EventHandler ModelChanged;
139    protected virtual void OnModelChanged() {
140      EventHandler handler = ModelChanged;
141      if (handler != null)
142        handler(this, EventArgs.Empty);
143    }
144
145    public event EventHandler EstimatedValuesChanged;
146    protected virtual void OnEstimatedValuesChanged() {
147      RecalculateResultValues();
148      var listeners = EstimatedValuesChanged;
149      if (listeners != null)
150        listeners(this, EventArgs.Empty);
151    }
152    #endregion
153
154    public override IDeepCloneable Clone(Cloner cloner) {
155      DataAnalysisSolution clone = (DataAnalysisSolution)base.Clone(cloner);
156      // don't clone the problem data!
157      clone.problemData = problemData;
158      clone.Model = (IDataAnalysisModel)cloner.Clone(model);
159      clone.lowerEstimationLimit = lowerEstimationLimit;
160      clone.upperEstimationLimit = upperEstimationLimit;
161      clone.Initialize();
162      return clone;
163    }
164
165    #region IStringConvertibleMatrix implementation
166    private List<string> rowNames = new List<string>() { "MeanSquaredError", "CoefficientOfDetermination" };
167    private List<string> columnNames = new List<string>() { "Training", "Test" };
168    private double[,] resultValues = new double[2, 2];
169    int IStringConvertibleMatrix.Rows { get { return rowNames.Count; } set { } }
170    int IStringConvertibleMatrix.Columns { get { return columnNames.Count; } set { } }
171    IEnumerable<string> IStringConvertibleMatrix.ColumnNames { get { return columnNames; } set { } }
172    IEnumerable<string> IStringConvertibleMatrix.RowNames { get { return rowNames; } set { } }
173    bool IStringConvertibleMatrix.SortableView { get { return false; } set { } }
174    bool IStringConvertibleMatrix.ReadOnly { get { return true; } }
175
176    string IStringConvertibleMatrix.GetValue(int rowIndex, int columnIndex) {
177      return resultValues[rowIndex, columnIndex].ToString();
178    }
179    bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
180      errorMessage = "This matrix is readonly.";
181      return false;
182    }
183    bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) { return false; }
184
185    protected void RecalculateResultValues() {
186      IEnumerable<double> originalTrainingValues = problemData.Dataset.GetVariableValues(problemData.TargetVariable.Value, problemData.TrainingSamplesStart.Value, problemData.TrainingSamplesEnd.Value);
187      IEnumerable<double> originalTestValues = problemData.Dataset.GetVariableValues(problemData.TargetVariable.Value, problemData.TestSamplesStart.Value, problemData.TestSamplesEnd.Value);
188      resultValues[0, 0] = SimpleMSEEvaluator.Calculate(originalTrainingValues, EstimatedTrainingValues);
189      resultValues[0, 1] = SimpleMSEEvaluator.Calculate(originalTestValues, EstimatedTestValues);
190      resultValues[1, 0] = SimpleRSquaredEvaluator.Calculate(originalTrainingValues, EstimatedTrainingValues);
191      resultValues[1, 1] = SimpleRSquaredEvaluator.Calculate(originalTestValues, EstimatedTestValues);
192      this.OnReset();
193    }
194
195    public event EventHandler ColumnNamesChanged;
196    public event EventHandler RowNamesChanged;
197    public event EventHandler SortableViewChanged;
198    public event EventHandler<EventArgs<int, int>> ItemChanged;
199    public event EventHandler Reset;
200    protected virtual void OnReset() {
201      EventHandler handler = Reset;
202      if (handler != null)
203        handler(this, EventArgs.Empty);
204    }
205    #endregion
206  }
207}
Note: See TracBrowser for help on using the repository browser.