Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/SymbolicRegressionSolution.cs @ 4800

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

Corrected SymbolicRegressionSolution to use UpperEstimationLimit instead double.NaN (ticket #939).

File size: 3.9 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.Drawing;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
30
31namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic {
32  /// <summary>
33  /// Represents a solution for a symbolic regression problem which can be visualized in the GUI.
34  /// </summary>
35  [Item("SymbolicRegressionSolution", "Represents a solution for a symbolic regression problem which can be visualized in the GUI.")]
36  [StorableClass]
37  public class SymbolicRegressionSolution : DataAnalysisSolution {
38    public override Image ItemImage {
39      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Function; }
40    }
41
42    public new SymbolicRegressionModel Model {
43      get { return (SymbolicRegressionModel)base.Model; }
44      set { base.Model = value; }
45    }
46
47    protected List<double> estimatedValues;
48    public override IEnumerable<double> EstimatedValues {
49      get {
50        if (estimatedValues == null) RecalculateEstimatedValues();
51        return estimatedValues;
52      }
53    }
54
55    public override IEnumerable<double> EstimatedTrainingValues {
56      get { return GetEstimatedValues(ProblemData.TrainingIndizes); }
57    }
58
59    public override IEnumerable<double> EstimatedTestValues {
60      get { return GetEstimatedValues(ProblemData.TestIndizes); }
61    }
62
63    [StorableConstructor]
64    protected SymbolicRegressionSolution(bool deserializing) : base(deserializing) { }
65    protected SymbolicRegressionSolution(SymbolicRegressionSolution original, Cloner cloner)
66      : base(original, cloner) {
67    }
68    public SymbolicRegressionSolution(DataAnalysisProblemData problemData, SymbolicRegressionModel model, double lowerEstimationLimit, double upperEstimationLimit)
69      : base(problemData, lowerEstimationLimit, upperEstimationLimit) {
70      this.Model = model;
71    }
72
73    public override IDeepCloneable Clone(Cloner cloner) {
74      return new SymbolicRegressionSolution(this, cloner);
75    }
76
77    protected override void RecalculateEstimatedValues() {
78      int minLag = 0;
79      var laggedTreeNodes = Model.SymbolicExpressionTree.IterateNodesPrefix().OfType<LaggedVariableTreeNode>();
80      if (laggedTreeNodes.Any())
81        minLag = laggedTreeNodes.Min(node => node.Lag);
82      IEnumerable<double> calculatedValues =
83          from x in Model.GetEstimatedValues(ProblemData, 0 - minLag, ProblemData.Dataset.Rows)
84          let boundedX = Math.Min(UpperEstimationLimit, Math.Max(LowerEstimationLimit, x))
85          select double.IsNaN(boundedX) ? UpperEstimationLimit : boundedX;
86      estimatedValues = Enumerable.Repeat(UpperEstimationLimit, Math.Abs(minLag)).Concat(calculatedValues).ToList();
87      OnEstimatedValuesChanged();
88    }
89
90    public virtual IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
91      if (estimatedValues == null) RecalculateEstimatedValues();
92      foreach (int row in rows)
93        yield return estimatedValues[row];
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.