[6240] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15583] | 3 | * Copyright (C) 2002-2018 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 |
|
---|
[14523] | 22 | using System.Threading;
|
---|
[6240] | 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Optimization;
|
---|
[8786] | 27 | using HeuristicLab.Parameters;
|
---|
[6240] | 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
| 32 | /// <summary>
|
---|
| 33 | /// Random forest regression data analysis algorithm.
|
---|
| 34 | /// </summary>
|
---|
[13238] | 35 | [Item("Random Forest Regression (RF)", "Random forest regression data analysis algorithm (wrapper for ALGLIB).")]
|
---|
[12504] | 36 | [Creatable(CreatableAttribute.Categories.DataAnalysisRegression, Priority = 120)]
|
---|
[6240] | 37 | [StorableClass]
|
---|
| 38 | public sealed class RandomForestRegression : FixedDataAnalysisAlgorithm<IRegressionProblem> {
|
---|
| 39 | private const string RandomForestRegressionModelResultName = "Random forest regression solution";
|
---|
| 40 | private const string NumberOfTreesParameterName = "Number of trees";
|
---|
| 41 | private const string RParameterName = "R";
|
---|
[8786] | 42 | private const string MParameterName = "M";
|
---|
| 43 | private const string SeedParameterName = "Seed";
|
---|
| 44 | private const string SetSeedRandomlyParameterName = "SetSeedRandomly";
|
---|
[13204] | 45 | private const string CreateSolutionParameterName = "CreateSolution";
|
---|
[8786] | 46 |
|
---|
[6240] | 47 | #region parameter properties
|
---|
[8786] | 48 | public IFixedValueParameter<IntValue> NumberOfTreesParameter {
|
---|
| 49 | get { return (IFixedValueParameter<IntValue>)Parameters[NumberOfTreesParameterName]; }
|
---|
[6240] | 50 | }
|
---|
[8786] | 51 | public IFixedValueParameter<DoubleValue> RParameter {
|
---|
| 52 | get { return (IFixedValueParameter<DoubleValue>)Parameters[RParameterName]; }
|
---|
[6240] | 53 | }
|
---|
[8786] | 54 | public IFixedValueParameter<DoubleValue> MParameter {
|
---|
| 55 | get { return (IFixedValueParameter<DoubleValue>)Parameters[MParameterName]; }
|
---|
| 56 | }
|
---|
| 57 | public IFixedValueParameter<IntValue> SeedParameter {
|
---|
| 58 | get { return (IFixedValueParameter<IntValue>)Parameters[SeedParameterName]; }
|
---|
| 59 | }
|
---|
| 60 | public IFixedValueParameter<BoolValue> SetSeedRandomlyParameter {
|
---|
| 61 | get { return (IFixedValueParameter<BoolValue>)Parameters[SetSeedRandomlyParameterName]; }
|
---|
| 62 | }
|
---|
[13204] | 63 | public IFixedValueParameter<BoolValue> CreateSolutionParameter {
|
---|
| 64 | get { return (IFixedValueParameter<BoolValue>)Parameters[CreateSolutionParameterName]; }
|
---|
| 65 | }
|
---|
[6240] | 66 | #endregion
|
---|
| 67 | #region properties
|
---|
| 68 | public int NumberOfTrees {
|
---|
| 69 | get { return NumberOfTreesParameter.Value.Value; }
|
---|
| 70 | set { NumberOfTreesParameter.Value.Value = value; }
|
---|
| 71 | }
|
---|
| 72 | public double R {
|
---|
| 73 | get { return RParameter.Value.Value; }
|
---|
| 74 | set { RParameter.Value.Value = value; }
|
---|
| 75 | }
|
---|
[8786] | 76 | public double M {
|
---|
| 77 | get { return MParameter.Value.Value; }
|
---|
| 78 | set { MParameter.Value.Value = value; }
|
---|
| 79 | }
|
---|
| 80 | public int Seed {
|
---|
| 81 | get { return SeedParameter.Value.Value; }
|
---|
| 82 | set { SeedParameter.Value.Value = value; }
|
---|
| 83 | }
|
---|
| 84 | public bool SetSeedRandomly {
|
---|
| 85 | get { return SetSeedRandomlyParameter.Value.Value; }
|
---|
| 86 | set { SetSeedRandomlyParameter.Value.Value = value; }
|
---|
| 87 | }
|
---|
[13204] | 88 | public bool CreateSolution {
|
---|
| 89 | get { return CreateSolutionParameter.Value.Value; }
|
---|
| 90 | set { CreateSolutionParameter.Value.Value = value; }
|
---|
| 91 | }
|
---|
[6240] | 92 | #endregion
|
---|
| 93 | [StorableConstructor]
|
---|
| 94 | private RandomForestRegression(bool deserializing) : base(deserializing) { }
|
---|
| 95 | private RandomForestRegression(RandomForestRegression original, Cloner cloner)
|
---|
| 96 | : base(original, cloner) {
|
---|
| 97 | }
|
---|
[8786] | 98 |
|
---|
[6240] | 99 | public RandomForestRegression()
|
---|
| 100 | : base() {
|
---|
| 101 | Parameters.Add(new FixedValueParameter<IntValue>(NumberOfTreesParameterName, "The number of trees in the forest. Should be between 50 and 100", new IntValue(50)));
|
---|
| 102 | 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] | 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 | Parameters.Add(new FixedValueParameter<IntValue>(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
| 105 | Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
[13204] | 106 | Parameters.Add(new FixedValueParameter<BoolValue>(CreateSolutionParameterName, "Flag that indicates if a solution should be produced at the end of the run", new BoolValue(true)));
|
---|
| 107 | Parameters[CreateSolutionParameterName].Hidden = true;
|
---|
| 108 |
|
---|
[6240] | 109 | Problem = new RegressionProblem();
|
---|
| 110 | }
|
---|
[8786] | 111 |
|
---|
[6240] | 112 | [StorableHook(HookType.AfterDeserialization)]
|
---|
[8786] | 113 | private void AfterDeserialization() {
|
---|
[13204] | 114 | // BackwardsCompatibility3.3
|
---|
| 115 | #region Backwards compatible code, remove with 3.4
|
---|
[8786] | 116 | if (!Parameters.ContainsKey(MParameterName))
|
---|
| 117 | 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)));
|
---|
| 118 | if (!Parameters.ContainsKey(SeedParameterName))
|
---|
| 119 | Parameters.Add(new FixedValueParameter<IntValue>(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
| 120 | if (!Parameters.ContainsKey((SetSeedRandomlyParameterName)))
|
---|
| 121 | Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
[13204] | 122 | if (!Parameters.ContainsKey(CreateSolutionParameterName)) {
|
---|
| 123 | Parameters.Add(new FixedValueParameter<BoolValue>(CreateSolutionParameterName, "Flag that indicates if a solution should be produced at the end of the run", new BoolValue(true)));
|
---|
| 124 | Parameters[CreateSolutionParameterName].Hidden = true;
|
---|
| 125 | }
|
---|
| 126 | #endregion
|
---|
[8786] | 127 | }
|
---|
[6240] | 128 |
|
---|
| 129 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 130 | return new RandomForestRegression(this, cloner);
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | #region random forest
|
---|
[14523] | 134 | protected override void Run(CancellationToken cancellationToken) {
|
---|
[6240] | 135 | double rmsError, avgRelError, outOfBagRmsError, outOfBagAvgRelError;
|
---|
[16071] | 136 | if (SetSeedRandomly) Seed = Random.RandomSeedGenerator.GetSeed();
|
---|
[13204] | 137 | var model = CreateRandomForestRegressionModel(Problem.ProblemData, NumberOfTrees, R, M, Seed,
|
---|
| 138 | out rmsError, out avgRelError, out outOfBagRmsError, out outOfBagAvgRelError);
|
---|
[8786] | 139 |
|
---|
[6240] | 140 | 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] | 141 | 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)));
|
---|
| 142 | 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)));
|
---|
| 143 | 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)));
|
---|
[13204] | 144 |
|
---|
| 145 | if (CreateSolution) {
|
---|
[13941] | 146 | var solution = new RandomForestRegressionSolution(model, (IRegressionProblemData)Problem.ProblemData.Clone());
|
---|
[13204] | 147 | Results.Add(new Result(RandomForestRegressionModelResultName, "The random forest regression solution.", solution));
|
---|
| 148 | }
|
---|
[6240] | 149 | }
|
---|
| 150 |
|
---|
[13204] | 151 | // keep for compatibility with old API
|
---|
| 152 | public static RandomForestRegressionSolution CreateRandomForestRegressionSolution(IRegressionProblemData problemData, int nTrees, double r, double m, int seed,
|
---|
[6240] | 153 | out double rmsError, out double avgRelError, out double outOfBagRmsError, out double outOfBagAvgRelError) {
|
---|
[13204] | 154 | var model = CreateRandomForestRegressionModel(problemData, nTrees, r, m, seed,
|
---|
| 155 | out rmsError, out avgRelError, out outOfBagRmsError, out outOfBagAvgRelError);
|
---|
[13941] | 156 | return new RandomForestRegressionSolution(model, (IRegressionProblemData)problemData.Clone());
|
---|
[6240] | 157 | }
|
---|
[13204] | 158 |
|
---|
| 159 | public static RandomForestModel CreateRandomForestRegressionModel(IRegressionProblemData problemData, int nTrees,
|
---|
| 160 | double r, double m, int seed,
|
---|
| 161 | out double rmsError, out double avgRelError, out double outOfBagRmsError, out double outOfBagAvgRelError) {
|
---|
[15464] | 162 | return RandomForestModel.CreateRegressionModel(problemData, nTrees, r, m, seed,
|
---|
| 163 | rmsError: out rmsError, avgRelError: out avgRelError, outOfBagRmsError: out outOfBagRmsError, outOfBagAvgRelError: out outOfBagAvgRelError);
|
---|
[13204] | 164 | }
|
---|
| 165 |
|
---|
[6240] | 166 | #endregion
|
---|
| 167 | }
|
---|
| 168 | }
|
---|