[3442] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3442] | 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;
|
---|
[4068] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Drawing;
|
---|
| 25 | using System.Linq;
|
---|
[4722] | 26 | using HeuristicLab.Common;
|
---|
[3442] | 27 | using HeuristicLab.Core;
|
---|
[5373] | 28 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[3442] | 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[4250] | 30 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
|
---|
[3442] | 31 |
|
---|
| 32 | namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic {
|
---|
| 33 | /// <summary>
|
---|
| 34 | /// Represents a solution for a symbolic regression problem which can be visualized in the GUI.
|
---|
| 35 | /// </summary>
|
---|
| 36 | [Item("SymbolicRegressionSolution", "Represents a solution for a symbolic regression problem which can be visualized in the GUI.")]
|
---|
| 37 | [StorableClass]
|
---|
[4415] | 38 | public class SymbolicRegressionSolution : DataAnalysisSolution {
|
---|
[3884] | 39 | public override Image ItemImage {
|
---|
[5287] | 40 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Function; }
|
---|
[3462] | 41 | }
|
---|
| 42 |
|
---|
[3884] | 43 | public new SymbolicRegressionModel Model {
|
---|
| 44 | get { return (SymbolicRegressionModel)base.Model; }
|
---|
| 45 | set { base.Model = value; }
|
---|
[3462] | 46 | }
|
---|
| 47 |
|
---|
[4415] | 48 | protected List<double> estimatedValues;
|
---|
[3462] | 49 | public override IEnumerable<double> EstimatedValues {
|
---|
| 50 | get {
|
---|
[3485] | 51 | if (estimatedValues == null) RecalculateEstimatedValues();
|
---|
[4468] | 52 | return estimatedValues;
|
---|
[3462] | 53 | }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | public override IEnumerable<double> EstimatedTrainingValues {
|
---|
[4468] | 57 | get { return GetEstimatedValues(ProblemData.TrainingIndizes); }
|
---|
[3462] | 58 | }
|
---|
| 59 |
|
---|
| 60 | public override IEnumerable<double> EstimatedTestValues {
|
---|
[4468] | 61 | get { return GetEstimatedValues(ProblemData.TestIndizes); }
|
---|
[3462] | 62 | }
|
---|
[4468] | 63 |
|
---|
[4722] | 64 | [StorableConstructor]
|
---|
| 65 | protected SymbolicRegressionSolution(bool deserializing) : base(deserializing) { }
|
---|
| 66 | protected SymbolicRegressionSolution(SymbolicRegressionSolution original, Cloner cloner)
|
---|
| 67 | : base(original, cloner) {
|
---|
| 68 | }
|
---|
| 69 | public SymbolicRegressionSolution(DataAnalysisProblemData problemData, SymbolicRegressionModel model, double lowerEstimationLimit, double upperEstimationLimit)
|
---|
| 70 | : base(problemData, lowerEstimationLimit, upperEstimationLimit) {
|
---|
| 71 | this.Model = model;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 75 | return new SymbolicRegressionSolution(this, cloner);
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | protected override void RecalculateEstimatedValues() {
|
---|
[5373] | 79 | int minLag = GetMinimumLagFromTree(Model.SymbolicExpressionTree.Root);
|
---|
[4722] | 80 | IEnumerable<double> calculatedValues =
|
---|
| 81 | from x in Model.GetEstimatedValues(ProblemData, 0 - minLag, ProblemData.Dataset.Rows)
|
---|
| 82 | let boundedX = Math.Min(UpperEstimationLimit, Math.Max(LowerEstimationLimit, x))
|
---|
| 83 | select double.IsNaN(boundedX) ? UpperEstimationLimit : boundedX;
|
---|
[4797] | 84 | estimatedValues = Enumerable.Repeat(UpperEstimationLimit, Math.Abs(minLag)).Concat(calculatedValues).ToList();
|
---|
[4722] | 85 | OnEstimatedValuesChanged();
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[4468] | 88 | public virtual IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) {
|
---|
| 89 | if (estimatedValues == null) RecalculateEstimatedValues();
|
---|
| 90 | foreach (int row in rows)
|
---|
| 91 | yield return estimatedValues[row];
|
---|
| 92 | }
|
---|
[5373] | 93 |
|
---|
| 94 | protected int GetMinimumLagFromTree(SymbolicExpressionTreeNode node) {
|
---|
| 95 | if (node == null) return 0;
|
---|
| 96 | int lag = 0;
|
---|
| 97 |
|
---|
| 98 | var laggedTreeNode = node as ILaggedTreeNode;
|
---|
| 99 | if (laggedTreeNode != null) lag += laggedTreeNode.Lag;
|
---|
[5377] | 100 | else if (node.Symbol is Derivative) lag -= 4;
|
---|
[5373] | 101 |
|
---|
| 102 | int subtreeLag = 0;
|
---|
| 103 | foreach (var subtree in node.SubTrees) {
|
---|
| 104 | subtreeLag = Math.Min(subtreeLag, GetMinimumLagFromTree(subtree));
|
---|
| 105 | }
|
---|
| 106 | return lag + subtreeLag;
|
---|
| 107 | }
|
---|
[3442] | 108 | }
|
---|
| 109 | }
|
---|