[6135] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14186] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6135] | 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 |
|
---|
[6503] | 22 | using System.Collections.Generic;
|
---|
[6135] | 23 | using System.Linq;
|
---|
[6503] | 24 | using System.Text;
|
---|
[6135] | 25 | using HeuristicLab.Analysis;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Data;
|
---|
[6503] | 29 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[6135] | 30 | using HeuristicLab.Parameters;
|
---|
| 31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 32 |
|
---|
| 33 | namespace 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 |
|
---|
[6503] | 48 | public IFixedValueParameter<IntValue> AlleleTreeDepthParameter {
|
---|
[6135] | 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) {
|
---|
[7972] | 72 | return CalculateAlleles(solution, AlleleTreeDepth);
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | public static Allele[] CalculateAlleles(ISymbolicExpressionTree solution, int alleleTreedepth) {
|
---|
| 76 | return GetAllSubtreesOfDepth(solution, alleleTreedepth)
|
---|
| 77 | .Select(t => GetAlleleFromSubtreeOfDepth(t, alleleTreedepth))
|
---|
[6728] | 78 | .ToArray();
|
---|
[6135] | 79 | }
|
---|
| 80 |
|
---|
[7972] | 81 | private static Allele GetAlleleFromSubtreeOfDepth(ISymbolicExpressionTreeNode tree, int d) {
|
---|
[6135] | 82 | string textualRepresentation = GetTextualRepresentationFromSubtreeOfDepth(tree, d);
|
---|
| 83 | return new Allele(textualRepresentation);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[7972] | 86 | private static string GetTextualRepresentationFromSubtreeOfDepth(ISymbolicExpressionTreeNode tree, int d) {
|
---|
[6135] | 87 | if (d == 0) return "";
|
---|
| 88 | StringBuilder builder = new StringBuilder();
|
---|
[7305] | 89 | var varTreeNode = tree as VariableTreeNode;
|
---|
| 90 | var constTreeNode = tree as ConstantTreeNode;
|
---|
[15131] | 91 | var factorVarTreeNode = tree as FactorVariableTreeNode;
|
---|
| 92 | var binFactorVarTreeNode = tree as BinaryFactorVariableTreeNode;
|
---|
[7305] | 93 | if (varTreeNode != null) {
|
---|
| 94 | builder.Append("(var " + varTreeNode.VariableName);
|
---|
[15131] | 95 | } else if (factorVarTreeNode != null) {
|
---|
| 96 | builder.Append("(factor " + factorVarTreeNode.VariableName);
|
---|
| 97 | } else if (binFactorVarTreeNode != null) {
|
---|
| 98 | builder.Append("(factor " + binFactorVarTreeNode.VariableName + "=" + binFactorVarTreeNode.VariableValue);
|
---|
[7305] | 99 | } else if (constTreeNode != null) {
|
---|
| 100 | builder.Append("(const");
|
---|
| 101 | } else {
|
---|
| 102 | builder.Append("(" + tree.ToString());
|
---|
| 103 | }
|
---|
[6803] | 104 | for (int i = 0; i < tree.SubtreeCount; i++) {
|
---|
[6135] | 105 | builder.Append(" " + GetTextualRepresentationFromSubtreeOfDepth(tree.GetSubtree(i), d - 1));
|
---|
| 106 | }
|
---|
| 107 | builder.Append(")");
|
---|
| 108 | return builder.ToString();
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[7972] | 111 | private static IEnumerable<ISymbolicExpressionTreeNode> GetAllSubtreesOfDepth(ISymbolicExpressionTree solution, int d) {
|
---|
[6135] | 112 | return from node in solution.IterateNodesPostfix()
|
---|
| 113 | where node.GetDepth() >= d
|
---|
| 114 | select node;
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 | }
|
---|