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