Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisAlleleFrequencyAnalyzer.cs @ 6459

Last change on this file since 6459 was 6135, checked in by gkronber, 14 years ago

#1467 added first implementation of allele frequency analyzer for symbolic data analysis.

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