Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.DataAnalysis.Views/3.4/RandomForestModelView.cs @ 15231

Last change on this file since 15231 was 15134, checked in by gkronber, 7 years ago

#2690: merged r14839,r15103:15106,r15123,r15124 from trunk to stable

File size: 3.3 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 HeuristicLab.Common;
23using HeuristicLab.Core.Views;
24using HeuristicLab.MainForm;
25using HeuristicLab.Problems.DataAnalysis;
26using HeuristicLab.Problems.DataAnalysis.Symbolic;                       
27using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
28
29namespace HeuristicLab.Algorithms.DataAnalysis.Views {
30  [View("Random forest model")]
31  [Content(typeof(IRandomForestModel), true)]
32  public partial class RandomForestModelView : ItemView {
33
34    public new IRandomForestModel Content {
35      get { return (IRandomForestModel)base.Content; }
36      set { base.Content = value; }
37    }
38
39    protected override void SetEnabledStateOfControls() {
40      base.SetEnabledStateOfControls();
41      listBox.Enabled = Content != null;
42      viewHost.Enabled = Content != null;
43    }
44
45    public RandomForestModelView()
46      : base() {
47      InitializeComponent();
48    }
49
50    protected override void OnContentChanged() {
51      base.OnContentChanged();
52      if (Content == null) {
53        viewHost.Content = null;
54        listBox.Items.Clear();
55      } else {
56        viewHost.Content = null;
57        listBox.Items.Clear();
58        var rfModel = Content;
59        var numTrees = rfModel.NumberOfTrees;
60        for (int i = 0; i < numTrees; i++) {
61          listBox.Items.Add(i + 1);
62        }
63      }
64    }
65
66    private void listBox_SelectedIndexChanged(object sender, System.EventArgs e) {
67      if (listBox.SelectedItem == null) viewHost.Content = null;
68      else {
69        var idx = (int)listBox.SelectedItem;
70        viewHost.Content = CreateModel(idx);
71      }
72    }
73
74    private void listBox_DoubleClick(object sender, System.EventArgs e) {
75      var selectedItem = listBox.SelectedItem;
76      if (selectedItem == null) return;
77      var idx = (int)listBox.SelectedItem;
78      MainFormManager.MainForm.ShowContent(CreateModel(idx));
79    }
80
81    private IContent CreateModel(int idx) {
82      idx -= 1;
83      var rfModel = Content;
84      var rfClassModel = rfModel as IClassificationModel; // rfModel is always a IRegressionModel and a IClassificationModel
85      var targetVariable = rfClassModel.TargetVariable;
86      if (rfModel == null) return null;
87      if (idx < 0 || idx >= rfModel.NumberOfTrees)
88        return null;
89      var syModel = new SymbolicRegressionModel(targetVariable, rfModel.ExtractTree(idx),
90          new SymbolicDataAnalysisExpressionTreeLinearInterpreter());
91      return syModel;
92    }
93  }
94}
Note: See TracBrowser for help on using the repository browser.