Free cookie consent management tool by TermsFeed Policy Generator

source: branches/SimplifierViewsProgress/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression.Views/3.4/SymbolicRegressionSolutionErrorCharacteristicsCurveView.cs @ 15353

Last change on this file since 15353 was 14843, checked in by gkronber, 8 years ago

#2697: applied r14390, r14391, r14393, r14394, r14396 again (resolving conflicts)

File size: 4.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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;
24using System.Collections.Generic;
25using System.Diagnostics.Contracts;
26using System.Linq;
27using HeuristicLab.Algorithms.DataAnalysis;
28using HeuristicLab.MainForm;
29using HeuristicLab.Problems.DataAnalysis.Views;
30
31namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression.Views {
32  [View("Error Characteristics Curve")]
33  [Content(typeof(ISymbolicRegressionSolution))]
34  public partial class SymbolicRegressionSolutionErrorCharacteristicsCurveView : RegressionSolutionErrorCharacteristicsCurveView {
35    public SymbolicRegressionSolutionErrorCharacteristicsCurveView() {
36      InitializeComponent();
37    }
38
39    public new ISymbolicRegressionSolution Content {
40      get { return (ISymbolicRegressionSolution)base.Content; }
41      set { base.Content = value; }
42    }
43
44    private IRegressionSolution CreateLinearRegressionSolution() {
45      if (Content == null) throw new InvalidOperationException();
46      double rmse, cvRmsError;
47      var problemData = (IRegressionProblemData)ProblemData.Clone();
48      if (!problemData.TrainingIndices.Any()) return null; // don't create an LR model if the problem does not have a training set (e.g. loaded into an existing model)
49
50      var usedVariables = Content.Model.VariablesUsedForPrediction;
51
52      var usedDoubleVariables = usedVariables
53        .Where(name => problemData.Dataset.VariableHasType<double>(name))
54      .Distinct();
55
56      var usedFactorVariables = usedVariables
57        .Where(name => problemData.Dataset.VariableHasType<string>(name))
58        .Distinct();
59
60      // gkronber: for binary factors we actually produce a binary variable in the new dataset
61      // but only if the variable is not used as a full factor anyway (LR creates binary columns anyway)
62      var usedBinaryFactors =
63        Content.Model.SymbolicExpressionTree.IterateNodesPostfix().OfType<BinaryFactorVariableTreeNode>()
64        .Where(node => !usedFactorVariables.Contains(node.VariableName))
65        .Select(node => Tuple.Create(node.VariableValue, node.VariableValue));
66
67      // create a new problem and dataset
68      var variableNames =
69        usedDoubleVariables
70        .Concat(usedFactorVariables)
71        .Concat(usedBinaryFactors.Select(t => t.Item1 + "=" + t.Item2))
72        .Concat(new string[] { problemData.TargetVariable })
73        .ToArray();
74      var variableValues =
75        usedDoubleVariables.Select(name => (IList)problemData.Dataset.GetDoubleValues(name).ToList())
76        .Concat(usedFactorVariables.Select(name => problemData.Dataset.GetStringValues(name).ToList()))
77        .Concat(
78          // create binary variable
79          usedBinaryFactors.Select(t => problemData.Dataset.GetReadOnlyStringValues(t.Item1).Select(val => val == t.Item2 ? 1.0 : 0.0).ToList())
80        )
81        .Concat(new[] { problemData.Dataset.GetDoubleValues(problemData.TargetVariable).ToList() });
82
83      var newDs = new Dataset(variableNames, variableValues);
84      var newProblemData = new RegressionProblemData(newDs, variableNames.Take(variableNames.Length - 1), variableNames.Last());
85      newProblemData.TrainingPartition.Start = problemData.TrainingPartition.Start;
86      newProblemData.TrainingPartition.End = problemData.TrainingPartition.End;
87      newProblemData.TestPartition.Start = problemData.TestPartition.Start;
88      newProblemData.TestPartition.End = problemData.TestPartition.End;
89
90      var solution = LinearRegression.CreateLinearRegressionSolution(newProblemData, out rmse, out cvRmsError);
91      solution.Name = "Baseline (linear subset)";
92      return solution;
93    }
94
95
96    protected override IEnumerable<IRegressionSolution> CreateBaselineSolutions() {
97      foreach (var sol in base.CreateBaselineSolutions()) yield return sol;
98
99      // does not support lagged variables
100      if (Content.Model.SymbolicExpressionTree.IterateNodesPrefix().OfType<LaggedVariableTreeNode>().Any()) yield break;
101
102      yield return CreateLinearRegressionSolution();
103    }
104  }
105}
Note: See TracBrowser for help on using the repository browser.