Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2988_ModelsOfModels2/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisModelComplexityCalculator.cs @ 17300

Last change on this file since 17300 was 16899, checked in by msemenki, 6 years ago

#2988: New version of class structure.

File size: 3.6 KB
RevLine 
[10751]1#region License Information
2
3/* HeuristicLab
[16565]4 * Copyright (C) 2002-2019 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 {
[13220]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
[16899]37      switch (OpCode.MapSymbolToOpCode(node)) {
38        case OpCode.Constant: {
[11310]39            return 1;
[10751]40          }
[16899]41        case OpCode.Variable:
42        case OpCode.BinaryFactorVariable:
43        case OpCode.FactorVariable: {
[11310]44            return 2;
[10751]45          }
[16899]46        case OpCode.Add:
47        case OpCode.Sub: {
[11310]48            double complexity = 0;
49            for (int i = 0; i < node.SubtreeCount; i++) {
50              complexity += CalculateComplexity(node.GetSubtree(i));
51            }
52            return complexity;
53          }
[16899]54        case OpCode.Mul:
55        case OpCode.Div: {
[11310]56            double complexity = 1;
57            for (int i = 0; i < node.SubtreeCount; i++) {
[11861]58              var nodeComplexity = CalculateComplexity(node.GetSubtree(i));
[13220]59              complexity *= nodeComplexity + 1;
[11310]60            }
[11883]61            return complexity;
[11310]62          }
[16899]63        case OpCode.Sin:
64        case OpCode.Cos:
65        case OpCode.Tan:
66        case OpCode.Exp:
67        case OpCode.Log: {
[11310]68            double complexity = CalculateComplexity(node.GetSubtree(0));
[11883]69            return Math.Pow(2.0, complexity);
[11310]70          }
[16899]71        case OpCode.Square: {
[11310]72            double complexity = CalculateComplexity(node.GetSubtree(0));
[11861]73            return complexity * complexity;
[11310]74          }
[16899]75        case OpCode.SquareRoot: {
[11310]76            double complexity = CalculateComplexity(node.GetSubtree(0));
[11861]77            return complexity * complexity * complexity;
[11310]78          }
[16899]79        case OpCode.Power:
80        case OpCode.Root: {
[11310]81            double complexity = CalculateComplexity(node.GetSubtree(0));
[13300]82            var exponent = node.GetSubtree(1) as ConstantTreeNode;
83            if (exponent != null) {
84              double expVal = exponent.Value;
85              if (expVal < 0) expVal = Math.Abs(expVal);
86              if (expVal < 1) expVal = 1 / expVal;
87              return Math.Pow(complexity, Math.Round(expVal));
[11310]88            }
89
[13300]90            double expComplexity = CalculateComplexity(node.GetSubtree(1));
91            return Math.Pow(complexity, 2 * expComplexity);
[11310]92          }
93
[10751]94        default:
95          throw new NotSupportedException();
96      }
97    }
98  }
99}
Note: See TracBrowser for help on using the repository browser.