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 | complexity *= CalculateComplexity(node.GetSubtree(i));
|
---|
55 | }
|
---|
56 | return complexity == 1 ? node.SubtreeCount : complexity;
|
---|
57 | }
|
---|
58 | case OpCodes.Div: {
|
---|
59 | double complexity = 1;
|
---|
60 | for (int i = 0; i < node.SubtreeCount; i++) {
|
---|
61 | complexity *= CalculateComplexity(node.GetSubtree(i));
|
---|
62 | }
|
---|
63 | return complexity;
|
---|
64 | }
|
---|
65 | case OpCodes.Sin: {
|
---|
66 | double complexity = CalculateComplexity(node.GetSubtree(0));
|
---|
67 | return complexity * 10;
|
---|
68 | }
|
---|
69 | case OpCodes.Cos: {
|
---|
70 | double complexity = CalculateComplexity(node.GetSubtree(0));
|
---|
71 | return complexity * 10;
|
---|
72 | }
|
---|
73 | case OpCodes.Tan: {
|
---|
74 | double complexity = CalculateComplexity(node.GetSubtree(0));
|
---|
75 | return complexity * 10;
|
---|
76 | }
|
---|
77 | case OpCodes.Exp: {
|
---|
78 | double complexity = CalculateComplexity(node.GetSubtree(0));
|
---|
79 | return complexity * 10;
|
---|
80 | }
|
---|
81 | case OpCodes.Log: {
|
---|
82 | double complexity = CalculateComplexity(node.GetSubtree(0));
|
---|
83 | return complexity * 10;
|
---|
84 | }
|
---|
85 | case OpCodes.Square: {
|
---|
86 | double complexity = CalculateComplexity(node.GetSubtree(0));
|
---|
87 | return 2 * complexity;
|
---|
88 | }
|
---|
89 | case OpCodes.SquareRoot: {
|
---|
90 | double complexity = CalculateComplexity(node.GetSubtree(0));
|
---|
91 | return 10 * complexity;
|
---|
92 | }
|
---|
93 | case OpCodes.Power: {
|
---|
94 | double complexity = CalculateComplexity(node.GetSubtree(0));
|
---|
95 | var exponentNode = node.GetSubtree(1) as ConstantTreeNode;
|
---|
96 | if (exponentNode != null) {
|
---|
97 | double exponent = exponentNode.Value;
|
---|
98 | if (exponent < 0) exponent = Math.Abs(exponent);
|
---|
99 | if (exponent < 1) exponent = 1 / exponent;
|
---|
100 | return Math.Pow(complexity, Math.Round(exponent));
|
---|
101 | }
|
---|
102 |
|
---|
103 | double exponentComplexity = CalculateComplexity(node.GetSubtree(1));
|
---|
104 | return Math.Pow(complexity, 2 * exponentComplexity);
|
---|
105 | }
|
---|
106 | case OpCodes.Root: {
|
---|
107 | double complexity = CalculateComplexity(node.GetSubtree(0));
|
---|
108 | var rootNode = node.GetSubtree(1) as ConstantTreeNode;
|
---|
109 | if (rootNode != null) {
|
---|
110 | double root = rootNode.Value;
|
---|
111 | if (root < 0) root = Math.Abs(root);
|
---|
112 | if (root < 1) root = 1 / root;
|
---|
113 | return Math.Pow(complexity, Math.Round(root));
|
---|
114 | }
|
---|
115 |
|
---|
116 | double rootComplexity = CalculateComplexity(node.GetSubtree(1));
|
---|
117 | return Math.Pow(complexity, 2 * rootComplexity);
|
---|
118 | }
|
---|
119 |
|
---|
120 | default:
|
---|
121 | throw new NotSupportedException();
|
---|
122 | }
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|