Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/TreeComplexityEvaluator.cs @ 17712

Last change on this file since 17712 was 2568, checked in by gkronber, 15 years ago

Implemented #821 (Additional model meta data field that stores number of different function symbols that appear in a symbolic regression model)

File size: 3.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using HeuristicLab.Core;
23using HeuristicLab.Data;
24using HeuristicLab.DataAnalysis;
25using HeuristicLab.GP.Interfaces;
26using System;
27using System.Collections.Generic;
28
29namespace HeuristicLab.GP.StructureIdentification {
30  public class TreeComplexityEvaluator : OperatorBase {
31    public TreeComplexityEvaluator()
32      : base() {
33      AddVariableInfo(new VariableInfo("FunctionTree", "The function tree that should be evaluated", typeof(IGeneticProgrammingModel), VariableKind.In));
34      AddVariableInfo(new VariableInfo("Complexity", "Complexity as weighted sum over all symbols in the tree.", typeof(DoubleData), VariableKind.New | VariableKind.Out));
35    }
36
37    public override IOperation Apply(IScope scope) {
38      IGeneticProgrammingModel model = GetVariableValue<IGeneticProgrammingModel>("FunctionTree", scope, true);
39      IItem complexityItem = GetVariableValue("Complexity", scope, true, false);
40      if (complexityItem == null) {
41        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("Complexity"), new DoubleData()));
42      }
43      GetVariableValue<DoubleData>("Complexity", scope, true).Data = Calculate(model.FunctionTree);
44      return null;
45    }
46
47    public static double Calculate(IFunctionTree tree) {
48      double sum = 0.0;
49      foreach (var t in FunctionTreeIterator.IteratePostfix(tree)) {
50        sum += GetComplexity(t.Function);
51      }
52      return sum;
53    }
54
55
56    private static readonly Dictionary<Type, double> complexity = new Dictionary<Type, double>() {
57      {typeof(Constant), 0.0},   
58
59      {typeof(Variable), 1.0},
60      {typeof(Differential), 1.0},
61 
62      { typeof(Addition), 2.0},
63      { typeof(Average), 2.0},
64      { typeof(Division), 2.0},
65      { typeof(Multiplication), 2.0},
66      { typeof(Subtraction), 2.0},
67      { typeof(And), 2.0},
68      { typeof(Or), 2.0},
69      { typeof(Not), 2.0},
70
71      { typeof(Exponential), 4.0},
72      { typeof(Logarithm), 4.0},
73      { typeof(Power), 4.0},
74      { typeof(Sqrt), 4.0},
75      { typeof(Signum), 4.0},
76      { typeof(LessThan), 4.0},
77      { typeof(GreaterThan), 4.0},
78      { typeof(Equal), 4.0},
79      { typeof(Xor), 4.0},
80      { typeof(IfThenElse), 4.0},
81
82      { typeof(Cosinus), 6.0},
83      { typeof(Sinus), 6.0},
84      { typeof(Tangens), 6.0}
85    };
86    private static double GetComplexity(IFunction f) {
87      return complexity[f.GetType()];
88    }
89  }
90}
Note: See TracBrowser for help on using the repository browser.