Free cookie consent management tool by TermsFeed Policy Generator

source: branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification.Views/3.4/SolutionComparisonView.cs @ 14241

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

#2650: added support for factor variables in specific solution comparison view for symbolic classification solutions

File size: 4.7 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.Generic;
24using System.Linq;
25using HeuristicLab.Algorithms.DataAnalysis;
26using HeuristicLab.MainForm;
27using HeuristicLab.Problems.DataAnalysis.Views.Classification;
28
29namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification.Views {
30  [View("Solution Comparison")]
31  [Content(typeof(ISymbolicClassificationSolution))]
32  public partial class SolutionComparisonView : ClassificationSolutionComparisonView {
33
34    public SolutionComparisonView() {
35      InitializeComponent();
36    }
37
38    public new ISymbolicClassificationSolution Content {
39      get { return (ISymbolicClassificationSolution)base.Content; }
40      set { base.Content = value; }
41    }
42
43    protected override IEnumerable<IClassificationSolution> GenerateClassificationSolutions() {
44      var solutionsBase = base.GenerateClassificationSolutions();
45      var solutions = new List<IClassificationSolution>();
46
47      var symbolicSolution = Content;
48
49      // does not support lagged variables
50      if (symbolicSolution.Model.SymbolicExpressionTree.IterateNodesPrefix().OfType<LaggedVariableTreeNode>().Any()) return solutionsBase;
51
52      var problemData = (IClassificationProblemData)symbolicSolution.ProblemData.Clone();
53      if (!problemData.TrainingIndices.Any()) return null; // don't create an comparison models if the problem does not have a training set (e.g. loaded into an existing model)
54
55      var usedDoubleVariables =
56        symbolicSolution.Model.SymbolicExpressionTree.IterateNodesPostfix()
57        .OfType<VariableTreeNode>()
58        .Select(node => node.VariableName)
59      .Concat(
60        symbolicSolution.Model.SymbolicExpressionTree.IterateNodesPostfix()
61        .OfType<VariableConditionTreeNode>()
62        .Select(node => node.VariableName)
63        )
64      .Distinct();
65
66      var usedFactorVariables =
67        symbolicSolution.Model.SymbolicExpressionTree.IterateNodesPostfix()
68        .OfType<FactorVariableTreeNode>()
69        .Select(node => Tuple.Create(node.VariableName, node.VariableValue))
70        .Distinct();
71
72      // create a new problem and dataset
73      var variableNames =
74        usedDoubleVariables
75        .Concat(usedFactorVariables.Select(t => t.Item1 + "=" + t.Item2))
76        .Concat(new string[] { problemData.TargetVariable })
77        .ToArray();
78      var variableValues =
79        usedDoubleVariables.Select(name => problemData.Dataset.GetDoubleValues(name).ToList())
80        .Concat(
81        // create binary variable
82          usedFactorVariables.Select(t => problemData.Dataset.GetReadOnlyStringValues(t.Item1).Select(val => val == t.Item2 ? 1.0 : 0.0).ToList())
83        )
84        .Concat(new[] { problemData.Dataset.GetDoubleValues(problemData.TargetVariable).ToList() });
85
86      var newDs = new Dataset(variableNames, variableValues);
87      var newProblemData = new ClassificationProblemData(newDs, variableNames.Take(variableNames.Length - 1), variableNames.Last());
88      newProblemData.TrainingPartition.Start = problemData.TrainingPartition.Start;
89      newProblemData.TrainingPartition.End = problemData.TrainingPartition.End;
90      newProblemData.TestPartition.Start = problemData.TestPartition.Start;
91      newProblemData.TestPartition.End = problemData.TestPartition.End;
92
93      try {
94        var oneR = OneR.CreateOneRSolution(newProblemData);
95        oneR.Name = "OneR Classification Solution (subset)";
96        solutions.Add(oneR);
97      } catch (NotSupportedException) { } catch (ArgumentException) { }
98      try {
99        var lda = LinearDiscriminantAnalysis.CreateLinearDiscriminantAnalysisSolution(newProblemData);
100        lda.Name = "Linear Discriminant Analysis Solution (subset)";
101        solutions.Add(lda);
102      } catch (NotSupportedException) { } catch (ArgumentException) { }
103
104      return solutionsBase.Concat(solutions);
105    }
106  }
107}
Note: See TracBrowser for help on using the repository browser.