Free cookie consent management tool by TermsFeed Policy Generator

source: branches/M5Regression/HeuristicLab.Algorithms.DataAnalysis/3.4/M5Regression/LeafTypes/ComplexLeaf.cs @ 15614

Last change on this file since 15614 was 15614, checked in by bwerth, 6 years ago

#2847 made changes to M5 according to review comments

File size: 3.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2017 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 System;
23using System.Linq;
24using System.Threading;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Problems.DataAnalysis;
30
31namespace HeuristicLab.Algorithms.DataAnalysis {
32  [StorableClass]
33  [Item("ComplexLeaf", "A leaf type that uses an arbitriary RegressionAlgorithm to create leaf models")]
34  public class ComplexLeaf : ParameterizedNamedItem, ILeafModel {
35    public const string RegressionParameterName = "Regression";
36    public IValueParameter<IDataAnalysisAlgorithm<IRegressionProblem>> RegressionParameter {
37      get { return Parameters[RegressionParameterName] as IValueParameter<IDataAnalysisAlgorithm<IRegressionProblem>>; }
38    }
39    public IDataAnalysisAlgorithm<IRegressionProblem> Regression {
40      get { return RegressionParameter.Value; }
41    }
42
43    #region Constructors & Cloning
44    [StorableConstructor]
45    private ComplexLeaf(bool deserializing) : base(deserializing) { }
46    private ComplexLeaf(ComplexLeaf original, Cloner cloner) : base(original, cloner) { }
47    public ComplexLeaf() {
48      var regression = new KernelRidgeRegression();
49      Parameters.Add(new ValueParameter<IDataAnalysisAlgorithm<IRegressionProblem>>(RegressionParameterName, "The algorithm creating RegressionModels", regression));
50    }
51    public override IDeepCloneable Clone(Cloner cloner) {
52      return new ComplexLeaf(this, cloner);
53    }
54    #endregion
55
56    #region IModelType
57    public bool ProvidesConfidence {
58      get { return false; }
59    }
60    public IRegressionModel Build(IRegressionProblemData pd, IRandom random, CancellationToken cancellationToken, out int noParameters) {
61      if (pd.Dataset.Rows < MinLeafSize(pd)) throw new ArgumentException("The number of training instances is too small to create a linear model");
62      noParameters = pd.Dataset.Rows + 1;
63      Regression.Problem = new RegressionProblem {ProblemData = pd};
64      var res = M5StaticUtilities.RunSubAlgorithm(Regression, random.Next(), cancellationToken);
65      var t = res.Select(x => x.Value).OfType<IRegressionSolution>().FirstOrDefault();
66      if (t == null) throw new ArgumentException("No RegressionSolution was provided by the algorithm");
67      return t.Model;
68    }
69
70    public int MinLeafSize(IRegressionProblemData pd) {
71      return 3;
72    }
73    #endregion
74  }
75}
Note: See TracBrowser for help on using the repository browser.