#region License Information /* HeuristicLab * Copyright (C) 2002-2010 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 HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Problems.DataAnalysis.Evaluators; using HeuristicLab.Problems.DataAnalysis.Symbolic; namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic { [Item("SymbolicRegressionMeanSquaredErrorEvaluator", "Calculates the mean squared error of a symbolic regression solution.")] [StorableClass] public class SymbolicRegressionMeanSquaredErrorEvaluator : SymbolicRegressionEvaluator { private const string UpperEstimationLimitParameterName = "UpperEstimationLimit"; private const string LowerEstimationLimitParameterName = "LowerEstimationLimit"; #region parameter properties public IValueLookupParameter UpperEstimationLimitParameter { get { return (IValueLookupParameter)Parameters[UpperEstimationLimitParameterName]; } } public IValueLookupParameter LowerEstimationLimitParameter { get { return (IValueLookupParameter)Parameters[LowerEstimationLimitParameterName]; } } #endregion #region properties public DoubleValue UpperEstimationLimit { get { return UpperEstimationLimitParameter.ActualValue; } } public DoubleValue LowerEstimationLimit { get { return LowerEstimationLimitParameter.ActualValue; } } #endregion public SymbolicRegressionMeanSquaredErrorEvaluator() : base() { Parameters.Add(new ValueLookupParameter(UpperEstimationLimitParameterName, "The upper limit that should be used as cut off value for the output values of symbolic expression trees.")); Parameters.Add(new ValueLookupParameter(LowerEstimationLimitParameterName, "The lower limit that should be used as cut off value for the output values of symbolic expression trees.")); } protected override double Evaluate(ISymbolicExpressionTreeInterpreter interpreter, SymbolicExpressionTree solution, Dataset dataset, StringValue targetVariable, IEnumerable rows) { double mse = Calculate(interpreter, solution, LowerEstimationLimit.Value, UpperEstimationLimit.Value, dataset, targetVariable.Value, rows); return mse; } public static double Calculate(ISymbolicExpressionTreeInterpreter interpreter, SymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, Dataset dataset, string targetVariable, IEnumerable rows) { IEnumerable estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, dataset, rows); IEnumerable originalValues = dataset.GetEnumeratedVariableValues(targetVariable, rows); IEnumerator originalEnumerator = originalValues.GetEnumerator(); IEnumerator estimatedEnumerator = estimatedValues.GetEnumerator(); OnlineMeanSquaredErrorEvaluator mseEvaluator = new OnlineMeanSquaredErrorEvaluator(); while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) { double estimated = estimatedEnumerator.Current; double original = originalEnumerator.Current; if (double.IsNaN(estimated)) estimated = upperEstimationLimit; else estimated = Math.Min(upperEstimationLimit, Math.Max(lowerEstimationLimit, estimated)); mseEvaluator.Add(original, estimated); } if (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext()) { throw new ArgumentException("Number of elements in original and estimated enumeration doesn't match."); } else { return mseEvaluator.MeanSquaredError; } } } }