1 | using System.Collections.Generic;
|
---|
2 | using System.Linq;
|
---|
3 | using HeuristicLab.Common;
|
---|
4 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
5 |
|
---|
6 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification {
|
---|
7 | public class SymbolicDiscriminantFunctionClassificationSolutionImpactValuesCalculator : SymbolicDataAnalysisSolutionImpactValuesCalculator {
|
---|
8 | public override Dictionary<ISymbolicExpressionTreeNode, double> CalculateReplacementValues(ISymbolicExpressionTree tree,
|
---|
9 | ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
|
---|
10 | IDataAnalysisProblemData problemData) {
|
---|
11 | var replacementValues = new Dictionary<ISymbolicExpressionTreeNode, double>();
|
---|
12 | foreach (ISymbolicExpressionTreeNode node in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()) {
|
---|
13 | replacementValues[node] = CalculateReplacementValue(node, tree, interpreter, problemData);
|
---|
14 | }
|
---|
15 | return replacementValues;
|
---|
16 | }
|
---|
17 | public override Dictionary<ISymbolicExpressionTreeNode, double> CalculateImpactValues(ISymbolicExpressionTree tree,
|
---|
18 | ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
|
---|
19 | IDataAnalysisProblemData classificationProblemData,
|
---|
20 | double lowerEstimationLimit, double upperEstimationLimit) {
|
---|
21 | var problemData = (IClassificationProblemData)classificationProblemData;
|
---|
22 | var dataset = problemData.Dataset;
|
---|
23 | var rows = problemData.TrainingIndices;
|
---|
24 | string targetVariable = problemData.TargetVariable;
|
---|
25 | Dictionary<ISymbolicExpressionTreeNode, double> impactValues = new Dictionary<ISymbolicExpressionTreeNode, double>();
|
---|
26 | List<ISymbolicExpressionTreeNode> nodes = tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPostfix().ToList();
|
---|
27 |
|
---|
28 | var targetClassValues = dataset.GetDoubleValues(targetVariable, rows);
|
---|
29 | var originalOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows)
|
---|
30 | .LimitToRange(lowerEstimationLimit, upperEstimationLimit)
|
---|
31 | .ToArray();
|
---|
32 | OnlineCalculatorError errorState;
|
---|
33 | double originalGini = NormalizedGiniCalculator.Calculate(targetClassValues, originalOutput, out errorState);
|
---|
34 | if (errorState != OnlineCalculatorError.None) originalGini = 0.0;
|
---|
35 |
|
---|
36 | foreach (ISymbolicExpressionTreeNode node in nodes) {
|
---|
37 | var parent = node.Parent;
|
---|
38 | var constantNode = ((ConstantTreeNode)new Constant().CreateTreeNode());
|
---|
39 | constantNode.Value = CalculateReplacementValue(node, tree, interpreter, classificationProblemData);
|
---|
40 | ISymbolicExpressionTreeNode replacementNode = constantNode;
|
---|
41 | SwitchNode(parent, node, replacementNode);
|
---|
42 | var newOutput = interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows)
|
---|
43 | .LimitToRange(lowerEstimationLimit, upperEstimationLimit)
|
---|
44 | .ToArray();
|
---|
45 | double newGini = NormalizedGiniCalculator.Calculate(targetClassValues, newOutput, out errorState);
|
---|
46 | if (errorState != OnlineCalculatorError.None) newGini = 0.0;
|
---|
47 |
|
---|
48 | // impact = 0 if no change
|
---|
49 | // impact < 0 if new solution is better
|
---|
50 | // impact > 0 if new solution is worse
|
---|
51 | impactValues[node] = originalGini - newGini;
|
---|
52 | SwitchNode(parent, replacementNode, node);
|
---|
53 | }
|
---|
54 | return impactValues;
|
---|
55 |
|
---|
56 | }
|
---|
57 | }
|
---|
58 | }
|
---|