Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisModelComplexityCalculator.cs @ 18214

Last change on this file since 18214 was 18132, checked in by gkronber, 3 years ago

#3140: merged r18091:18131 from branch to trunk

File size: 3.6 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 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
24using System;
25using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
26
27namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
28  public static class SymbolicDataAnalysisModelComplexityCalculator {
29    public static double CalculateComplexity(ISymbolicExpressionTree tree) {
30      return CalculateComplexity(tree.Root);
31    }
32    public static double CalculateComplexity(ISymbolicExpressionTreeNode treeNode) {
33      var node = treeNode;
34      if (node.Symbol is ProgramRootSymbol) node = node.GetSubtree(0);
35      if (node.Symbol is StartSymbol) node = node.GetSubtree(0);
36
37      switch (OpCodes.MapSymbolToOpCode(node)) {
38        case OpCodes.Number: // fall through
39        case OpCodes.Constant: {
40            return 1;
41          }
42        case OpCodes.Variable:
43        case OpCodes.BinaryFactorVariable:
44        case OpCodes.FactorVariable: {
45            return 2;
46          }
47        case OpCodes.Add:
48        case OpCodes.Sub: {
49            double complexity = 0;
50            for (int i = 0; i < node.SubtreeCount; i++) {
51              complexity += CalculateComplexity(node.GetSubtree(i));
52            }
53            return complexity;
54          }
55        case OpCodes.Mul:
56        case OpCodes.Div: {
57            double complexity = 1;
58            for (int i = 0; i < node.SubtreeCount; i++) {
59              var nodeComplexity = CalculateComplexity(node.GetSubtree(i));
60              complexity *= nodeComplexity + 1;
61            }
62            return complexity;
63          }
64        case OpCodes.Sin:
65        case OpCodes.Cos:
66        case OpCodes.Tan:
67        case OpCodes.Exp:
68        case OpCodes.Log: {
69            double complexity = CalculateComplexity(node.GetSubtree(0));
70            return Math.Pow(2.0, complexity);
71          }
72        case OpCodes.Square: {
73            double complexity = CalculateComplexity(node.GetSubtree(0));
74            return complexity * complexity;
75          }
76        case OpCodes.SquareRoot: {
77            double complexity = CalculateComplexity(node.GetSubtree(0));
78            return complexity * complexity * complexity;
79          }
80        case OpCodes.Power:
81        case OpCodes.Root: {
82            double complexity = CalculateComplexity(node.GetSubtree(0));
83            var exponent = node.GetSubtree(1) as INumericTreeNode;
84            if (exponent != null) {
85              double expVal = exponent.Value;
86              if (expVal < 0) expVal = Math.Abs(expVal);
87              if (expVal < 1) expVal = 1 / expVal;
88              return Math.Pow(complexity, Math.Round(expVal));
89            }
90
91            double expComplexity = CalculateComplexity(node.GetSubtree(1));
92            return Math.Pow(complexity, 2 * expComplexity);
93          }
94
95        default:
96          throw new NotSupportedException();
97      }
98    }
99  }
100}
Note: See TracBrowser for help on using the repository browser.