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 |
|
---|
22 | using System;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
28 | using System.Collections.Generic;
|
---|
29 | using System.Linq;
|
---|
30 |
|
---|
31 | namespace 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 | }
|
---|
101 |
|
---|
102 | private void RecalculateEstimatedValues() {
|
---|
103 | estimatedValues = GetEstimatedValues(0, problemData.Dataset.Rows).ToList();
|
---|
104 | int nTrainingValues = problemData.TrainingSamplesEnd.Value - problemData.TrainingSamplesStart.Value;
|
---|
105 | estimatedTrainingValues = estimatedValues.Skip(problemData.TrainingSamplesStart.Value).Take(nTrainingValues).ToList();
|
---|
106 | int nTestValues = problemData.TestSamplesEnd.Value - problemData.TestSamplesStart.Value;
|
---|
107 | estimatedTestValues = estimatedValues.Skip(problemData.TestSamplesStart.Value).Take(nTestValues).ToList();
|
---|
108 | }
|
---|
109 |
|
---|
110 | private IEnumerable<double> GetEstimatedValues(int start, int end) {
|
---|
111 | double[] xs = new double[ProblemData.InputVariables.Count];
|
---|
112 | for (int row = 0; row < ProblemData.Dataset.Rows; row++) {
|
---|
113 | for (int i = 0; i < xs.Length; i++) {
|
---|
114 | var variableIndex = ProblemData.Dataset.GetVariableIndex(ProblemData.InputVariables[i].Value);
|
---|
115 | xs[i] = ProblemData.Dataset[row, variableIndex];
|
---|
116 | }
|
---|
117 | yield return model.GetValue(xs);
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
122 | DataAnalysisSolution clone = new DataAnalysisSolution();
|
---|
123 | cloner.RegisterClonedObject(this, clone);
|
---|
124 | clone.model = (IModel)cloner.Clone(model);
|
---|
125 | clone.problemData = problemData;
|
---|
126 | clone.Initialize();
|
---|
127 | return clone;
|
---|
128 | }
|
---|
129 |
|
---|
130 | #region Events
|
---|
131 | public event EventHandler ModelChanged;
|
---|
132 | private void OnModelChanged() {
|
---|
133 | RecalculateEstimatedValues();
|
---|
134 | var changed = ModelChanged;
|
---|
135 | if (changed != null)
|
---|
136 | changed(this, EventArgs.Empty);
|
---|
137 | }
|
---|
138 | public event EventHandler ProblemDataChanged;
|
---|
139 | private void OnProblemDataChanged() {
|
---|
140 | RecalculateEstimatedValues();
|
---|
141 | var changed = ProblemDataChanged;
|
---|
142 | if (changed != null)
|
---|
143 | changed(this, EventArgs.Empty);
|
---|
144 | }
|
---|
145 |
|
---|
146 | private void RegisterProblemDataEvents() {
|
---|
147 | ProblemData.ProblemDataChanged += new EventHandler(ProblemData_Changed);
|
---|
148 | }
|
---|
149 | private void DeregisterProblemDataEvents() {
|
---|
150 | ProblemData.ProblemDataChanged += new EventHandler(ProblemData_Changed);
|
---|
151 | }
|
---|
152 |
|
---|
153 | private void ProblemData_Changed(object sender, EventArgs e) {
|
---|
154 | OnProblemDataChanged();
|
---|
155 | }
|
---|
156 | #endregion
|
---|
157 | }
|
---|
158 | }
|
---|