Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis.ComplexityAnalyzer/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisModelComplexityAnalyzer.cs @ 12849

Last change on this file since 12849 was 12849, checked in by mkommend, 9 years ago

#2175: Updated license year to 2015.

File size: 4.8 KB
RevLine 
[10751]1#region License Information
2
3/* HeuristicLab
[12849]4 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[10751]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 class SymbolicDataAnalysisModelComplexityAnalyzer {
29    public static double CalculateComplexity(ISymbolicExpressionTreeNode node) {
30      switch (OpCodes.MapSymbolToOpCode(node)) {
[11310]31        case OpCodes.Constant: {
32            return 1;
[10751]33          }
[11310]34        case OpCodes.Variable: {
35            return 2;
[10751]36          }
[11310]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;
[10751]43          }
[11310]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++) {
[11861]54              var nodeComplexity = CalculateComplexity(node.GetSubtree(i));
[12547]55              complexity *= nodeComplexity + 1;
[11310]56            }
[11883]57            return complexity;
[11310]58          }
59        case OpCodes.Div: {
60            double complexity = 1;
61            for (int i = 0; i < node.SubtreeCount; i++) {
[11861]62              var nodeComplexity = CalculateComplexity(node.GetSubtree(i));
[12547]63              complexity *= nodeComplexity +1;
[11310]64            }
[11883]65            return complexity;
[11310]66          }
67        case OpCodes.Sin: {
68            double complexity = CalculateComplexity(node.GetSubtree(0));
[12187]69            return Math.Pow(2.0, complexity);
[11310]70          }
71        case OpCodes.Cos: {
72            double complexity = CalculateComplexity(node.GetSubtree(0));
[11883]73            return Math.Pow(2.0, complexity);
[11310]74          }
75        case OpCodes.Tan: {
76            double complexity = CalculateComplexity(node.GetSubtree(0));
[12547]77            return Math.Pow(2.0, complexity);
[11310]78          }
79        case OpCodes.Exp: {
80            double complexity = CalculateComplexity(node.GetSubtree(0));
[11883]81            return Math.Pow(2.0, complexity);
82          }
[11310]83        case OpCodes.Log: {
84            double complexity = CalculateComplexity(node.GetSubtree(0));
[11883]85            return Math.Pow(2.0, complexity);
[11310]86          }
87        case OpCodes.Square: {
88            double complexity = CalculateComplexity(node.GetSubtree(0));
[11861]89            return complexity * complexity;
[11310]90          }
91        case OpCodes.SquareRoot: {
92            double complexity = CalculateComplexity(node.GetSubtree(0));
[11861]93            return complexity * complexity * complexity;
[11310]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
[10751]122        default:
123          throw new NotSupportedException();
124      }
125    }
126  }
127}
Note: See TracBrowser for help on using the repository browser.