[5649] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7259] | 3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5649] | 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 |
|
---|
[6233] | 22 | using System;
|
---|
[5649] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification {
|
---|
| 31 | /// <summary>
|
---|
| 32 | /// Represents a symbolic classification model
|
---|
| 33 | /// </summary>
|
---|
| 34 | [StorableClass]
|
---|
| 35 | [Item(Name = "SymbolicDiscriminantFunctionClassificationModel", Description = "Represents a symbolic classification model unsing a discriminant function.")]
|
---|
[8594] | 36 | public class SymbolicDiscriminantFunctionClassificationModel : SymbolicClassificationModel, ISymbolicDiscriminantFunctionClassificationModel {
|
---|
[5649] | 37 |
|
---|
| 38 | [Storable]
|
---|
| 39 | private double[] thresholds;
|
---|
| 40 | public IEnumerable<double> Thresholds {
|
---|
| 41 | get { return (IEnumerable<double>)thresholds.Clone(); }
|
---|
[5736] | 42 | private set { thresholds = value.ToArray(); }
|
---|
[5649] | 43 | }
|
---|
[5678] | 44 | [Storable]
|
---|
| 45 | private double[] classValues;
|
---|
| 46 | public IEnumerable<double> ClassValues {
|
---|
| 47 | get { return (IEnumerable<double>)classValues.Clone(); }
|
---|
[5736] | 48 | private set { classValues = value.ToArray(); }
|
---|
[5678] | 49 | }
|
---|
[8594] | 50 |
|
---|
| 51 | private IDiscriminantFunctionThresholdCalculator thresholdCalculator;
|
---|
[5720] | 52 | [Storable]
|
---|
[8594] | 53 | public IDiscriminantFunctionThresholdCalculator ThresholdCalculator {
|
---|
| 54 | get { return thresholdCalculator; }
|
---|
| 55 | private set { thresholdCalculator = value; }
|
---|
| 56 | }
|
---|
[5720] | 57 |
|
---|
[8594] | 58 |
|
---|
[5649] | 59 | [StorableConstructor]
|
---|
| 60 | protected SymbolicDiscriminantFunctionClassificationModel(bool deserializing) : base(deserializing) { }
|
---|
| 61 | protected SymbolicDiscriminantFunctionClassificationModel(SymbolicDiscriminantFunctionClassificationModel original, Cloner cloner)
|
---|
| 62 | : base(original, cloner) {
|
---|
| 63 | classValues = (double[])original.classValues.Clone();
|
---|
| 64 | thresholds = (double[])original.thresholds.Clone();
|
---|
[8594] | 65 | thresholdCalculator = cloner.Clone(original.thresholdCalculator);
|
---|
[5649] | 66 | }
|
---|
[8594] | 67 | public SymbolicDiscriminantFunctionClassificationModel(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IDiscriminantFunctionThresholdCalculator thresholdCalculator,
|
---|
[5720] | 68 | double lowerEstimationLimit = double.MinValue, double upperEstimationLimit = double.MaxValue)
|
---|
[8594] | 69 | : base(tree, interpreter, lowerEstimationLimit, upperEstimationLimit) {
|
---|
[8531] | 70 | this.thresholds = new double[0];
|
---|
| 71 | this.classValues = new double[0];
|
---|
[8594] | 72 | this.ThresholdCalculator = thresholdCalculator;
|
---|
[5649] | 73 | }
|
---|
| 74 |
|
---|
[8594] | 75 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 76 | private void AfterDeserialization() {
|
---|
| 77 | if (ThresholdCalculator == null) ThresholdCalculator = new AccuracyMaximizationThresholdCalculator();
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[5649] | 80 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 81 | return new SymbolicDiscriminantFunctionClassificationModel(this, cloner);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[5736] | 84 | public void SetThresholdsAndClassValues(IEnumerable<double> thresholds, IEnumerable<double> classValues) {
|
---|
| 85 | var classValuesArr = classValues.ToArray();
|
---|
| 86 | var thresholdsArr = thresholds.ToArray();
|
---|
| 87 | if (thresholdsArr.Length != classValuesArr.Length) throw new ArgumentException();
|
---|
| 88 |
|
---|
| 89 | this.classValues = classValuesArr;
|
---|
| 90 | this.thresholds = thresholdsArr;
|
---|
| 91 | OnThresholdsChanged(EventArgs.Empty);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[8594] | 94 | public override void RecalculateModelParameters(IClassificationProblemData problemData, IEnumerable<int> rows) {
|
---|
| 95 | double[] classValues;
|
---|
| 96 | double[] thresholds;
|
---|
| 97 | var targetClassValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
|
---|
| 98 | var estimatedTrainingValues = GetEstimatedValues(problemData.Dataset, rows);
|
---|
| 99 | thresholdCalculator.Calculate(problemData, estimatedTrainingValues, targetClassValues, out classValues, out thresholds);
|
---|
| 100 | SetThresholdsAndClassValues(thresholds, classValues);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[5649] | 103 | public IEnumerable<double> GetEstimatedValues(Dataset dataset, IEnumerable<int> rows) {
|
---|
[8594] | 104 | return Interpreter.GetSymbolicExpressionTreeValues(SymbolicExpressionTree, dataset, rows).LimitToRange(LowerEstimationLimit, UpperEstimationLimit);
|
---|
[5649] | 105 | }
|
---|
| 106 |
|
---|
[8594] | 107 | public override IEnumerable<double> GetEstimatedClassValues(Dataset dataset, IEnumerable<int> rows) {
|
---|
[8531] | 108 | if (!Thresholds.Any() && !ClassValues.Any()) throw new ArgumentException("No thresholds and class values were set for the current symbolic classification model.");
|
---|
[5649] | 109 | foreach (var x in GetEstimatedValues(dataset, rows)) {
|
---|
| 110 | int classIndex = 0;
|
---|
[5678] | 111 | // find first threshold value which is larger than x => class index = threshold index + 1
|
---|
[5649] | 112 | for (int i = 0; i < thresholds.Length; i++) {
|
---|
| 113 | if (x > thresholds[i]) classIndex++;
|
---|
| 114 | else break;
|
---|
| 115 | }
|
---|
[5657] | 116 | yield return classValues.ElementAt(classIndex - 1);
|
---|
[5649] | 117 | }
|
---|
| 118 | }
|
---|
| 119 |
|
---|
[8594] | 120 |
|
---|
| 121 | public override ISymbolicClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
|
---|
| 122 | return CreateDiscriminantClassificationSolution(problemData);
|
---|
| 123 | }
|
---|
| 124 | public SymbolicDiscriminantFunctionClassificationSolution CreateDiscriminantClassificationSolution(IClassificationProblemData problemData) {
|
---|
[8528] | 125 | return new SymbolicDiscriminantFunctionClassificationSolution(this, new ClassificationProblemData(problemData));
|
---|
[6604] | 126 | }
|
---|
| 127 | IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) {
|
---|
[8594] | 128 | return CreateDiscriminantClassificationSolution(problemData);
|
---|
[6604] | 129 | }
|
---|
| 130 | IDiscriminantFunctionClassificationSolution IDiscriminantFunctionClassificationModel.CreateDiscriminantFunctionClassificationSolution(IClassificationProblemData problemData) {
|
---|
[8594] | 131 | return CreateDiscriminantClassificationSolution(problemData);
|
---|
[6604] | 132 | }
|
---|
| 133 |
|
---|
[5649] | 134 | #region events
|
---|
| 135 | public event EventHandler ThresholdsChanged;
|
---|
| 136 | protected virtual void OnThresholdsChanged(EventArgs e) {
|
---|
| 137 | var listener = ThresholdsChanged;
|
---|
| 138 | if (listener != null) listener(this, e);
|
---|
| 139 | }
|
---|
[5720] | 140 | #endregion
|
---|
[5649] | 141 | }
|
---|
| 142 | }
|
---|