Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP/Logging/TreeArityAnalyser.cs @ 1235

Last change on this file since 1235 was 698, checked in by gkronber, 16 years ago

implemented #325

File size: 4.5 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 System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Core;
27using HeuristicLab.Operators;
28using HeuristicLab.Random;
29using HeuristicLab.Data;
30using HeuristicLab.Constraints;
31using System.Diagnostics;
32
33namespace HeuristicLab.GP {
34  /// <summary>
35  /// Creates a histogram of the arities of functions used in the whole GP population.
36  /// </summary>
37  public class TreeArityAnalyser : OperatorBase {
38    public override string Description {
39      get {
40        return @"Creates a histogram of the arities of functions used in the whole GP population.";
41      }
42    }
43    public TreeArityAnalyser()
44      : base() {
45      AddVariableInfo(new VariableInfo("FunctionTree", "The tree to mutate", typeof(IFunctionTree), VariableKind.In));
46      AddVariableInfo(new VariableInfo("Histogram", "The histogram of arities over all functions in all functiontrees", typeof(ItemList<ItemList<IntData>>), VariableKind.New | VariableKind.Out));
47    }
48
49    public override IOperation Apply(IScope scope) {
50      ItemList<ItemList<IntData>> histogram = GetVariableValue<ItemList<ItemList<IntData>>>("Histogram", scope, false, false);
51      if(histogram == null) {
52        histogram = new ItemList<ItemList<IntData>>();
53        IVariableInfo info = GetVariableInfo("Histogram");
54        if(info.Local)
55          AddVariable(new HeuristicLab.Core.Variable(info.ActualName, histogram));
56        else
57          scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(info.FormalName), histogram));
58      } else {
59        histogram.Clear();
60      }
61
62      foreach(IScope subScope in scope.SubScopes) {
63        ItemList<ItemList<IntData>> subHistogram = GetArityHistogram(subScope);
64        MergeHistogram(subHistogram, histogram);
65      }
66
67      return null;
68    }
69
70    private ItemList<ItemList<IntData>> GetArityHistogram(IScope subScope) {
71      IFunctionTree tree = GetVariableValue<IFunctionTree>("FunctionTree", subScope, false);
72      List<int> arities = new List<int>();
73      Arities(tree, arities);
74      var histogram = arities.GroupBy(x => x, (g, xs) => new { Group = g, Count = xs.Count() }).OrderBy(g=>g.Group); // count number of distinct arities
75      ItemList<ItemList<IntData>> result = new ItemList<ItemList<IntData>>();
76     
77      // translate enumerable to itemlist
78      histogram.Aggregate(result, (r, g) => {
79        ItemList<IntData> entry = new ItemList<IntData>();
80        entry.Add(new IntData(g.Group));
81        entry.Add(new IntData(g.Count));
82        r.Add(entry); return r;
83      });
84      return result;
85    }
86
87    private void Arities(IFunctionTree tree, List<int> arities) {
88      arities.Add(tree.SubTrees.Count);
89      foreach(IFunctionTree subTree in tree.SubTrees) {
90        Arities(subTree, arities);
91      }
92    }
93
94    private void MergeHistogram(ItemList<ItemList<IntData>> srcHistogram, ItemList<ItemList<IntData>> dstHistogram) {
95      int j = 0;
96      for(int i = 0; i < srcHistogram.Count; i++) {
97        int g = srcHistogram[i][0].Data;
98        int c = srcHistogram[i][1].Data;
99        int dstG, dstC;
100        if(j < dstHistogram.Count) {
101          dstG = dstHistogram[j][0].Data;
102          dstC = dstHistogram[j][1].Data;
103        } else {
104          dstG = int.MaxValue;
105          dstC = 0;
106        }
107        if(g < dstG) {
108          // new group before the current group
109          if(j < dstHistogram.Count)
110            dstHistogram.Insert(j, srcHistogram[i]);
111          else
112            dstHistogram.Add(srcHistogram[i]);
113        } else if(g == dstG) {
114          dstHistogram[j][1] = new IntData(c + dstC);
115        }
116        j++;
117      }
118    }
119  }
120}
Note: See TracBrowser for help on using the repository browser.