Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/MultiObjective/SymbolicClassificationMultiObjectivePearsonRSquaredTreeSizeEvaluator.cs @ 5851

Last change on this file since 5851 was 5851, checked in by gkronber, 13 years ago

#1411 added evaluated nodes parameter to symbolic data analysis evaluators.

File size: 3.4 KB
Line 
1using System;
2using System.Linq;
3using System.Collections.Generic;
4using HeuristicLab.Common;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
8using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
9
10namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification {
11  [Item("Pearson R² & Tree size Evaluator", "Calculates the Pearson R² and the tree size of a symbolic classification solution.")]
12  [StorableClass]
13  public class SymbolicClassificationMultiObjectivePearsonRSquaredTreeSizeEvaluator : SymbolicClassificationMultiObjectiveEvaluator {
14    [StorableConstructor]
15    protected SymbolicClassificationMultiObjectivePearsonRSquaredTreeSizeEvaluator(bool deserializing) : base(deserializing) { }
16    protected SymbolicClassificationMultiObjectivePearsonRSquaredTreeSizeEvaluator(SymbolicClassificationMultiObjectivePearsonRSquaredTreeSizeEvaluator original, Cloner cloner)
17      : base(original, cloner) {
18    }
19    public override IDeepCloneable Clone(Cloner cloner) {
20      return new SymbolicClassificationMultiObjectivePearsonRSquaredTreeSizeEvaluator(this, cloner);
21    }
22
23    public SymbolicClassificationMultiObjectivePearsonRSquaredTreeSizeEvaluator() : base() { }
24
25    public override IEnumerable<bool> Maximization { get { return new bool[2] { true, false }; } }
26
27    public override IOperation Apply() {
28      IEnumerable<int> rows = GenerateRowsToEvaluate();
29      var solution = SymbolicExpressionTreeParameter.ActualValue;
30      double[] qualities = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows);
31      QualitiesParameter.ActualValue = new DoubleArray(qualities);
32      AddEvaluatedNodes(solution.Length * rows.Count());
33      return base.Apply();
34    }
35
36    public static double[] Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IClassificationProblemData problemData, IEnumerable<int> rows) {
37      IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows);
38      IEnumerable<double> originalValues = problemData.Dataset.GetEnumeratedVariableValues(problemData.TargetVariable, rows);
39      double r2 = OnlinePearsonsRSquaredEvaluator.Calculate(estimatedValues, originalValues);
40      return new double[] { double.IsNaN(r2) ? 0.0 : r2, solution.Length };
41    }
42
43    public override double[] Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IClassificationProblemData problemData, IEnumerable<int> rows) {
44      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
45      EstimationLimitsParameter.ExecutionContext = context;
46      EvaluatedNodesParameter.ExecutionContext = context;
47
48      double[] quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows);
49      AddEvaluatedNodes(tree.Length * rows.Count());
50     
51      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
52      EstimationLimitsParameter.ExecutionContext = null;
53      EvaluatedNodesParameter.ExecutionContext = null;
54
55      return quality;
56    }
57  }
58}
Note: See TracBrowser for help on using the repository browser.