[6240] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12009] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6240] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Optimization;
|
---|
[8786] | 29 | using HeuristicLab.Parameters;
|
---|
[6240] | 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 32 |
|
---|
| 33 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
| 34 | /// <summary>
|
---|
| 35 | /// Random forest regression data analysis algorithm.
|
---|
| 36 | /// </summary>
|
---|
| 37 | [Item("Random Forest Regression", "Random forest regression data analysis algorithm (wrapper for ALGLIB).")]
|
---|
| 38 | [Creatable("Data Analysis")]
|
---|
| 39 | [StorableClass]
|
---|
| 40 | public sealed class RandomForestRegression : FixedDataAnalysisAlgorithm<IRegressionProblem> {
|
---|
| 41 | private const string RandomForestRegressionModelResultName = "Random forest regression solution";
|
---|
| 42 | private const string NumberOfTreesParameterName = "Number of trees";
|
---|
| 43 | private const string RParameterName = "R";
|
---|
[8786] | 44 | private const string MParameterName = "M";
|
---|
| 45 | private const string SeedParameterName = "Seed";
|
---|
| 46 | private const string SetSeedRandomlyParameterName = "SetSeedRandomly";
|
---|
| 47 |
|
---|
[6240] | 48 | #region parameter properties
|
---|
[8786] | 49 | public IFixedValueParameter<IntValue> NumberOfTreesParameter {
|
---|
| 50 | get { return (IFixedValueParameter<IntValue>)Parameters[NumberOfTreesParameterName]; }
|
---|
[6240] | 51 | }
|
---|
[8786] | 52 | public IFixedValueParameter<DoubleValue> RParameter {
|
---|
| 53 | get { return (IFixedValueParameter<DoubleValue>)Parameters[RParameterName]; }
|
---|
[6240] | 54 | }
|
---|
[8786] | 55 | public IFixedValueParameter<DoubleValue> MParameter {
|
---|
| 56 | get { return (IFixedValueParameter<DoubleValue>)Parameters[MParameterName]; }
|
---|
| 57 | }
|
---|
| 58 | public IFixedValueParameter<IntValue> SeedParameter {
|
---|
| 59 | get { return (IFixedValueParameter<IntValue>)Parameters[SeedParameterName]; }
|
---|
| 60 | }
|
---|
| 61 | public IFixedValueParameter<BoolValue> SetSeedRandomlyParameter {
|
---|
| 62 | get { return (IFixedValueParameter<BoolValue>)Parameters[SetSeedRandomlyParameterName]; }
|
---|
| 63 | }
|
---|
[6240] | 64 | #endregion
|
---|
| 65 | #region properties
|
---|
| 66 | public int NumberOfTrees {
|
---|
| 67 | get { return NumberOfTreesParameter.Value.Value; }
|
---|
| 68 | set { NumberOfTreesParameter.Value.Value = value; }
|
---|
| 69 | }
|
---|
| 70 | public double R {
|
---|
| 71 | get { return RParameter.Value.Value; }
|
---|
| 72 | set { RParameter.Value.Value = value; }
|
---|
| 73 | }
|
---|
[8786] | 74 | public double M {
|
---|
| 75 | get { return MParameter.Value.Value; }
|
---|
| 76 | set { MParameter.Value.Value = value; }
|
---|
| 77 | }
|
---|
| 78 | public int Seed {
|
---|
| 79 | get { return SeedParameter.Value.Value; }
|
---|
| 80 | set { SeedParameter.Value.Value = value; }
|
---|
| 81 | }
|
---|
| 82 | public bool SetSeedRandomly {
|
---|
| 83 | get { return SetSeedRandomlyParameter.Value.Value; }
|
---|
| 84 | set { SetSeedRandomlyParameter.Value.Value = value; }
|
---|
| 85 | }
|
---|
[6240] | 86 | #endregion
|
---|
| 87 | [StorableConstructor]
|
---|
| 88 | private RandomForestRegression(bool deserializing) : base(deserializing) { }
|
---|
| 89 | private RandomForestRegression(RandomForestRegression original, Cloner cloner)
|
---|
| 90 | : base(original, cloner) {
|
---|
| 91 | }
|
---|
[8786] | 92 |
|
---|
[6240] | 93 | public RandomForestRegression()
|
---|
| 94 | : base() {
|
---|
| 95 | Parameters.Add(new FixedValueParameter<IntValue>(NumberOfTreesParameterName, "The number of trees in the forest. Should be between 50 and 100", new IntValue(50)));
|
---|
| 96 | 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)));
|
---|
[8786] | 97 | 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)));
|
---|
| 98 | Parameters.Add(new FixedValueParameter<IntValue>(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
| 99 | Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
[6240] | 100 | Problem = new RegressionProblem();
|
---|
| 101 | }
|
---|
[8786] | 102 |
|
---|
[6240] | 103 | [StorableHook(HookType.AfterDeserialization)]
|
---|
[8786] | 104 | private void AfterDeserialization() {
|
---|
| 105 | if (!Parameters.ContainsKey(MParameterName))
|
---|
| 106 | 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)));
|
---|
| 107 | if (!Parameters.ContainsKey(SeedParameterName))
|
---|
| 108 | Parameters.Add(new FixedValueParameter<IntValue>(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
| 109 | if (!Parameters.ContainsKey((SetSeedRandomlyParameterName)))
|
---|
| 110 | Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
| 111 | }
|
---|
[6240] | 112 |
|
---|
| 113 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 114 | return new RandomForestRegression(this, cloner);
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | #region random forest
|
---|
| 118 | protected override void Run() {
|
---|
| 119 | double rmsError, avgRelError, outOfBagRmsError, outOfBagAvgRelError;
|
---|
[8786] | 120 | if (SetSeedRandomly) Seed = new System.Random().Next();
|
---|
| 121 |
|
---|
| 122 | var solution = CreateRandomForestRegressionSolution(Problem.ProblemData, NumberOfTrees, R, M, Seed, out rmsError, out avgRelError, out outOfBagRmsError, out outOfBagAvgRelError);
|
---|
[6240] | 123 | Results.Add(new Result(RandomForestRegressionModelResultName, "The random forest regression solution.", solution));
|
---|
| 124 | 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)));
|
---|
[6241] | 125 | 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)));
|
---|
| 126 | 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)));
|
---|
| 127 | 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)));
|
---|
[6240] | 128 | }
|
---|
| 129 |
|
---|
[8786] | 130 | public static IRegressionSolution CreateRandomForestRegressionSolution(IRegressionProblemData problemData, int nTrees, double r, double m, int seed,
|
---|
[6240] | 131 | out double rmsError, out double avgRelError, out double outOfBagRmsError, out double outOfBagAvgRelError) {
|
---|
[11006] | 132 | var model = RandomForestModel.CreateRegressionModel(problemData, nTrees, r, m, seed, out rmsError, out avgRelError, out outOfBagRmsError, out outOfBagAvgRelError);
|
---|
| 133 | return new RandomForestRegressionSolution((IRegressionProblemData)problemData.Clone(), model);
|
---|
[6240] | 134 | }
|
---|
| 135 | #endregion
|
---|
| 136 | }
|
---|
| 137 | }
|
---|