Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.GP.StructureIdentification/3.3/VariableNamesExtractor.cs @ 10166

Last change on this file since 10166 was 2319, checked in by gkronber, 15 years ago

Applied patch from mkommend for variable impact calculators and adapted data-modeling algorithms to use the new operators for variable impact calculation. #728

File size: 3.1 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.Data;
28using HeuristicLab.GP.Interfaces;
29
30namespace HeuristicLab.GP.StructureIdentification {
31  public class VariableNamesExtractor : OperatorBase {
32    public VariableNamesExtractor()
33      : base() {
34      AddVariableInfo(new VariableInfo("FunctionTree", "The function tree from which the used variables should be extracted", typeof(IGeneticProgrammingModel), VariableKind.In));
35      AddVariableInfo(new VariableInfo("VariableNames", "Extracted variable names from model", typeof(ItemList<StringData>), VariableKind.New | VariableKind.Out));
36    }
37
38    public override string Description {
39      get { return "Extracts the variable names used in the given function tree."; }
40    }
41
42    public override IOperation Apply(IScope scope) {
43      IGeneticProgrammingModel model = GetVariableValue<IGeneticProgrammingModel>("FunctionTree", scope, true);
44      ItemList<StringData> data = GetVariableValue<ItemList<StringData>>("VariableNames", scope, true, false);
45      if (data == null) {
46        data = new ItemList<StringData>();
47        IVariableInfo info = GetVariableInfo("VariableNames");
48        if (info.Local)
49          AddVariable(new HeuristicLab.Core.Variable(info.ActualName, data));
50        else
51          scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(info.FormalName), data));
52      }
53      data.Clear();
54
55      foreach (string s in ExtractVariableNames(model.FunctionTree)) {
56        data.Add(new StringData(s));
57      }
58
59      return null;
60    }
61
62    public static IEnumerable<string> ExtractVariableNames(IFunctionTree functionTree) {
63      List<string> names = new List<string>();
64      Extract(functionTree, names);
65      return names;
66    }
67
68    private static void Extract(IFunctionTree functionTree, List<string> variableNames) {
69      if (functionTree is VariableFunctionTree) {
70        VariableFunctionTree v = (VariableFunctionTree)functionTree;
71        if (!variableNames.Contains(v.VariableName))
72          variableNames.Add(v.VariableName);
73      }
74
75      foreach (IFunctionTree child in functionTree.SubTrees) {
76        Extract(child, variableNames);
77      }
78    }
79  }
80}
Note: See TracBrowser for help on using the repository browser.