Free cookie consent management tool by TermsFeed Policy Generator

Changeset 10654 for branches


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.

Location:
branches/HeuristicLab.EvolutionTracking
Files:
3 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    }
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SymbolicDataAnalysisExpressionAfterCrossoverOperator.cs

    r10650 r10654  
    99  public class SymbolicDataAnalysisExpressionAfterCrossoverOperator : AfterCrossoverOperator<ISymbolicExpressionTree> {
    1010    public override IOperation Apply() {
    11       var childVertex = GenealogyGraph[ChildParameter.ActualValue].Last();
     11      var child = ChildParameter.ActualValue;
     12      var childVertex = GenealogyGraph[child].Last();
    1213      var arc0 = (IGenealogyGraphArc)childVertex.InArcs[0];
    1314      var arc1 = (IGenealogyGraphArc)childVertex.InArcs[1];
    1415      var nodes0 = (List<ISymbolicExpressionTreeNode>)arc0.Data;
    1516      var nodes1 = (List<ISymbolicExpressionTreeNode>)arc1.Data;
    16       var childNodes = ChildParameter.ActualValue.IterateNodesBreadth().ToList();
     17      var childNodes = child.IterateNodesPrefix().ToList();
    1718      IFragment<ISymbolicExpressionTreeNode> fragment = null;
    18 
    1919      for (int i = 0; i < Math.Min(nodes0.Count, childNodes.Count); ++i) {
    2020        if (nodes0[i] == childNodes[i]) continue;
    2121        fragment = new Fragment<ISymbolicExpressionTreeNode> {
    2222          Root = childNodes[i],
    23           Index = i
     23          Index = i,
    2424        };
     25        fragment.OldIndex = nodes1.IndexOf(fragment.Root);
     26        break;
    2527      }
    2628      if (fragment == null) throw new Exception("Could not determine fragment!");
    27 
    28       for (int i = 0; i < nodes1.Count; ++i) {
    29         if (nodes1[i] != fragment.Root) continue;
    30         fragment.OldIndex = i;
    31       }
    3229
    3330      arc0.Data = null;
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SymbolicDataAnalysisExpressionBeforeCrossoverOperator.cs

    r10459 r10654  
    77  public class SymbolicDataAnalysisExpressionBeforeCrossoverOperator : BeforeCrossoverOperator<ISymbolicExpressionTree> {
    88    public override IOperation Apply() {
    9       var result = base.Apply();
     9      var result = base.Apply(); // the child will be added to the graph before the crossover
    1010      var parents = ParentsParameter.ActualValue.ToList();
    1111      var childVertex = GenealogyGraph[parents[0]].Last(); // use the parent since it is actually the child before crossover (and the ChildParameter doesn't have a value yet)
    1212      //      var parentVertices = ExecutionContext.Scope.SubScopes.Select(s => CurrentGeneration[int.Parse(s.Name)]).ToList();
    1313
     14      // we add the breadth list of nodes of each parent as the data element of the arc from the parent to the child
    1415      for (int i = 0; i < parents.Count; ++i) {
    15         var nodes = parents[i].IterateNodesBreadth().ToList();
     16        var nodes = parents[i].IterateNodesPrefix().ToList();
    1617        var arc = (IGenealogyGraphArc)childVertex.InArcs[i];
    1718        arc.Data = nodes;
Note: See TracChangeset for help on using the changeset viewer.