Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestRegression.cs @ 13042

Last change on this file since 13042 was 12504, checked in by mkommend, 9 years ago

#2025: Changed categories for all creatables.

File size: 7.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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;
24using HeuristicLab.Data;
25using HeuristicLab.Optimization;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Problems.DataAnalysis;
29
30namespace HeuristicLab.Algorithms.DataAnalysis {
31  /// <summary>
32  /// Random forest regression data analysis algorithm.
33  /// </summary>
34  [Item("Random Forest Regression", "Random forest regression data analysis algorithm (wrapper for ALGLIB).")]
35  [Creatable(CreatableAttribute.Categories.DataAnalysisRegression, Priority = 120)]
36  [StorableClass]
37  public sealed class RandomForestRegression : FixedDataAnalysisAlgorithm<IRegressionProblem> {
38    private const string RandomForestRegressionModelResultName = "Random forest regression solution";
39    private const string NumberOfTreesParameterName = "Number of trees";
40    private const string RParameterName = "R";
41    private const string MParameterName = "M";
42    private const string SeedParameterName = "Seed";
43    private const string SetSeedRandomlyParameterName = "SetSeedRandomly";
44
45    #region parameter properties
46    public IFixedValueParameter<IntValue> NumberOfTreesParameter {
47      get { return (IFixedValueParameter<IntValue>)Parameters[NumberOfTreesParameterName]; }
48    }
49    public IFixedValueParameter<DoubleValue> RParameter {
50      get { return (IFixedValueParameter<DoubleValue>)Parameters[RParameterName]; }
51    }
52    public IFixedValueParameter<DoubleValue> MParameter {
53      get { return (IFixedValueParameter<DoubleValue>)Parameters[MParameterName]; }
54    }
55    public IFixedValueParameter<IntValue> SeedParameter {
56      get { return (IFixedValueParameter<IntValue>)Parameters[SeedParameterName]; }
57    }
58    public IFixedValueParameter<BoolValue> SetSeedRandomlyParameter {
59      get { return (IFixedValueParameter<BoolValue>)Parameters[SetSeedRandomlyParameterName]; }
60    }
61    #endregion
62    #region properties
63    public int NumberOfTrees {
64      get { return NumberOfTreesParameter.Value.Value; }
65      set { NumberOfTreesParameter.Value.Value = value; }
66    }
67    public double R {
68      get { return RParameter.Value.Value; }
69      set { RParameter.Value.Value = value; }
70    }
71    public double M {
72      get { return MParameter.Value.Value; }
73      set { MParameter.Value.Value = value; }
74    }
75    public int Seed {
76      get { return SeedParameter.Value.Value; }
77      set { SeedParameter.Value.Value = value; }
78    }
79    public bool SetSeedRandomly {
80      get { return SetSeedRandomlyParameter.Value.Value; }
81      set { SetSeedRandomlyParameter.Value.Value = value; }
82    }
83    #endregion
84    [StorableConstructor]
85    private RandomForestRegression(bool deserializing) : base(deserializing) { }
86    private RandomForestRegression(RandomForestRegression original, Cloner cloner)
87      : base(original, cloner) {
88    }
89
90    public RandomForestRegression()
91      : base() {
92      Parameters.Add(new FixedValueParameter<IntValue>(NumberOfTreesParameterName, "The number of trees in the forest. Should be between 50 and 100", new IntValue(50)));
93      Parameters.Add(new FixedValueParameter<DoubleValue>(RParameterName, "The ratio of the training set that will be used in the construction of individual trees (0<r<=1). Should be adjusted depending on the noise level in the dataset in the range from 0.66 (low noise) to 0.05 (high noise). This parameter should be adjusted to achieve good generalization error.", new DoubleValue(0.3)));
94      Parameters.Add(new FixedValueParameter<DoubleValue>(MParameterName, "The ratio of features that will be used in the construction of individual trees (0<m<=1)", new DoubleValue(0.5)));
95      Parameters.Add(new FixedValueParameter<IntValue>(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
96      Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
97      Problem = new RegressionProblem();
98    }
99
100    [StorableHook(HookType.AfterDeserialization)]
101    private void AfterDeserialization() {
102      if (!Parameters.ContainsKey(MParameterName))
103        Parameters.Add(new FixedValueParameter<DoubleValue>(MParameterName, "The ratio of features that will be used in the construction of individual trees (0<m<=1)", new DoubleValue(0.5)));
104      if (!Parameters.ContainsKey(SeedParameterName))
105        Parameters.Add(new FixedValueParameter<IntValue>(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
106      if (!Parameters.ContainsKey((SetSeedRandomlyParameterName)))
107        Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
108    }
109
110    public override IDeepCloneable Clone(Cloner cloner) {
111      return new RandomForestRegression(this, cloner);
112    }
113
114    #region random forest
115    protected override void Run() {
116      double rmsError, avgRelError, outOfBagRmsError, outOfBagAvgRelError;
117      if (SetSeedRandomly) Seed = new System.Random().Next();
118
119      var solution = CreateRandomForestRegressionSolution(Problem.ProblemData, NumberOfTrees, R, M, Seed, out rmsError, out avgRelError, out outOfBagRmsError, out outOfBagAvgRelError);
120      Results.Add(new Result(RandomForestRegressionModelResultName, "The random forest regression solution.", solution));
121      Results.Add(new Result("Root mean square error", "The root of the mean of squared errors of the random forest regression solution on the training set.", new DoubleValue(rmsError)));
122      Results.Add(new Result("Average relative error", "The average of relative errors of the random forest regression solution on the training set.", new PercentValue(avgRelError)));
123      Results.Add(new Result("Root mean square error (out-of-bag)", "The out-of-bag root of the mean of squared errors of the random forest regression solution.", new DoubleValue(outOfBagRmsError)));
124      Results.Add(new Result("Average relative error (out-of-bag)", "The out-of-bag average of relative errors of the random forest regression solution.", new PercentValue(outOfBagAvgRelError)));
125    }
126
127    public static IRegressionSolution CreateRandomForestRegressionSolution(IRegressionProblemData problemData, int nTrees, double r, double m, int seed,
128      out double rmsError, out double avgRelError, out double outOfBagRmsError, out double outOfBagAvgRelError) {
129      var model = RandomForestModel.CreateRegressionModel(problemData, nTrees, r, m, seed, out rmsError, out avgRelError, out outOfBagRmsError, out outOfBagAvgRelError);
130      return new RandomForestRegressionSolution((IRegressionProblemData)problemData.Clone(), model);
131    }
132    #endregion
133  }
134}
Note: See TracBrowser for help on using the repository browser.