Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/VariableFrequencyAnalyser.cs @ 3155

Last change on this file since 3155 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.7 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.StructureIdentification {
30  /// <summary>
31  /// Creates accumulated frequencies of variable-symbols over the whole population.
32  /// </summary>
33  public class VariableFrequencyAnalyser : OperatorBase {
34    public override string Description {
35      get {
36        return @"Creates accumulated frequencies of variable-symbols over the whole population.";
37      }
38    }
39    public VariableFrequencyAnalyser()
40      : base() {
41      AddVariableInfo(new VariableInfo("InputVariables", "The input variables", typeof(ItemList), VariableKind.In));
42      AddVariableInfo(new VariableInfo("FunctionTree", "The tree to analyse", typeof(IGeneticProgrammingModel), VariableKind.In));
43      AddVariableInfo(new VariableInfo("VariableFrequency", "The accumulated variable-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>>("VariableFrequency", scope, false, false);
48      ItemList inputVariables = GetVariableValue<ItemList>("InputVariables", scope, true);
49      if (frequenciesList == null) {
50        frequenciesList = new ItemList<ItemList>();
51        // first line should contain a list of variables
52        ItemList varList = new ItemList();
53        foreach (var inputVariable in inputVariables) {
54          varList.Add(inputVariable);
55        }
56        frequenciesList.Add(varList);
57        IVariableInfo info = GetVariableInfo("VariableFrequency");
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[inputVariables.Count()];
64      int variableNodesSum = 0;
65      foreach (var subScope in scope.SubScopes) {
66        IGeneticProgrammingModel gpModel = GetVariableValue<IGeneticProgrammingModel>("FunctionTree", subScope, false);
67        var subScopeFrequencies = GetFrequencies(gpModel.FunctionTree, inputVariables);
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        variableNodesSum += CountVariableNodes(gpModel.FunctionTree);
74      }
75      ItemList freqList = new ItemList();
76      for (int i = 0; i < frequencySum.Length; i++) {
77        freqList.Add(new DoubleData(frequencySum[i] / variableNodesSum));
78      }
79      frequenciesList.Add(freqList);
80      return null;
81    }
82
83    private int CountVariableNodes(IFunctionTree tree) {
84      return (from x in FunctionTreeIterator.IteratePostfix(tree)
85              where x is VariableFunctionTree
86              select 1).Sum();
87    }
88
89    private static IEnumerable<double> GetFrequencies(IFunctionTree tree, ItemList inputVariables) {
90      var groupedFuns = (from node in FunctionTreeIterator.IteratePostfix(tree)
91                         let varNode = node as VariableFunctionTree
92                         where varNode != null
93                         select varNode.VariableName).GroupBy(x => x);
94
95      foreach (var inputVariable in inputVariables.Cast<StringData>()) {
96        var matchingFuns = from g in groupedFuns
97                           where g.Key == inputVariable.Data
98                           select g.Count();
99        if (matchingFuns.Count() == 0) yield return 0.0;
100        else {
101          yield return matchingFuns.Single(); // / (double)gpModel.Size;
102        }
103      }
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.