[4197] | 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 HeuristicLab.Core;
|
---|
| 23 | using HeuristicLab.Data;
|
---|
| 24 | using System;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using System.Linq;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
| 29 | using System.Collections.Generic;
|
---|
| 30 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 31 | using HeuristicLab.Problems.DataAnalysis.Regression.Symbolic;
|
---|
| 32 | using HeuristicLab.Problems.DataAnalysis.MultiVariate.Regression.Symbolic.Interfaces;
|
---|
| 33 | using HeuristicLab.Parameters;
|
---|
| 34 |
|
---|
| 35 |
|
---|
| 36 | namespace HeuristicLab.Problems.DataAnalysis.MultiVariate.Regression.Symbolic.Evaluators {
|
---|
| 37 | [Item("PartialDerivativeEvaluator", "Evaluator for implict equation modelling")]
|
---|
| 38 | [StorableClass]
|
---|
| 39 | public class PartialDerivativeEvaluator : SingleObjectiveSymbolicVectorRegressionEvaluator {
|
---|
| 40 |
|
---|
| 41 | public PartialDerivativeEvaluator(bool deserializing) : base(deserializing) { }
|
---|
| 42 | public PartialDerivativeEvaluator()
|
---|
| 43 | : base() {
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | public override double Evaluate(SymbolicExpressionTree tree, ISymbolicExpressionTreeInterpreter interpreter, MultiVariateDataAnalysisProblemData problemData, IEnumerable<string> targetVariables, IEnumerable<int> rows, DoubleArray lowerEstimationBound, DoubleArray upperEstimationBound) {
|
---|
| 47 |
|
---|
| 48 | Dataset dataset = problemData.Dataset;
|
---|
| 49 | IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows);
|
---|
| 50 |
|
---|
| 51 | var sortedNames = targetVariables.OrderBy(x => x);
|
---|
| 52 | var pairs = from v1 in sortedNames
|
---|
| 53 | from v2 in sortedNames.Skip(1)
|
---|
| 54 | where v1.CompareTo(v2) < 0
|
---|
| 55 | select new { x = v1, y = v2 };
|
---|
| 56 |
|
---|
| 57 | double meanErrorSum = 0;
|
---|
| 58 | foreach (var pair in pairs) {
|
---|
| 59 | double errorSum = 0;
|
---|
| 60 | string variableX = pair.x;
|
---|
| 61 | string variableY = pair.y;
|
---|
| 62 |
|
---|
| 63 | var dFdX = new SymbolicExpressionTree(PartialSymbolicDifferential.Apply((SymbolicExpressionTreeNode)tree.Root.Clone(), variableX, variableY));
|
---|
| 64 | IEnumerable<double> estimatedDfDx = interpreter.GetSymbolicExpressionTreeValues(dFdX, dataset, rows).ToList();
|
---|
| 65 |
|
---|
| 66 | var dFdY = new SymbolicExpressionTree(PartialSymbolicDifferential.Apply((SymbolicExpressionTreeNode)tree.Root.Clone(), variableY, variableX));
|
---|
| 67 | IEnumerable<double> estimatedDfDy = interpreter.GetSymbolicExpressionTreeValues(dFdY, dataset, rows).ToList();
|
---|
| 68 |
|
---|
| 69 | List<int> rowsList = rows.ToList();
|
---|
| 70 | int n = rowsList.Count;
|
---|
| 71 | int x = dataset.GetVariableIndex(variableX);
|
---|
| 72 | int y = dataset.GetVariableIndex(variableY);
|
---|
| 73 |
|
---|
| 74 | var estimatedDfDxEnumerator = estimatedDfDx.GetEnumerator();
|
---|
| 75 | var estimatedDfDyEnumerator = estimatedDfDy.GetEnumerator();
|
---|
| 76 | var rowsEnumerator = rows.GetEnumerator();
|
---|
| 77 |
|
---|
| 78 | // skip 1
|
---|
| 79 | estimatedDfDxEnumerator.MoveNext();
|
---|
| 80 | estimatedDfDyEnumerator.MoveNext();
|
---|
| 81 | rowsEnumerator.MoveNext();
|
---|
| 82 |
|
---|
| 83 | for (int i = 1; i < n - 1; i++) {
|
---|
| 84 | // evaluate next
|
---|
| 85 | estimatedDfDxEnumerator.MoveNext();
|
---|
| 86 | estimatedDfDyEnumerator.MoveNext();
|
---|
| 87 | rowsEnumerator.MoveNext();
|
---|
| 88 |
|
---|
| 89 | double dFdXValue = estimatedDfDxEnumerator.Current;
|
---|
| 90 | double dFdYValue = estimatedDfDyEnumerator.Current;
|
---|
| 91 | double dXdY = GetLocalDifferential(dataset, rowsEnumerator.Current, x, y);
|
---|
| 92 |
|
---|
| 93 | if ((dFdXValue.IsAlmost(0.0) && dFdYValue.IsAlmost(0.0))) {
|
---|
| 94 | errorSum += Math.Log(1 + Math.Abs(dXdY));
|
---|
| 95 | } else if (dFdXValue.IsAlmost(0.0) ||
|
---|
| 96 | double.IsInfinity(dFdXValue) || double.IsNaN(dFdXValue) ||
|
---|
| 97 | double.IsInfinity(dFdYValue) || double.IsNaN(dFdYValue)) {
|
---|
| 98 | errorSum += 1000000;
|
---|
| 99 | } else {
|
---|
| 100 | double error = dXdY - dFdYValue / dFdXValue;
|
---|
| 101 | errorSum += Math.Log(1 + Math.Abs(error));
|
---|
| 102 | // errorSum += error * error;
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | meanErrorSum += errorSum / n;
|
---|
| 107 | }
|
---|
| 108 | meanErrorSum /= pairs.Count();
|
---|
| 109 |
|
---|
| 110 | return meanErrorSum;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | private double GetLocalDifferential(Dataset dataset, int i, int varX, int varY) {
|
---|
| 114 | return
|
---|
| 115 | (dataset[i + 1, varX] - dataset[i - 1, varX]) /
|
---|
| 116 | (dataset[i + 1, varY] - dataset[i - 1, varY]);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | }
|
---|
| 120 | }
|
---|