Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11290 was 10751, checked in by mkommend, 11 years ago

#2175: Added solution for complexity analysis.

File size: 2.2 KB
Line 
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
24using System;
25using System.Linq;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27
28namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
29  public class SymbolicDataAnalysisModelComplexityAnalyzer {
30    public static double CalculateComplexity(ISymbolicExpressionTreeNode node) {
31      if (node is ConstantTreeNode) return 0;
32      if (node is VariableTreeNode) return 1;
33
34      var subtreeComplexities = node.Subtrees.Select(CalculateComplexity);
35
36      double result = 0.0;
37      switch (OpCodes.MapSymbolToOpCode(node)) {
38        case OpCodes.Add:
39        case OpCodes.Sub:
40          foreach (var comp in subtreeComplexities) {
41            result += comp;
42          }
43          break;
44        case OpCodes.Mul:
45          result = 1;
46          foreach (var comp in subtreeComplexities) {
47            result *= comp + 1;
48          }
49          break;
50        //return subtreeComplexities.Aggregate((a, b) => a == 0 ? b : b == 0 ? a : (a + 1) * (b + 1));
51        case OpCodes.Div:
52          result = 1;
53          foreach (var comp in subtreeComplexities) {
54            result *= comp + 1;
55          }
56          result *= 2;
57          break;
58        //return subtreeComplexities.Aggregate((a, b) => a == 0 ? b * 2 : b == 0 ? a : (a + 1) * (b + 1) * 2);
59        default:
60          throw new NotSupportedException();
61      }
62      return result;
63    }
64  }
65}
Note: See TracBrowser for help on using the repository browser.