[169] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2008 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 System.Linq;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Operators;
|
---|
| 29 | using HeuristicLab.Functions;
|
---|
| 30 | using HeuristicLab.DataAnalysis;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.StructureIdentification {
|
---|
| 33 | public class MCCEvaluator : GPEvaluatorBase {
|
---|
| 34 | public override string Description {
|
---|
| 35 | get {
|
---|
| 36 | return @"TASK";
|
---|
| 37 | }
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | public MCCEvaluator()
|
---|
| 41 | : base() {
|
---|
[170] | 42 | AddVariableInfo(new VariableInfo("ClassSeparation", "The value of separation between negative and positive target classification values (for instance 0.5 if negative=0 and positive=1).", typeof(DoubleData), VariableKind.In));
|
---|
[169] | 43 | }
|
---|
| 44 |
|
---|
[191] | 45 | private double[] original = new double[1];
|
---|
| 46 | private double[] estimated = new double[1];
|
---|
[169] | 47 | public override double Evaluate(IScope scope, IFunctionTree functionTree, int targetVariable, Dataset dataset) {
|
---|
[367] | 48 | int trainingStart = GetVariableValue<IntData>("TrainingSamplesStart", scope, true).Data;
|
---|
| 49 | int trainingEnd = GetVariableValue<IntData>("TrainingSamplesEnd", scope, true).Data;
|
---|
| 50 | int nSamples = trainingEnd-trainingStart;
|
---|
[170] | 51 | double limit = GetVariableValue<DoubleData>("ClassSeparation", scope, false).Data;
|
---|
[367] | 52 | if(estimated.Length != nSamples) {
|
---|
| 53 | estimated = new double[nSamples];
|
---|
| 54 | original = new double[nSamples];
|
---|
[191] | 55 | }
|
---|
[367] | 56 |
|
---|
[169] | 57 | double positive = 0;
|
---|
| 58 | double negative = 0;
|
---|
[367] | 59 | double targetMean = dataset.GetMean(targetVariable, trainingStart, trainingEnd);
|
---|
| 60 | for(int sample = trainingStart; sample < trainingEnd; sample++) {
|
---|
[396] | 61 | double est = evaluator.Evaluate(sample);
|
---|
[169] | 62 | double orig = dataset.GetValue(sample, targetVariable);
|
---|
| 63 | if(double.IsNaN(est) || double.IsInfinity(est)) {
|
---|
| 64 | est = targetMean + maximumPunishment;
|
---|
| 65 | } else if(est > targetMean + maximumPunishment) {
|
---|
| 66 | est = targetMean + maximumPunishment;
|
---|
| 67 | } else if(est < targetMean - maximumPunishment) {
|
---|
| 68 | est = targetMean - maximumPunishment;
|
---|
| 69 | }
|
---|
[367] | 70 | estimated[sample-trainingStart] = est;
|
---|
| 71 | original[sample-trainingStart] = orig;
|
---|
[169] | 72 | if(orig >= limit) positive++;
|
---|
| 73 | else negative++;
|
---|
| 74 | }
|
---|
| 75 | Array.Sort(estimated, original);
|
---|
[170] | 76 | double best_mcc = -1.0;
|
---|
[169] | 77 | double tp = 0;
|
---|
| 78 | double fn = positive;
|
---|
| 79 | double tn = negative;
|
---|
| 80 | double fp = 0;
|
---|
| 81 | for(int i = original.Length-1; i >= 0 ; i--) {
|
---|
| 82 | if(original[i] >= limit) {
|
---|
| 83 | tp++; fn--;
|
---|
| 84 | } else {
|
---|
| 85 | tn--; fp++;
|
---|
| 86 | }
|
---|
[170] | 87 | double mcc = (tp * tn - fp * fn) / Math.Sqrt(positive * (tp + fn) * (tn + fp) * negative);
|
---|
[169] | 88 | if(best_mcc < mcc) {
|
---|
| 89 | best_mcc = mcc;
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
[400] | 92 | scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data = totalEvaluatedNodes + treeSize * (trainingEnd - trainingStart);
|
---|
[169] | 93 | return best_mcc;
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 | }
|
---|