Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisAlleleFrequencyAnalyzer.cs @ 11247

Last change on this file since 11247 was 7972, checked in by gkronber, 13 years ago

#1722 added static methods in OnePointShaker and SymbolicDataAnalysisAlleleFrequencyAnalyzer

File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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 System.Text;
25using HeuristicLab.Analysis;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
34  [Item("SymbolicDataAnalysisAlleleFrequencyAnalyzer", "")]
35  [StorableClass]
36  public sealed class SymbolicDataAnalysisAlleleFrequencyAnalyzer : AlleleFrequencyAnalyzer<ISymbolicExpressionTree>, ISymbolicDataAnalysisAnalyzer {
37    private const string AlleleTreeDepthParameterName = "AlleleTreeDepth";
38
39    #region parameter properties
40    public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
41      get { return SolutionParameter; }
42    }
43
44    public ILookupParameter<Optimization.ResultCollection> ResultCollectionParameter {
45      get { return ResultsParameter; }
46    }
47
48    public IFixedValueParameter<IntValue> AlleleTreeDepthParameter {
49      get { return (IFixedValueParameter<IntValue>)Parameters[AlleleTreeDepthParameterName]; }
50    }
51    #endregion
52    #region properties
53    public int AlleleTreeDepth {
54      get { return AlleleTreeDepthParameter.Value.Value; }
55      set { AlleleTreeDepthParameter.Value.Value = value; }
56    }
57    #endregion
58
59    [StorableConstructor]
60    private SymbolicDataAnalysisAlleleFrequencyAnalyzer(bool deserializing) : base(deserializing) { }
61    private SymbolicDataAnalysisAlleleFrequencyAnalyzer(SymbolicDataAnalysisAlleleFrequencyAnalyzer original, Cloner cloner) : base(original, cloner) { }
62    public SymbolicDataAnalysisAlleleFrequencyAnalyzer()
63      : base() {
64      Parameters.Add(new FixedValueParameter<IntValue>(AlleleTreeDepthParameterName, "The depth of subtrees that should be considered as allele", new IntValue(2)));
65    }
66
67    public override IDeepCloneable Clone(Cloner cloner) {
68      return new SymbolicDataAnalysisAlleleFrequencyAnalyzer(this, cloner);
69    }
70
71    protected override Allele[] CalculateAlleles(ISymbolicExpressionTree solution) {
72      return CalculateAlleles(solution, AlleleTreeDepth);
73    }
74
75    public static Allele[] CalculateAlleles(ISymbolicExpressionTree solution, int alleleTreedepth) {
76      return GetAllSubtreesOfDepth(solution, alleleTreedepth)
77        .AsParallel()
78        .Select(t => GetAlleleFromSubtreeOfDepth(t, alleleTreedepth))
79        .ToArray();
80    }
81
82    private static Allele GetAlleleFromSubtreeOfDepth(ISymbolicExpressionTreeNode tree, int d) {
83      string textualRepresentation = GetTextualRepresentationFromSubtreeOfDepth(tree, d);
84      return new Allele(textualRepresentation);
85    }
86
87    private static string GetTextualRepresentationFromSubtreeOfDepth(ISymbolicExpressionTreeNode tree, int d) {
88      if (d == 0) return "";
89      StringBuilder builder = new StringBuilder();
90      var varTreeNode = tree as VariableTreeNode;
91      var constTreeNode = tree as ConstantTreeNode;
92      if (varTreeNode != null) {
93        builder.Append("(var " + varTreeNode.VariableName);
94      } else if (constTreeNode != null) {
95        builder.Append("(const");
96      } else {
97        builder.Append("(" + tree.ToString());
98      }
99      for (int i = 0; i < tree.SubtreeCount; i++) {
100        builder.Append(" " + GetTextualRepresentationFromSubtreeOfDepth(tree.GetSubtree(i), d - 1));
101      }
102      builder.Append(")");
103      return builder.ToString();
104    }
105
106    private static IEnumerable<ISymbolicExpressionTreeNode> GetAllSubtreesOfDepth(ISymbolicExpressionTree solution, int d) {
107      return from node in solution.IterateNodesPostfix()
108             where node.GetDepth() >= d
109             select node;
110    }
111  }
112}
Note: See TracBrowser for help on using the repository browser.