Ignore:
Timestamp:
07/24/14 16:57:38 (8 years ago)
Author:
bburlacu
Message:

#2215: Added checks for debugging purposes in the BottomUpSimilarityCalculator and refactored the SymbolicDataAnalysisInternalDiversityAnalyzer.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.BottomUpTreeDistance/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SimilarityCalculators/BottomUpSimilarityCalculator.cs

    r11221 r11224  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Drawing;
     25using System.Globalization;
    2426using System.Linq;
     27using System.Text;
    2528using HeuristicLab.Common;
    2629using HeuristicLab.Core;
    2730using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     31using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views;
    2832using HeuristicLab.Optimization.Operators;
    2933using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    5256        throw new ArgumentException("Cannot calculate similarity when one of the arguments is null.");
    5357
    54       return CalculateSolutionSimilarity(t1, t2);
     58      var similarity = CalculateSolutionSimilarity(t1, t2);
     59      if (similarity > 1.0)
     60        throw new Exception("Similarity value cannot be greater than 1");
     61
     62      return similarity;
    5563    }
    5664
     
    6977      var reverseMap = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>(); // nodes of t2 => nodes of t1
    7078
    71       foreach (var v in n1.IterateNodesBreadth()) {
     79      foreach (var v in IterateBreadthOrdered(n1)) {
    7280        if (forwardMap.ContainsKey(v)) continue;
    7381        var kv = compactedGraph[v];
    7482        ISymbolicExpressionTreeNode w = null;
    75         foreach (var t in n2.IterateNodesBreadth()) {
     83        foreach (var t in IterateBreadthOrdered(n2)) {
    7684          if (reverseMap.ContainsKey(t) || compactedGraph[t] != kv) continue;
    7785          w = t;
     
    8896          var s = eV.Current;
    8997          var t = eW.Current;
     98          if (reverseMap.ContainsKey(t))
     99            throw new Exception("Mapping already present");
    90100          forwardMap[s] = t;
    91101          reverseMap[t] = s;
     
    201211      return arc;
    202212    }
     213
     214    // debugging
     215    private static string FormatMapping(ISymbolicExpressionTree t1, ISymbolicExpressionTree t2, Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> map) {
     216      var symbolNameMap = new Dictionary<string, string>
     217    {
     218      {"ProgramRootSymbol", "Prog"},
     219      {"StartSymbol","RPB"},
     220      {"Multiplication", "$\\times$"},
     221      {"Division", "$\\div$"},
     222      {"Addition", "$+$"},
     223      {"Subtraction", "$-$"},
     224      {"Exponential", "$\\exp$"},
     225      {"Logarithm", "$\\log$"}
     226    };
     227
     228      var sb = new StringBuilder();
     229      var nodeIds = new Dictionary<ISymbolicExpressionTreeNode, string>();
     230      int offset = 0;
     231      var layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode>(x => x.Subtrees);
     232      var nodeCoordinates = layoutEngine.CalculateLayout(t1.Root).ToDictionary(n => n.Content, n => new PointF(n.X, n.Y));
     233
     234      double ws = 0.5;
     235      double hs = 0.5;
     236
     237      var nl = Environment.NewLine;
     238      sb.Append("\\documentclass[class=minimal,border=0pt]{standalone}" + nl +
     239                 "\\usepackage{tikz}" + nl +
     240                 "\\begin{document}" + nl +
     241                 "\\begin{tikzpicture}" + nl +
     242                 "\\def\\ws{1}" + nl +
     243                 "\\def\\hs{0.7}" + nl +
     244                 "\\def\\offs{" + offset + "}" + nl);
     245
     246      foreach (var node in t1.IterateNodesBreadth()) {
     247        var id = Guid.NewGuid().ToString();
     248        nodeIds[node] = id;
     249        var coord = nodeCoordinates[node];
     250        var nodeName = symbolNameMap.ContainsKey(node.Symbol.Name) ? symbolNameMap[node.Symbol.Name] : node.ToString();
     251        sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "\\node ({0}) at (\\ws*{1} + \\offs,\\hs*{2}) {{{3}}};", nodeIds[node], ws * coord.X, -hs * coord.Y, EscapeLatexString(nodeName)));
     252      }
     253
     254      foreach (ISymbolicExpressionTreeNode t in t1.IterateNodesBreadth()) {
     255        var n = t;
     256        foreach (var s in t.Subtrees) {
     257          sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "\\draw ({0}) -- ({1});", nodeIds[n], nodeIds[s]));
     258        }
     259      }
     260
     261      nodeCoordinates = layoutEngine.CalculateLayout(t2.Root).ToDictionary(n => n.Content, n => new PointF(n.X, n.Y));
     262
     263      offset = 20;
     264      sb.Append("\\def\\offs{" + offset + "}" + nl);
     265      foreach (var node in t2.IterateNodesBreadth()) {
     266        var id = Guid.NewGuid().ToString();
     267        nodeIds[node] = id;
     268        var coord = nodeCoordinates[node];
     269        var nodeName = symbolNameMap.ContainsKey(node.Symbol.Name) ? symbolNameMap[node.Symbol.Name] : node.ToString();
     270        sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "\\node ({0}) at (\\ws*{1} + \\offs,\\hs*{2}) {{{3}}};", nodeIds[node], ws * coord.X, -hs * coord.Y, EscapeLatexString(nodeName)));
     271      }
     272
     273      foreach (ISymbolicExpressionTreeNode t in t2.IterateNodesBreadth()) {
     274        var n = t;
     275        foreach (var s in t.Subtrees) {
     276          sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "\\draw ({0}) -- ({1});", nodeIds[n], nodeIds[s]));
     277        }
     278      }
     279
     280      foreach (var p in map) {
     281        var id1 = nodeIds[p.Key];
     282        var id2 = nodeIds[p.Value];
     283
     284        sb.Append(string.Format(CultureInfo.InvariantCulture, "\\path[draw,->,color=gray] ({0}) edge[bend left,dashed] ({1});" + Environment.NewLine, id1, id2));
     285      }
     286      sb.Append("\\end{tikzpicture}" + nl +
     287                "\\end{document}" + nl);
     288      return sb.ToString();
     289    }
     290
     291    private static string EscapeLatexString(string s) {
     292      return s.Replace("\\", "\\\\").Replace("{", "\\{").Replace("}", "\\}").Replace("_", "\\_");
     293    }
    203294  }
    204295}
Note: See TracChangeset for help on using the changeset viewer.