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 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using System.Threading;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Problems.DataAnalysis;
|
---|
29 | using HEAL.Attic;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
32 | [StorableType("F34A0ED9-2CF6-4DEE-850D-08790663B66D")]
|
---|
33 | [Item("ComplexLeaf", "A leaf type that uses an arbitrary RegressionAlgorithm to create leaf models")]
|
---|
34 | public sealed class ComplexLeaf : LeafBase {
|
---|
35 | public const string RegressionParameterName = "Regression";
|
---|
36 | public IValueParameter<IDataAnalysisAlgorithm<IRegressionProblem>> RegressionParameter {
|
---|
37 | get { return (IValueParameter<IDataAnalysisAlgorithm<IRegressionProblem>>)Parameters[RegressionParameterName]; }
|
---|
38 | }
|
---|
39 | public IDataAnalysisAlgorithm<IRegressionProblem> Regression {
|
---|
40 | get { return RegressionParameter.Value; }
|
---|
41 | set { RegressionParameter.Value = value; }
|
---|
42 | }
|
---|
43 |
|
---|
44 | #region Constructors & Cloning
|
---|
45 | [StorableConstructor]
|
---|
46 | private ComplexLeaf(StorableConstructorFlag _) : base(_) { }
|
---|
47 | private ComplexLeaf(ComplexLeaf original, Cloner cloner) : base(original, cloner) { }
|
---|
48 | public ComplexLeaf() {
|
---|
49 | var regression = new KernelRidgeRegression();
|
---|
50 | Parameters.Add(new ValueParameter<IDataAnalysisAlgorithm<IRegressionProblem>>(RegressionParameterName, "The algorithm creating RegressionModels", regression));
|
---|
51 | }
|
---|
52 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
53 | return new ComplexLeaf(this, cloner);
|
---|
54 | }
|
---|
55 | #endregion
|
---|
56 |
|
---|
57 | #region IModelType
|
---|
58 | public override bool ProvidesConfidence {
|
---|
59 | get { return false; }
|
---|
60 | }
|
---|
61 | public override IRegressionModel Build(IRegressionProblemData pd, IRandom random, CancellationToken cancellationToken, out int noParameters) {
|
---|
62 | if (pd.Dataset.Rows < MinLeafSize(pd)) throw new ArgumentException("The number of training instances is too small to create a linear model");
|
---|
63 | noParameters = pd.Dataset.Rows + 1;
|
---|
64 | Regression.Problem = new RegressionProblem { ProblemData = pd };
|
---|
65 | var res = RegressionTreeUtilities.RunSubAlgorithm(Regression, random.Next(), cancellationToken);
|
---|
66 | var t = res.Select(x => x.Value).OfType<IRegressionSolution>().FirstOrDefault();
|
---|
67 | if (t == null) throw new ArgumentException("No RegressionSolution was provided by the algorithm");
|
---|
68 | return t.Model;
|
---|
69 | }
|
---|
70 | public override int MinLeafSize(IRegressionProblemData pd) {
|
---|
71 | return 3;
|
---|
72 | }
|
---|
73 | #endregion
|
---|
74 | }
|
---|
75 | } |
---|