[475] | 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 {
|
---|
[482] | 33 | public class ClassificationMeanSquaredErrorEvaluator : MeanSquaredErrorEvaluator {
|
---|
[478] | 34 | private const double EPSILON = 1.0E-6;
|
---|
[479] | 35 | private double[] classesArr;
|
---|
[475] | 36 | public override string Description {
|
---|
| 37 | get {
|
---|
| 38 | return @"Evaluates 'FunctionTree' for all samples of 'DataSet' and calculates the mean-squared-error
|
---|
| 39 | for the estimated values vs. the real values of 'TargetVariable'.";
|
---|
| 40 | }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | public ClassificationMeanSquaredErrorEvaluator()
|
---|
| 44 | : base() {
|
---|
| 45 | AddVariableInfo(new VariableInfo("TargetClassValues", "The original class values of target variable (for instance negative=0 and positive=1).", typeof(ItemList<DoubleData>), VariableKind.In));
|
---|
| 46 | }
|
---|
| 47 |
|
---|
[479] | 48 | public override IOperation Apply(IScope scope) {
|
---|
[475] | 49 | ItemList<DoubleData> classes = GetVariableValue<ItemList<DoubleData>>("TargetClassValues", scope, true);
|
---|
[479] | 50 | classesArr = new double[classes.Count];
|
---|
[475] | 51 | for(int i = 0; i < classesArr.Length; i++) classesArr[i] = classes[i].Data;
|
---|
| 52 | Array.Sort(classesArr);
|
---|
[479] | 53 | return base.Apply(scope);
|
---|
| 54 | }
|
---|
[475] | 55 |
|
---|
[482] | 56 | public override void Evaluate(int start, int end) {
|
---|
[475] | 57 | double errorsSquaredSum = 0;
|
---|
[479] | 58 | for(int sample = start; sample < end; sample++) {
|
---|
| 59 | double estimated = GetEstimatedValue(sample);
|
---|
| 60 | double original = GetOriginalValue(sample);
|
---|
[480] | 61 | SetOriginalValue(sample, estimated);
|
---|
[479] | 62 | if(!double.IsNaN(original) && !double.IsInfinity(original)) {
|
---|
| 63 | double error = estimated - original;
|
---|
| 64 | // between classes use squared error
|
---|
| 65 | // on the lower end and upper end only add linear error if the absolute error is larger than 1
|
---|
| 66 | // the error>1.0 constraint is needed for balance because in the interval ]-1, 1[ the squared error is smaller than the absolute error
|
---|
[511] | 67 | if((IsEqual(original, classesArr[0]) && error < -1.0) ||
|
---|
| 68 | (IsEqual(original, classesArr[classesArr.Length - 1]) && error > 1.0)) {
|
---|
[479] | 69 | errorsSquaredSum += Math.Abs(error); // only add linear error below the smallest class or above the largest class
|
---|
| 70 | } else {
|
---|
| 71 | errorsSquaredSum += error * error;
|
---|
| 72 | }
|
---|
[475] | 73 | }
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[479] | 76 | errorsSquaredSum /= (end - start);
|
---|
[475] | 77 | if(double.IsNaN(errorsSquaredSum) || double.IsInfinity(errorsSquaredSum)) {
|
---|
| 78 | errorsSquaredSum = double.MaxValue;
|
---|
| 79 | }
|
---|
[482] | 80 | mse.Data = errorsSquaredSum;
|
---|
[475] | 81 | }
|
---|
[478] | 82 |
|
---|
| 83 | private bool IsEqual(double x, double y) {
|
---|
| 84 | return Math.Abs(x - y) < EPSILON;
|
---|
| 85 | }
|
---|
[475] | 86 | }
|
---|
| 87 | }
|
---|