Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Algorithms.DataAnalysis.Views/3.4/RandomForestModelEvaluationView.cs @ 16662

Last change on this file since 16662 was 16662, checked in by gkronber, 5 years ago

#2925: merged all changes from trunk to branch (up to r16659)

File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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
21using System;
22using HeuristicLab.Common;
23using HeuristicLab.MainForm;
24using HeuristicLab.Problems.DataAnalysis;
25using HeuristicLab.Problems.DataAnalysis.Symbolic;
26using HeuristicLab.Problems.DataAnalysis.Symbolic.Classification;
27using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
28using HeuristicLab.Problems.DataAnalysis.Views;
29
30namespace HeuristicLab.Algorithms.DataAnalysis.Views {
31  [View("RF Model Evaluation")]
32  [Content(typeof(IRandomForestRegressionSolution), false)]
33  [Content(typeof(IRandomForestClassificationSolution), false)]
34  public partial class RandomForestModelEvaluationView : DataAnalysisSolutionEvaluationView {
35
36    protected override void SetEnabledStateOfControls() {
37      base.SetEnabledStateOfControls();
38      listBox.Enabled = Content != null;
39      viewHost.Enabled = Content != null;
40    }
41
42    public RandomForestModelEvaluationView()
43      : base() {
44      InitializeComponent();
45    }
46
47    protected override void OnContentChanged() {
48      base.OnContentChanged();
49      if (Content == null) {
50        viewHost.Content = null;
51        listBox.Items.Clear();
52      } else {
53        viewHost.Content = null;
54        listBox.Items.Clear();
55        var classSol = Content as IRandomForestClassificationSolution;
56        var regSol = Content as IRandomForestRegressionSolution;
57        var numTrees = classSol != null ? classSol.NumberOfTrees : regSol != null ? regSol.NumberOfTrees : 0;
58        for (int i = 0; i < numTrees; i++) {
59          listBox.Items.Add(i + 1);
60        }
61      }
62    }
63
64    private void listBox_SelectedIndexChanged(object sender, System.EventArgs e) {
65      if (listBox.SelectedItem == null) viewHost.Content = null;
66      else {
67        var idx = (int)listBox.SelectedItem;
68        viewHost.Content = CreateModel(idx);
69      }
70    }
71
72    private void listBox_DoubleClick(object sender, System.EventArgs e) {
73      var selectedItem = listBox.SelectedItem;
74      if (selectedItem == null) return;
75      var idx = (int)listBox.SelectedItem;
76      MainFormManager.MainForm.ShowContent(CreateModel(idx));
77    }
78
79    private IContent CreateModel(int idx) {
80      idx -= 1;
81      var rfModel = Content.Model as RandomForestModel;
82      if (rfModel == null) return null;
83      var regProblemData = Content.ProblemData as IRegressionProblemData;
84      var classProblemData = Content.ProblemData as IClassificationProblemData;
85      if (idx < 0 || idx >= rfModel.NumberOfTrees)
86        return null;
87      if (regProblemData != null) {
88        var syModel = new SymbolicRegressionModel(regProblemData.TargetVariable, rfModel.ExtractTree(idx),
89          new SymbolicDataAnalysisExpressionTreeLinearInterpreter());
90        return syModel.CreateRegressionSolution(regProblemData);
91      } else if (classProblemData != null) {
92        var syModel = new SymbolicDiscriminantFunctionClassificationModel(classProblemData.TargetVariable, rfModel.ExtractTree(idx),
93          new SymbolicDataAnalysisExpressionTreeLinearInterpreter(), new NormalDistributionCutPointsThresholdCalculator());
94        syModel.RecalculateModelParameters(classProblemData, classProblemData.TrainingIndices);
95        return syModel.CreateClassificationSolution(classProblemData);
96      } else throw new InvalidProgramException();
97    }
98  }
99}
Note: See TracBrowser for help on using the repository browser.