Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/24/14 23:50:17 (11 years ago)
Author:
bburlacu
Message:

#1772: Some progress on tracking fragments and building blocks, still incomplete.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/SymboldDataAnalysisGenealogyView.cs

    r10650 r10654  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using System.Drawing;
     
    110111        graphChart_highlightMatchingVertices(vertices);
    111112      } else {
    112         // perform matching like before
     113        // perform matching like it was done before
     114        // currently there is no possibility to specify the subtree matching criteria
    113115        var trees = Content.Nodes.Select(x => (ISymbolicExpressionTree)x.Content);
    114116        var matchingTrees = trees.Where(x => x.Root.ContainsSubtree(subtree, comparer));
     
    174176    }
    175177
    176     private void Trace(IGenealogyGraphNode<ISymbolicExpressionTree> graphNode, ISymbolicExpressionTreeNode subtree, FragmentGraph fragmentGraph, FragmentNode fragmentNode = null) {
    177       // add the subtree as a vertex in the fragment graph
    178       var fn = new FragmentNode { Content = new Fragment<ISymbolicExpressionTreeNode> { Root = subtree }, Rank = graphNode.Rank };
    179       fragmentGraph.AddVertex(fn);
    180 
    181       // maybe we reached the end of the genealogy
     178    private void Trace(IGenealogyGraphNode<ISymbolicExpressionTree> graphNode, ISymbolicExpressionTreeNode subtree, FragmentGraph fragmentGraph, FragmentNode parentNode = null) {
    182179      if (graphNode.InArcs == null) return;
    183 
    184180      // the subtree must belong to the currently displayed tree which in turn must belong to the currently selected graph node
    185181      var tree = graphNode.Content;
     
    190186      var fragmentLength = fragment.Root.GetLength();
    191187
    192       if (fragment.Index <= index && index < fragment.Index + fragmentLength) {
    193         // the selected subtree is contained by the fragment
    194         var fn1 = new FragmentNode { Content = fragment };
    195         if (fragmentNode != null) {
    196           AddChild(fragmentNode, fn1);
    197         }
    198         fragmentGraph.AddVertex(fn1);
     188      if (fragment.Index == index) {
     189        // the selected subtree is the actual fragment
     190        if (fragmentLength != length) throw new Exception("Fragment and subtree lengths should be the same!");
     191
     192        var node = new FragmentNode { Content = fragment, Rank = graphNode.Rank };
     193
     194        if (parentNode != null) {
     195          AddChild(parentNode, node);
     196        }
     197
     198        fragmentGraph.AddVertex(node);
     199
    199200        graphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)graphNode.InArcs.Last().Source;
    200201        subtree = graphNode.Content.IterateNodesPrefix().ToList()[fragment.OldIndex];
    201         Trace(graphNode, subtree, fragmentGraph, fn1);
    202       } else if (fragment.Index > index && fragment.Index < index + length) {
     202        Trace(graphNode, subtree, fragmentGraph, node);
     203      } else if (fragment.Index < index && index < fragment.Index + fragmentLength) {
     204        // the fragment contains the selected subtree
     205        if (length >= fragmentLength) throw new Exception("Fragment contains subtree, so subtree length should be less than the fragment length.");
     206
     207        var node = new FragmentNode { Content = fragment, Rank = graphNode.Rank };
     208        if (parentNode != null) {
     209          AddChild(parentNode, node);
     210        }
     211
     212        fragmentGraph.AddVertex(node);
     213
     214        graphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)graphNode.InArcs.Last().Source;
     215        var i = fragment.Root.IterateNodesPrefix().ToList().IndexOf(subtree);
     216        subtree = graphNode.Content.IterateNodesPrefix().ToList()[fragment.OldIndex + i];
     217        Trace(graphNode, subtree, fragmentGraph, node);
     218
     219      } else if (index < fragment.Index && fragment.Index < index + length) {
    203220        // the selected subtree contains the fragment
    204         // first trace the fragment, then trace the subtree
    205         graphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)graphNode.InArcs.Last().Source;
    206         subtree = graphNode.Content.IterateNodesPrefix().ToList()[fragment.OldIndex];
    207 
    208         var fn0 = new FragmentNode { Content = new Fragment<ISymbolicExpressionTreeNode> { Root = subtree } };
    209         var fn1 = new FragmentNode { Content = fragment };
    210 
    211         if (fragmentNode != null) {
    212           AddChild(fragmentNode, fn0);
    213           AddChild(fragmentNode, fn1);
    214         }
    215         fragmentGraph.AddVertex(fn0);
    216         fragmentGraph.AddVertex(fn1);
     221        if (fragmentLength >= length) throw new Exception("Subtree contains fragment, so fragment length should be less than the subtree length.");
     222
     223        var node = new FragmentNode { Content = new Fragment<ISymbolicExpressionTreeNode> { Root = subtree }, Rank = graphNode.Rank };
     224
     225        if (parentNode != null) {
     226          AddChild(parentNode, node);
     227        }
     228        fragmentGraph.AddVertex(node);
    217229
    218230        var graphNode0 = (IGenealogyGraphNode<ISymbolicExpressionTree>)graphNode.InArcs[0].Source;
    219231        var subtree0 = graphNode0.Content.IterateNodesPrefix().ToList()[index];
    220         Trace(graphNode0, subtree0, fragmentGraph, fn0);
    221 
    222         var graphNode1 = (IGenealogyGraphNode<ISymbolicExpressionTree>)graphNode.InArcs.Last().Source;
    223         var subtree1 = graphNode1.Content.IterateNodesPrefix().ToList()[fragment.OldIndex];
    224         Trace(graphNode1, subtree1, fragmentGraph, fn1);
    225       } else {
    226         // the fragment is not contained by the selected subtree, so we go one level up
    227         graphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)graphNode.InArcs[0].Source;
    228         subtree = graphNode.Content.IterateNodesPrefix().ToList()[index];
    229         fragmentGraph.Clear(); // restart the trace one level up
    230         Trace(graphNode, subtree, fragmentGraph);
     232        Trace(graphNode0, subtree0, fragmentGraph, node);
     233
     234        if (graphNode.InArcs.Count > 1) {
     235          var graphNode1 = (IGenealogyGraphNode<ISymbolicExpressionTree>)graphNode.InArcs[1].Source;
     236          var subtree1 = graphNode1.Content.IterateNodesPrefix().ToList()[fragment.OldIndex];
     237          Trace(graphNode1, subtree1, fragmentGraph, node);
     238        }
    231239      }
    232240    }
Note: See TracChangeset for help on using the changeset viewer.