#region License Information
/* HeuristicLab
* Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification.SingleObjective {
[Item("Bounded Mean squared error Evaluator", "Calculates the bounded mean squared error of a symbolic classification solution (estimations above or below the class values are only penaltilized linearly.")]
[StorableClass]
public class SymbolicClassificationSingleObjectiveBoundedMeanSquaredErrorEvaluator : SymbolicClassificationSingleObjectiveEvaluator {
[StorableConstructor]
protected SymbolicClassificationSingleObjectiveBoundedMeanSquaredErrorEvaluator(bool deserializing) : base(deserializing) { }
protected SymbolicClassificationSingleObjectiveBoundedMeanSquaredErrorEvaluator(SymbolicClassificationSingleObjectiveBoundedMeanSquaredErrorEvaluator original, Cloner cloner) : base(original, cloner) { }
public override IDeepCloneable Clone(Cloner cloner) {
return new SymbolicClassificationSingleObjectiveBoundedMeanSquaredErrorEvaluator(this, cloner);
}
public SymbolicClassificationSingleObjectiveBoundedMeanSquaredErrorEvaluator() : base() { }
public override bool Maximization { get { return false; } }
public override IOperation Apply() {
IEnumerable rows = GenerateRowsToEvaluate();
var solution = SymbolicExpressionTreeParameter.ActualValue;
double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows);
QualityParameter.ActualValue = new DoubleValue(quality);
AddEvaluatedNodes(solution.Length * rows.Count());
return base.Apply();
}
public static double Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IClassificationProblemData problemData, IEnumerable rows) {
IEnumerable estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows);
IEnumerable originalValues = problemData.Dataset.GetEnumeratedVariableValues(problemData.TargetVariable, rows);
IEnumerable boundedEstimationValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
double minClassValue = problemData.ClassValues.OrderBy(x => x).First();
double maxClassValue = problemData.ClassValues.OrderBy(x => x).Last();
IEnumerator originalEnumerator = originalValues.GetEnumerator();
IEnumerator estimatedEnumerator = estimatedValues.GetEnumerator();
OnlineMeanSquaredErrorEvaluator mseEvaluator = new OnlineMeanSquaredErrorEvaluator();
double errorSum = 0.0;
int n = 0;
// always move forward both enumerators (do not use short-circuit evaluation!)
while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
double estimated = estimatedEnumerator.Current;
double original = originalEnumerator.Current;
double error = estimated - original;
if (estimated < minClassValue || estimated > maxClassValue)
errorSum += Math.Abs(error);
else
errorSum += Math.Pow(error, 2);
n++;
}
// check if both enumerators are at the end to make sure both enumerations have the same length
if (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext()) {
throw new ArgumentException("Number of elements in first and second enumeration doesn't match.");
} else {
return errorSum / n;
}
}
public override double Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IClassificationProblemData problemData, IEnumerable rows) {
SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
EstimationLimitsParameter.ExecutionContext = context;
EvaluatedNodesParameter.ExecutionContext = context;
double mse = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows);
AddEvaluatedNodes(tree.Length * rows.Count());
SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
EstimationLimitsParameter.ExecutionContext = null;
EvaluatedNodesParameter.ExecutionContext = null;
return mse;
}
}
}