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