Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.Operators/3.3/Logging/SymbolFrequencyAnalyser.cs @ 3446

Last change on this file since 3446 was 2682, checked in by gkronber, 15 years ago

Implemented operators to count relative frequencies of function-symbols and variable-symbols in over the whole population.
#5 (Operators to analyse structural diversity in GP populations)

File size: 4.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 System.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.GP.Interfaces;
27using System;
28
29namespace HeuristicLab.GP.Operators {
30  /// <summary>
31  /// Creates accumulated frequencies of function-symbols over the whole population.
32  /// </summary>
33  public class SymbolFrequencyAnalyser : OperatorBase {
34    public override string Description {
35      get {
36        return @"Creates accumulated frequencies of function-symbols over the whole population.";
37      }
38    }
39    public SymbolFrequencyAnalyser()
40      : base() {
41      AddVariableInfo(new VariableInfo("FunctionLibrary", "The function library", typeof(FunctionLibrary), VariableKind.In));
42      AddVariableInfo(new VariableInfo("FunctionTree", "The tree to analyse", typeof(IGeneticProgrammingModel), VariableKind.In));
43      AddVariableInfo(new VariableInfo("SymbolFrequency", "The accumulated symbol-frequencies over the whole population.", typeof(ItemList<ItemList>), VariableKind.New | VariableKind.Out));
44    }
45
46    public override IOperation Apply(IScope scope) {
47      ItemList<ItemList> frequenciesList = GetVariableValue<ItemList<ItemList>>("SymbolFrequency", scope, false, false);
48      FunctionLibrary funLib = GetVariableValue<FunctionLibrary>("FunctionLibrary", scope, true);
49      if (frequenciesList == null) {
50        frequenciesList = new ItemList<ItemList>();
51        // first line should contain a list of all functions
52        ItemList funList = new ItemList();
53        foreach (var fun in funLib.Functions) {
54          funList.Add(fun);
55        }
56        frequenciesList.Add(funList);
57        IVariableInfo info = GetVariableInfo("SymbolFrequency");
58        if (info.Local)
59          AddVariable(new HeuristicLab.Core.Variable(info.ActualName, frequenciesList));
60        else
61          scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(info.FormalName), frequenciesList));
62      }
63      double[] frequencySum = new double[funLib.Functions.Count()];
64      int treeSizeSum = 0;
65      foreach (var subScope in scope.SubScopes) {
66        IGeneticProgrammingModel gpModel = GetVariableValue<IGeneticProgrammingModel>("FunctionTree", subScope, false);
67        var subScopeFrequencies = GetFrequencies(gpModel.FunctionTree, funLib);
68        if (subScopeFrequencies.Count() != frequencySum.Length) throw new InvalidProgramException();
69        int i = 0;
70        foreach (var freq in subScopeFrequencies) {
71          frequencySum[i++] += freq;
72        }
73        treeSizeSum += gpModel.Size;
74      }
75      ItemList freqList = new ItemList();
76      for (int i = 0; i < frequencySum.Length; i++) {
77        freqList.Add(new DoubleData(frequencySum[i] / treeSizeSum));
78      }
79      frequenciesList.Add(freqList);
80      return null;
81    }
82
83    private static IEnumerable<double> GetFrequencies(IFunctionTree tree, FunctionLibrary funLib) {
84      var groupedFuns = (from node in FunctionTreeIterator.IteratePostfix(tree)
85                         select node.Function).GroupBy(x => x);
86
87      foreach (var fun in funLib.Functions) {
88        var matchingFuns = from g in groupedFuns
89                           where g.Key == fun
90                           select g.Count();
91        if (matchingFuns.Count() == 0) yield return 0.0;
92        else {
93          yield return matchingFuns.Single(); // / (double)gpModel.Size;
94        }
95      }
96    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.