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