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