1 | #region License Information
|
---|
2 |
|
---|
3 | /* HeuristicLab
|
---|
4 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
5 | *
|
---|
6 | * This file is part of HeuristicLab.
|
---|
7 | *
|
---|
8 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
9 | * it under the terms of the GNU General Public License as published by
|
---|
10 | * the Free Software Foundation, either version 3 of the License, or
|
---|
11 | * (at your option) any later version.
|
---|
12 | *
|
---|
13 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
16 | * GNU General Public License for more details.
|
---|
17 | *
|
---|
18 | * You should have received a copy of the GNU General Public License
|
---|
19 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #endregion
|
---|
23 |
|
---|
24 | using System;
|
---|
25 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
28 | public class SymbolicDataAnalysisModelComplexityAnalyzer {
|
---|
29 | public static double CalculateComplexity(ISymbolicExpressionTreeNode node) {
|
---|
30 | switch (OpCodes.MapSymbolToOpCode(node)) {
|
---|
31 | case OpCodes.Constant: {
|
---|
32 | return 1;
|
---|
33 | }
|
---|
34 | case OpCodes.Variable: {
|
---|
35 | return 2;
|
---|
36 | }
|
---|
37 | case OpCodes.Add: {
|
---|
38 | double complexity = 0;
|
---|
39 | for (int i = 0; i < node.SubtreeCount; i++) {
|
---|
40 | complexity += CalculateComplexity(node.GetSubtree(i));
|
---|
41 | }
|
---|
42 | return complexity;
|
---|
43 | }
|
---|
44 | case OpCodes.Sub: {
|
---|
45 | double complexity = 0;
|
---|
46 | for (int i = 0; i < node.SubtreeCount; i++) {
|
---|
47 | complexity += CalculateComplexity(node.GetSubtree(i));
|
---|
48 | }
|
---|
49 | return complexity;
|
---|
50 | }
|
---|
51 | case OpCodes.Mul: {
|
---|
52 | double complexity = 1;
|
---|
53 | for (int i = 0; i < node.SubtreeCount; i++) {
|
---|
54 | var nodeComplexity = CalculateComplexity(node.GetSubtree(i));
|
---|
55 | complexity *= nodeComplexity > 2 ? nodeComplexity : 2;
|
---|
56 | }
|
---|
57 | return complexity;
|
---|
58 | }
|
---|
59 | case OpCodes.Div: {
|
---|
60 | double complexity = 1;
|
---|
61 | for (int i = 0; i < node.SubtreeCount; i++) {
|
---|
62 | var nodeComplexity = CalculateComplexity(node.GetSubtree(i));
|
---|
63 | complexity *= nodeComplexity > 2 ? nodeComplexity : 2;
|
---|
64 | }
|
---|
65 | return complexity;
|
---|
66 | }
|
---|
67 | case OpCodes.Sin: {
|
---|
68 | double complexity = CalculateComplexity(node.GetSubtree(0));
|
---|
69 | return Math.Pow(2.0, complexity);
|
---|
70 | }
|
---|
71 | case OpCodes.Cos: {
|
---|
72 | double complexity = CalculateComplexity(node.GetSubtree(0));
|
---|
73 | return Math.Pow(2.0, complexity);
|
---|
74 | }
|
---|
75 | case OpCodes.Tan: {
|
---|
76 | double complexity = CalculateComplexity(node.GetSubtree(0));
|
---|
77 | return Math.Pow(2.0, complexity); ;
|
---|
78 | }
|
---|
79 | case OpCodes.Exp: {
|
---|
80 | double complexity = CalculateComplexity(node.GetSubtree(0));
|
---|
81 | return Math.Pow(2.0, complexity);
|
---|
82 | }
|
---|
83 | case OpCodes.Log: {
|
---|
84 | double complexity = CalculateComplexity(node.GetSubtree(0));
|
---|
85 | return Math.Pow(2.0, complexity);
|
---|
86 | }
|
---|
87 | case OpCodes.Square: {
|
---|
88 | double complexity = CalculateComplexity(node.GetSubtree(0));
|
---|
89 | return complexity * complexity;
|
---|
90 | }
|
---|
91 | case OpCodes.SquareRoot: {
|
---|
92 | double complexity = CalculateComplexity(node.GetSubtree(0));
|
---|
93 | return complexity * complexity * complexity;
|
---|
94 | }
|
---|
95 | case OpCodes.Power: {
|
---|
96 | double complexity = CalculateComplexity(node.GetSubtree(0));
|
---|
97 | var exponentNode = node.GetSubtree(1) as ConstantTreeNode;
|
---|
98 | if (exponentNode != null) {
|
---|
99 | double exponent = exponentNode.Value;
|
---|
100 | if (exponent < 0) exponent = Math.Abs(exponent);
|
---|
101 | if (exponent < 1) exponent = 1 / exponent;
|
---|
102 | return Math.Pow(complexity, Math.Round(exponent));
|
---|
103 | }
|
---|
104 |
|
---|
105 | double exponentComplexity = CalculateComplexity(node.GetSubtree(1));
|
---|
106 | return Math.Pow(complexity, 2 * exponentComplexity);
|
---|
107 | }
|
---|
108 | case OpCodes.Root: {
|
---|
109 | double complexity = CalculateComplexity(node.GetSubtree(0));
|
---|
110 | var rootNode = node.GetSubtree(1) as ConstantTreeNode;
|
---|
111 | if (rootNode != null) {
|
---|
112 | double root = rootNode.Value;
|
---|
113 | if (root < 0) root = Math.Abs(root);
|
---|
114 | if (root < 1) root = 1 / root;
|
---|
115 | return Math.Pow(complexity, Math.Round(root));
|
---|
116 | }
|
---|
117 |
|
---|
118 | double rootComplexity = CalculateComplexity(node.GetSubtree(1));
|
---|
119 | return Math.Pow(complexity, 2 * rootComplexity);
|
---|
120 | }
|
---|
121 |
|
---|
122 | default:
|
---|
123 | throw new NotSupportedException();
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|
127 | }
|
---|