Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/VariableFrequencyBasedImpactCalculator.cs

Last change on this file was 2683, checked in by gkronber, 15 years ago

Implemented operator to calculate variable impacts based on the log of variable frequencies over the whole GP run and integrated operators into default SGP and OSGP algorithms. #853 (Operator to calculate variable impacts as integral over the relative frequencies of variable references over the whole GP run and the whole population)

File size: 4.4 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.Text;
25using System.Xml;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.DataAnalysis;
29using System.Linq;
30using HeuristicLab.GP.Interfaces;
31using HeuristicLab.Modeling;
32
33namespace HeuristicLab.GP.StructureIdentification {
34  public class VariableFrequencyBasedImpactCalculator : OperatorBase {
35
36    public VariableFrequencyBasedImpactCalculator()
37      : base() {
38      AddVariableInfo(new VariableInfo("VariableFrequency", "List of variable frequencies over all iterations", typeof(ItemList<ItemList>), VariableKind.In));
39      AddVariableInfo(new VariableInfo("RelativeFrequencyVariableImpact", "Variable impacts", typeof(ItemList), VariableKind.New | VariableKind.Out));
40    }
41
42    public override IOperation Apply(IScope scope) {
43      ItemList<ItemList> variableFrequencies = GetVariableValue<ItemList<ItemList>>("VariableFrequency", scope, true, false);
44      if (variableFrequencies != null) {
45        var inputVariables = from x in variableFrequencies[0]
46                             select ((StringData)x).Data;
47
48        var frequencies = from x in variableFrequencies.Skip(1)
49                          let row = from v in x
50                                    select ((DoubleData)v).Data
51                          select row;
52        Dictionary<string, double> qualityImpacts = Calculate(inputVariables, frequencies);
53
54        ItemList varImpacts = GetVariableValue<ItemList>("RelativeFrequencyVariableImpact", scope, true, false);
55        if (varImpacts == null) {
56          varImpacts = new ItemList();
57          scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("RelativeFrequencyVariableImpact"), varImpacts));
58        }
59
60        varImpacts.Clear();
61        foreach (KeyValuePair<string, double> p in qualityImpacts) {
62          ItemList row = new ItemList();
63          row.Add(new StringData(p.Key));
64          row.Add(new DoubleData(p.Value));
65          varImpacts.Add(row);
66        }
67
68      }
69      return null;
70    }
71
72    private static Dictionary<string, double> Calculate(
73      IEnumerable<string> inputvariables,
74      IEnumerable<IEnumerable<double>> frequencies) {
75      int nVariables = inputvariables.Count();
76      int iterations = frequencies.Count();
77      // allocate lists of frequencies for each variable
78      List<double>[] frequencyTrajectory = new List<double>[nVariables];
79      for (int i = 0; i < nVariables; i++) {
80        frequencyTrajectory[i] = new List<double>();
81      }
82      foreach (var iterationFrequency in frequencies) {
83        int varIndex = 0;
84        if (iterationFrequency.Count() != frequencyTrajectory.Length) throw new ArgumentException();
85        foreach (var varIterationFrequency in iterationFrequency) {
86          frequencyTrajectory[varIndex++].Add(varIterationFrequency);
87        }
88      }
89
90      double[] impacts = new double[nVariables];
91
92      // calculate impacts as integral over the whole run
93      for (int variableIndex = 0; variableIndex < nVariables; variableIndex++) {
94        double baseLine = frequencyTrajectory[variableIndex][0];
95        for (int i = 0; i < iterations; i++) {
96          impacts[variableIndex] += (frequencyTrajectory[variableIndex][i] - baseLine);
97        }
98        impacts[variableIndex] /= iterations - 1;
99      }
100
101      Dictionary<string, double> result = new Dictionary<string, double>();
102      int j = 0;
103      foreach (string variableName in inputvariables) {
104        result[variableName] = impacts[j++];
105      }
106      return result;
107    }
108  }
109}
Note: See TracBrowser for help on using the repository browser.