Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MemoryOperations/HeuristicLab.Problems.DataAnalysis/3.3/DataAnalysisSolution.cs @ 5722

Last change on this file since 5722 was 4722, checked in by swagner, 14 years ago

Merged cloning refactoring branch back into trunk (#922)

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    [StorableConstructor]
41    protected DataAnalysisSolution(bool deserializing) : base(deserializing) { }
42    protected DataAnalysisSolution(DataAnalysisSolution original, Cloner cloner)
43      : base(original, cloner) {
44      problemData = (DataAnalysisProblemData)cloner.Clone(original.problemData);
45      model = (IDataAnalysisModel)cloner.Clone(original.model);
46      lowerEstimationLimit = original.lowerEstimationLimit;
47      upperEstimationLimit = original.upperEstimationLimit;
48      AfterDeserialization();
49    }
50    protected DataAnalysisSolution()
51      : base() { }
52    protected DataAnalysisSolution(DataAnalysisProblemData problemData) : this(problemData, double.NegativeInfinity, double.PositiveInfinity) { }
53    protected DataAnalysisSolution(DataAnalysisProblemData problemData, double lowerEstimationLimit, double upperEstimationLimit)
54      : this() {
55      this.problemData = problemData;
56      this.lowerEstimationLimit = lowerEstimationLimit;
57      this.upperEstimationLimit = upperEstimationLimit;
58      AfterDeserialization();
59    }
60
61    [StorableHook(HookType.AfterDeserialization)]
62    private void AfterDeserialization() {
63      if (problemData != null)
64        RegisterProblemDataEvents();
65    }
66
67    [Storable]
68    private DataAnalysisProblemData problemData;
69    public DataAnalysisProblemData ProblemData {
70      get { return problemData; }
71      set {
72        if (problemData != value) {
73          if (value == null) throw new ArgumentNullException();
74          if (model != null && problemData != null && !problemData.InputVariables.Select(c => c.Value).SequenceEqual(
75            value.InputVariables.Select(c => c.Value)))
76            throw new ArgumentException("Could not set new problem data with different structure");
77
78          if (problemData != null) DeregisterProblemDataEvents();
79          problemData = value;
80          RegisterProblemDataEvents();
81          OnProblemDataChanged();
82          RecalculateEstimatedValues();
83        }
84      }
85    }
86
87    [Storable]
88    private IDataAnalysisModel model;
89    public IDataAnalysisModel Model {
90      get { return model; }
91      set {
92        if (model != value) {
93          if (value == null) throw new ArgumentNullException();
94          model = value;
95          OnModelChanged();
96          RecalculateEstimatedValues();
97        }
98      }
99    }
100
101    [Storable]
102    private double lowerEstimationLimit;
103    public double LowerEstimationLimit {
104      get { return lowerEstimationLimit; }
105      set {
106        if (lowerEstimationLimit != value) {
107          lowerEstimationLimit = value;
108          RecalculateEstimatedValues();
109        }
110      }
111    }
112
113    [Storable]
114    private double upperEstimationLimit;
115    public double UpperEstimationLimit {
116      get { return upperEstimationLimit; }
117      set {
118        if (upperEstimationLimit != value) {
119          upperEstimationLimit = value;
120          RecalculateEstimatedValues();
121        }
122      }
123    }
124
125    public abstract IEnumerable<double> EstimatedValues { get; }
126    public abstract IEnumerable<double> EstimatedTrainingValues { get; }
127    public abstract IEnumerable<double> EstimatedTestValues { get; }
128    protected abstract void RecalculateEstimatedValues();
129
130    #region Events
131    protected virtual void RegisterProblemDataEvents() {
132      ProblemData.ProblemDataChanged += new EventHandler(ProblemData_Changed);
133    }
134    protected virtual void DeregisterProblemDataEvents() {
135      ProblemData.ProblemDataChanged += new EventHandler(ProblemData_Changed);
136    }
137    private void ProblemData_Changed(object sender, EventArgs e) {
138      OnProblemDataChanged();
139    }
140
141    public event EventHandler ProblemDataChanged;
142    protected virtual void OnProblemDataChanged() {
143      var listeners = ProblemDataChanged;
144      if (listeners != null)
145        listeners(this, EventArgs.Empty);
146    }
147
148    public event EventHandler ModelChanged;
149    protected virtual void OnModelChanged() {
150      EventHandler listeners = ModelChanged;
151      if (listeners != null)
152        listeners(this, EventArgs.Empty);
153    }
154
155    public event EventHandler EstimatedValuesChanged;
156    protected virtual void OnEstimatedValuesChanged() {
157      var listeners = EstimatedValuesChanged;
158      if (listeners != null)
159        listeners(this, EventArgs.Empty);
160    }
161    #endregion
162
163  }
164}
Note: See TracBrowser for help on using the repository browser.