[6642] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14185] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6642] | 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;
|
---|
[14251] | 23 | using System.Collections;
|
---|
[13003] | 24 | using System.Collections.Generic;
|
---|
[14238] | 25 | using System.Diagnostics.Contracts;
|
---|
[6642] | 26 | using System.Linq;
|
---|
| 27 | using HeuristicLab.Algorithms.DataAnalysis;
|
---|
| 28 | using HeuristicLab.MainForm;
|
---|
| 29 | using HeuristicLab.Problems.DataAnalysis.Views;
|
---|
| 30 |
|
---|
| 31 | namespace 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();
|
---|
[13003] | 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)
|
---|
[6642] | 49 |
|
---|
[14251] | 50 | var usedVariables = Content.Model.SymbolicExpressionTree.IterateNodesPostfix()
|
---|
| 51 | .OfType<IVariableTreeNode>()
|
---|
| 52 | .Select(node => node.VariableName).ToArray();
|
---|
| 53 |
|
---|
| 54 | var usedDoubleVariables = usedVariables
|
---|
| 55 | .Where(name => problemData.Dataset.VariableHasType<double>(name))
|
---|
[14238] | 56 | .Distinct();
|
---|
[6642] | 57 |
|
---|
[14251] | 58 | var usedFactorVariables = usedVariables
|
---|
| 59 | .Where(name => problemData.Dataset.VariableHasType<string>(name))
|
---|
[14238] | 60 | .Distinct();
|
---|
[6642] | 61 |
|
---|
[14251] | 62 | // gkronber: for binary factors we actually produce a binary variable in the new dataset
|
---|
| 63 | // but only if the variable is not used as a full factor anyway (LR creates binary columns anyway)
|
---|
| 64 | var usedBinaryFactors =
|
---|
| 65 | Content.Model.SymbolicExpressionTree.IterateNodesPostfix().OfType<BinaryFactorVariableTreeNode>()
|
---|
| 66 | .Where(node => !usedFactorVariables.Contains(node.VariableName))
|
---|
| 67 | .Select(node => Tuple.Create(node.VariableValue, node.VariableValue));
|
---|
| 68 |
|
---|
[14238] | 69 | // create a new problem and dataset
|
---|
| 70 | var variableNames =
|
---|
| 71 | usedDoubleVariables
|
---|
[14251] | 72 | .Concat(usedFactorVariables)
|
---|
| 73 | .Concat(usedBinaryFactors.Select(t => t.Item1 + "=" + t.Item2))
|
---|
[14238] | 74 | .Concat(new string[] { problemData.TargetVariable })
|
---|
| 75 | .ToArray();
|
---|
| 76 | var variableValues =
|
---|
[14251] | 77 | usedDoubleVariables.Select(name => (IList)problemData.Dataset.GetDoubleValues(name).ToList())
|
---|
| 78 | .Concat(usedFactorVariables.Select(name => problemData.Dataset.GetStringValues(name).ToList()))
|
---|
[14238] | 79 | .Concat(
|
---|
[14251] | 80 | // create binary variable
|
---|
| 81 | usedBinaryFactors.Select(t => problemData.Dataset.GetReadOnlyStringValues(t.Item1).Select(val => val == t.Item2 ? 1.0 : 0.0).ToList())
|
---|
[14238] | 82 | )
|
---|
| 83 | .Concat(new[] { problemData.Dataset.GetDoubleValues(problemData.TargetVariable).ToList() });
|
---|
| 84 |
|
---|
| 85 | var newDs = new Dataset(variableNames, variableValues);
|
---|
| 86 | var newProblemData = new RegressionProblemData(newDs, variableNames.Take(variableNames.Length - 1), variableNames.Last());
|
---|
| 87 | newProblemData.TrainingPartition.Start = problemData.TrainingPartition.Start;
|
---|
| 88 | newProblemData.TrainingPartition.End = problemData.TrainingPartition.End;
|
---|
| 89 | newProblemData.TestPartition.Start = problemData.TestPartition.Start;
|
---|
| 90 | newProblemData.TestPartition.End = problemData.TestPartition.End;
|
---|
| 91 |
|
---|
| 92 | var solution = LinearRegression.CreateLinearRegressionSolution(newProblemData, out rmse, out cvRmsError);
|
---|
[13003] | 93 | solution.Name = "Baseline (linear subset)";
|
---|
[6642] | 94 | return solution;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 |
|
---|
[13003] | 98 | protected override IEnumerable<IRegressionSolution> CreateBaselineSolutions() {
|
---|
| 99 | foreach (var sol in base.CreateBaselineSolutions()) yield return sol;
|
---|
[14238] | 100 |
|
---|
| 101 | // does not support lagged variables
|
---|
| 102 | if (Content.Model.SymbolicExpressionTree.IterateNodesPrefix().OfType<LaggedVariableTreeNode>().Any()) yield break;
|
---|
| 103 |
|
---|
[13003] | 104 | yield return CreateLinearRegressionSolution();
|
---|
[6642] | 105 | }
|
---|
| 106 | }
|
---|
| 107 | }
|
---|