[15830] | 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.Collections.Generic;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Parameters;
|
---|
[16847] | 28 | using HEAL.Attic;
|
---|
[15830] | 29 |
|
---|
| 30 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
[16847] | 31 | [StorableType("502B1429-7A28-45C1-A60A-93E72CB3AF4A")]
|
---|
[17080] | 32 | [Item("Splitter", "A split selector that uses the ratio between Variances^(1/Order) to determine good splits.")]
|
---|
[17081] | 33 | public sealed class Splitter : SplitterBase {
|
---|
[15830] | 34 | public const string OrderParameterName = "Order";
|
---|
| 35 | public IFixedValueParameter<DoubleValue> OrderParameter {
|
---|
| 36 | get { return (IFixedValueParameter<DoubleValue>)Parameters[OrderParameterName]; }
|
---|
| 37 | }
|
---|
| 38 | public double Order {
|
---|
| 39 | get { return OrderParameter.Value.Value; }
|
---|
[16847] | 40 | set { OrderParameter.Value.Value = value; }
|
---|
[15830] | 41 | }
|
---|
| 42 |
|
---|
| 43 | #region Constructors & Cloning
|
---|
| 44 | [StorableConstructor]
|
---|
[17081] | 45 | private Splitter(StorableConstructorFlag _) { }
|
---|
| 46 | private Splitter(Splitter original, Cloner cloner) : base(original, cloner) { }
|
---|
| 47 | public Splitter() {
|
---|
[16847] | 48 | Parameters.Add(new FixedValueParameter<DoubleValue>(OrderParameterName, "The exponent in the split calculation sum (x_i - x_avg)^Order (default=5).", new DoubleValue(5)));
|
---|
[15830] | 49 | }
|
---|
| 50 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[17081] | 51 | return new Splitter(this, cloner);
|
---|
[15830] | 52 | }
|
---|
| 53 | #endregion
|
---|
| 54 |
|
---|
| 55 | protected override void AttributeSplit(IReadOnlyList<double> attValues, IReadOnlyList<double> targetValues, int minLeafSize, out int position, out double maxImpurity, out double splitValue) {
|
---|
| 56 | position = 0;
|
---|
[16852] | 57 | maxImpurity = double.NegativeInfinity;
|
---|
[15830] | 58 | splitValue = 0.0;
|
---|
| 59 | var length = targetValues.Count;
|
---|
| 60 |
|
---|
| 61 | // weka code
|
---|
| 62 | var low = 0;
|
---|
| 63 | var high = length - 1;
|
---|
| 64 | if (high - low + 1 < 4) return;
|
---|
| 65 | var len = Math.Max(minLeafSize - 1, high - low + 1 < 5 ? 1 : (high - low + 1) / 5);
|
---|
| 66 | position = low;
|
---|
| 67 | var part = low + len - 1;
|
---|
| 68 | var imp = new OrderImpurityCalculator(part + 1, targetValues, Order);
|
---|
| 69 |
|
---|
| 70 | for (var i = low + len; i < high - len; i++) {
|
---|
| 71 | imp.Increment(targetValues[i], OrderImpurityCalculator.IncrementType.Left);
|
---|
| 72 | if (attValues[i].IsAlmost(attValues[i + 1])) continue; //splits can not be made between to equal points
|
---|
| 73 | if (imp.Impurity < maxImpurity) continue;
|
---|
| 74 | maxImpurity = imp.Impurity;
|
---|
| 75 | splitValue = (attValues[i] + attValues[i + 1]) / 2;
|
---|
| 76 | position = i;
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 | } |
---|