Free cookie consent management tool by TermsFeed Policy Generator

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

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

Made data analysis problem data and data analysis solutions savable. ##1193

File size: 5.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 System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Problems.DataAnalysis {
30  /// <summary>
31  /// Represents a solution for a data analysis problem which can be visualized in the GUI.
32  /// </summary>
33  [Item("DataAnalysisSolution", "Represents a solution for a data analysis problem which can be visualized in the GUI.")]
34  [StorableClass]
35  public abstract class DataAnalysisSolution : NamedItem, IStorableContent {
36    #region IStorableContent Members
37    public string Filename { get; set; }
38    #endregion
39   
40    protected DataAnalysisSolution()
41      : base() { }
42    protected DataAnalysisSolution(DataAnalysisProblemData problemData) : this(problemData, double.NegativeInfinity, double.PositiveInfinity) { }
43    protected DataAnalysisSolution(DataAnalysisProblemData problemData, double lowerEstimationLimit, double upperEstimationLimit)
44      : this() {
45      this.problemData = problemData;
46      this.lowerEstimationLimit = lowerEstimationLimit;
47      this.upperEstimationLimit = upperEstimationLimit;
48      Initialize();
49    }
50
51    [StorableConstructor]
52    private DataAnalysisSolution(bool deserializing) : base(deserializing) { }
53    [StorableHook(HookType.AfterDeserialization)]
54    private void Initialize() {
55      if (problemData != null)
56        RegisterProblemDataEvents();
57    }
58
59    [Storable]
60    private DataAnalysisProblemData problemData;
61    public DataAnalysisProblemData ProblemData {
62      get { return problemData; }
63      set {
64        if (problemData != value) {
65          if (value == null) throw new ArgumentNullException();
66          if (model != null && problemData != null && !problemData.InputVariables.Select(c => c.Value).SequenceEqual(
67            value.InputVariables.Select(c => c.Value)))
68            throw new ArgumentException("Could not set new problem data with different structure");
69
70          if (problemData != null) DeregisterProblemDataEvents();
71          problemData = value;
72          RegisterProblemDataEvents();
73          OnProblemDataChanged();
74          RecalculateEstimatedValues();
75        }
76      }
77    }
78
79    [Storable]
80    private IDataAnalysisModel model;
81    public IDataAnalysisModel Model {
82      get { return model; }
83      set {
84        if (model != value) {
85          if (value == null) throw new ArgumentNullException();
86          model = value;
87          OnModelChanged();
88          RecalculateEstimatedValues();
89        }
90      }
91    }
92
93    [Storable]
94    private double lowerEstimationLimit;
95    public double LowerEstimationLimit {
96      get { return lowerEstimationLimit; }
97      set {
98        if (lowerEstimationLimit != value) {
99          lowerEstimationLimit = value;
100          RecalculateEstimatedValues();
101        }
102      }
103    }
104
105    [Storable]
106    private double upperEstimationLimit;
107    public double UpperEstimationLimit {
108      get { return upperEstimationLimit; }
109      set {
110        if (upperEstimationLimit != value) {
111          upperEstimationLimit = value;
112          RecalculateEstimatedValues();
113        }
114      }
115    }
116
117    public abstract IEnumerable<double> EstimatedValues { get; }
118    public abstract IEnumerable<double> EstimatedTrainingValues { get; }
119    public abstract IEnumerable<double> EstimatedTestValues { get; }
120    protected abstract void RecalculateEstimatedValues();
121
122    #region Events
123    protected virtual void RegisterProblemDataEvents() {
124      ProblemData.ProblemDataChanged += new EventHandler(ProblemData_Changed);
125    }
126    protected virtual void DeregisterProblemDataEvents() {
127      ProblemData.ProblemDataChanged += new EventHandler(ProblemData_Changed);
128    }
129    private void ProblemData_Changed(object sender, EventArgs e) {
130      OnProblemDataChanged();
131    }
132
133    public event EventHandler ProblemDataChanged;
134    protected virtual void OnProblemDataChanged() {
135      var listeners = ProblemDataChanged;
136      if (listeners != null)
137        listeners(this, EventArgs.Empty);
138    }
139
140    public event EventHandler ModelChanged;
141    protected virtual void OnModelChanged() {
142      EventHandler handler = ModelChanged;
143      if (handler != null)
144        handler(this, EventArgs.Empty);
145    }
146
147    public event EventHandler EstimatedValuesChanged;
148    protected virtual void OnEstimatedValuesChanged() {
149      var listeners = EstimatedValuesChanged;
150      if (listeners != null)
151        listeners(this, EventArgs.Empty);
152    }
153    #endregion
154
155    public override IDeepCloneable Clone(Cloner cloner) {
156      DataAnalysisSolution clone = (DataAnalysisSolution)base.Clone(cloner);
157      clone.problemData = (DataAnalysisProblemData)cloner.Clone(problemData);
158      clone.model = (IDataAnalysisModel)cloner.Clone(model);
159      clone.lowerEstimationLimit = lowerEstimationLimit;
160      clone.upperEstimationLimit = upperEstimationLimit;
161      clone.Initialize();
162
163      return clone;
164    }
165  }
166}
Note: See TracBrowser for help on using the repository browser.