Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1772_HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SchemaDiversification/SchemaUtil.cs @ 16132

Last change on this file since 16132 was 14427, checked in by bburlacu, 8 years ago

#1772: Extract common methods (used by the schema creator and the schema frequency analyzer) in static SchemaUtil class. Make AnyNode constructor public.

File size: 3.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Encodings.SymbolicExpressionTreeEncoding;
27
28namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
29  public static class SchemaUtil {
30    private static readonly Dictionary<string, string> ShortNames = new Dictionary<string, string> {
31      { "Addition", "+" },
32      { "Subtraction", "-" },
33      { "Multiplication", "*" },
34      { "Division", "/" },
35      { "Logarithm", "Log" },
36      { "Exponential", "Exp" }
37    };
38
39    public static void ReplaceSubtree(ISymbolicExpressionTreeNode original, ISymbolicExpressionTreeNode replacement, bool preserveChildren = true) {
40      var parent = original.Parent;
41      if (parent == null)
42        throw new ArgumentException("Parent cannot be null for node " + original);
43      var index = parent.IndexOfSubtree(original);
44      parent.RemoveSubtree(index);
45      parent.InsertSubtree(index, replacement);
46
47      if (preserveChildren) {
48        var subtrees = original.Subtrees.ToList();
49
50        for (int i = subtrees.Count - 1; i >= 0; --i)
51          original.RemoveSubtree(i);
52
53        for (int i = 0; i < subtrees.Count; ++i) {
54          replacement.AddSubtree(subtrees[i]);
55        }
56      }
57    }
58
59    public static string FormatToString(this ISymbolicExpressionTreeNode node, bool strict = false) {
60      StringBuilder strBuilder = new StringBuilder();
61      // internal nodes or leaf nodes?
62      if (node is AnySubtree)
63        return "# ";
64
65      if (node.SubtreeCount > 0) {
66        strBuilder.Append("(");
67        // symbol on same line as '('
68        string label = string.Empty;
69        if (node is AnyNode)
70          label = "=";
71        else {
72          string shortName;
73          ShortNames.TryGetValue(node.Symbol.Name, out shortName);
74          label = string.IsNullOrEmpty(shortName) ? node.Symbol.Name : shortName;
75        }
76        strBuilder.Append(label + " ");
77        // each subtree expression on a new line
78        // and closing ')' also on new line
79        foreach (var subtree in node.Subtrees) {
80          strBuilder.Append(subtree.FormatToString(strict));
81        }
82        strBuilder.Append(") ");
83      } else {
84        // symbol in the same line with as '(' and ')'
85        var v = node as VariableTreeNode;
86        var c = node as ConstantTreeNode;
87        var w = node as AnyNode; // wildcard
88        string label = string.Empty;
89        if (w != null)
90          label = "=";
91        else if (v != null)
92          label = strict ? string.Format("{0:0.00}_{1}", v.Weight, v.VariableName) : string.Format("{0}", v.VariableName);
93        else if (c != null)
94          label = strict ? string.Format("{0:0.00}", c.Value) : "C";
95        strBuilder.Append(label);
96        if (node.Parent != null && node != node.Parent.Subtrees.Last())
97          strBuilder.Append(" ");
98        //strBuilder.Append(")");
99      }
100      return strBuilder.ToString();
101    }
102  }
103}
Note: See TracBrowser for help on using the repository browser.