Free cookie consent management tool by TermsFeed Policy Generator

Changeset 10752 for branches


Ignore:
Timestamp:
04/15/14 17:08:35 (10 years ago)
Author:
bburlacu
Message:

#1772: Refactored subtree tracing functionality and moved it from the view into a separate static class. Shortened symbol names in the FragmentGraphView so that trees are displayed more nicely.

Location:
branches/HeuristicLab.EvolutionTracking
Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionTreeTile.cs

    r10746 r10752  
    142142
    143143        RectangularPrimitiveBase rectangularPrimitive;
     144        var label = ShortenLabel(node);
    144145        if (node.SubtreeCount == 0) {
    145           rectangularPrimitive = new Rectangle(Chart, lowerLeft, upperRight) { Font = font, Text = visualNode.Content.ToString() };
     146          rectangularPrimitive = new Rectangle(Chart, lowerLeft, upperRight) { Font = font, Text = label };
    146147        } else {
    147           rectangularPrimitive = new Ellipse(Chart, lowerLeft, upperRight) { Font = font, Text = visualNode.Content.ToString() };
     148          rectangularPrimitive = new Ellipse(Chart, lowerLeft, upperRight) { Font = font, Text = label };
    148149        }
    149150
     
    202203      base.Draw(graphics);
    203204    }
     205
     206    private string ShortenLabel(ISymbolicExpressionTreeNode node) {
     207      var term = node as SymbolicExpressionTreeTerminalNode;
     208      if (term != null) {
     209        var parts = term.ToString().Split(' '); // split by space
     210        if (parts.Length == 1) {
     211          return parts[0].Substring(0, 5);
     212        } else {
     213          return parts[0].Substring(0, 5) + parts[1];
     214        }
     215      }
     216      return node.Symbol.ToString().Substring(0, 3);
     217    }
    204218  }
    205219}
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/Tracking/FragmentGraphView.cs

    r10746 r10752  
    9393        var aPos = aTile.Position;
    9494
    95         // mark swapped fragment
    96         if (node.Content.Index != 0) {
     95        if (node.Content.Index > 0) {
    9796          var subtree = node.Content.Root.NodeAt(node.Content.Index);
    9897          foreach (var s in subtree.IterateNodesPrefix()) {
     
    102101              if (rpb != null) {
    103102                rpb.Pen = new Pen(Color.RoyalBlue);
    104               }
    105             }
    106           }
    107         }
    108 
    109         // mark the subtree that was replaced by the fragment
    110         if (node.Children.Any() && node.Content.Index != 0) {
    111           var child = node.Children.First();
    112           {
    113             var subtree = child.Content.Root.NodeAt(node.Content.Index);
    114             var tile = tileDictionary[child].Tile;
    115             foreach (var s in subtree.IterateNodesPrefix()) {
    116               var primitive = tile.GetPrimitive(s);
    117               if (primitive != null) {
    118                 var rpb = primitive as RectangularPrimitiveBase;
    119                 if (rpb != null) {
    120                   rpb.Pen = new Pen(Color.DarkOrange);
    121                 }
    122103              }
    123104            }
     
    133114          symbolicExpressionChartControl.Add(line);
    134115
    135 
     116          if (child == node.Children.First()) {
     117            if (node.Content.Index > 0) {
     118              var subtree = child.Content.Root.NodeAt(node.Content.Index);
     119              foreach (var s in subtree.IterateNodesPrefix()) {
     120                var primitive = bTile.GetPrimitive(s);
     121                if (primitive != null) {
     122                  var rpb = primitive as RectangularPrimitiveBase;
     123                  if (rpb != null) {
     124                    rpb.Pen = new Pen(Color.DarkOrange);
     125                  }
     126                }
     127              }
     128            }
     129          }
    136130        }
    137131      }
     
    173167  }
    174168
     169  internal static class Util {
     170    internal static ISymbolicExpressionTreeNode NodeAt(this ISymbolicExpressionTree tree, int position) {
     171      return NodeAt(tree.Root, position);
     172    }
     173    internal static ISymbolicExpressionTreeNode NodeAt(this ISymbolicExpressionTreeNode root, int position) {
     174      return root.IterateNodesPrefix().ElementAt(position);
     175    }
     176  }
     177
    175178  internal class TileLayoutNode {
    176179    public SymbolicExpressionTreeTile Tile { get; set; }
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/Tracking/SymboldDataAnalysisGenealogyView.cs

    r10746 r10752  
    2020#endregion
    2121
    22 using System;
    2322using System.Collections.Generic;
    2423using System.Drawing;
    25 using System.IO;
    2624using System.Linq;
    2725using System.Windows.Forms;
     
    3331using HeuristicLab.MainForm;
    3432
    35 using FragmentGraph = HeuristicLab.EvolutionTracking.IGenealogyGraph<HeuristicLab.EvolutionTracking.IFragment<HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ISymbolicExpressionTreeNode>>;
    36 using FragmentNode = HeuristicLab.EvolutionTracking.GenealogyGraphNode<HeuristicLab.EvolutionTracking.IFragment<HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ISymbolicExpressionTreeNode>>;
    3733
    3834namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
     
    8480      if (graphNode.InArcs.Any()) {
    8581        var fragment = (IFragment<ISymbolicExpressionTreeNode>)graphNode.InArcs.Last().Data;
    86         treeChart_HighlightSubtree(fragment.Root);
     82        if (fragment != null) {
     83          treeChart_HighlightSubtree(fragment.Root);
     84        }
    8785      }
    8886    }
     
    10098      if (trace) {
    10199        // perform fragment tracing
    102         var fragmentGraph = TraceSubtree(subtree);
     100        var graphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)genealogyGraphChart.SelectedGraphNode;
     101        var subtreeIndex = graphNode.Content.IterateNodesPrefix().ToList().IndexOf(subtree);
     102        var fragmentGraph = SymbolicDataAnalysisExpressionTracing.TraceSubtree(graphNode, subtreeIndex);
    103103        MainFormManager.MainForm.ShowContent(fragmentGraph); // display the fragment graph on the screen
    104104      } else {
     
    159159    }
    160160    #endregion
    161 
    162     #region fragment tracing
    163     private FragmentGraph TraceSubtree(ISymbolicExpressionTreeNode subtree) {
    164       var graphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)genealogyGraphChart.SelectedGraphNode;
    165       var graph = new GenealogyGraph<IFragment<ISymbolicExpressionTreeNode>>();
    166       Trace(graphNode, subtree, graph);
    167       return graph;
    168     }
    169 
    170     private static void Trace(IGenealogyGraphNode<ISymbolicExpressionTree> graphNode, ISymbolicExpressionTreeNode subtree, FragmentGraph fragmentGraph, FragmentNode parentNode = null) {
    171       while (true) {
    172         if (!graphNode.InArcs.Any()) return;
    173 
    174         var parentVertices = graphNode.Parents.ToList();
    175         // the subtree must belong to the currently displayed tree which in turn must belong to the currently selected graph node
    176         var tree = graphNode.Content;
    177         var subtreeIndex = tree.IndexOf(subtree);
    178         var subtreeLength = subtree.GetLength();
    179 
    180         var fragment = (IFragment<ISymbolicExpressionTreeNode>)graphNode.InArcs.Last().Data;
    181         if (fragment == null) return;
    182         var fragmentLength = fragment.Root.GetLength();
    183 
    184         var fragmentNode = new FragmentNode {
    185           Content = new Fragment<ISymbolicExpressionTreeNode> {
    186             Root = subtree,
    187           },
    188           Rank = graphNode.Rank
    189         };
    190 
    191         if (parentNode != null) {
    192           if (parentNode == fragmentNode) {
    193             throw new Exception("Node cannot be a child of itself!");
    194           }
    195           var arc = new GenealogyGraphArc { Source = parentNode, Target = fragmentNode };
    196           parentNode.AddForwardArc(arc);
    197         }
    198         fragmentGraph.AddVertex(fragmentNode);
    199 
    200         parentNode = fragmentNode;
    201 
    202         // if the selected subtree is the actual fragment
    203         if (fragment.Index == subtreeIndex) {
    204           if (fragmentLength != subtreeLength) throw new Exception("Fragment and subtree lengths should be the same!");
    205           graphNode = parentVertices.Last();
    206           tree = graphNode.Content;
    207           subtree = tree.NodeAt(fragment.OldIndex);
    208           continue;
    209         }
    210         // if the fragment contains the selected subtree => track fragment, then track subtree
    211         if (fragment.Index < subtreeIndex && subtreeIndex < fragment.Index + fragmentLength) {
    212           if (subtreeLength >= fragmentLength) throw new Exception("Fragment contains subtree, so subtree length should be less than the fragment length.");
    213 
    214           graphNode = parentVertices.Last();
    215           tree = graphNode.Content;
    216           var i = fragment.Root.IndexOf(subtree); // get the index of the selected subtree, relative to the fragment root
    217           subtree = tree.NodeAt(fragment.OldIndex + i);
    218           continue;
    219         }
    220         // if the selected subtree contains the fragment => track fragment and subtree
    221         if (subtreeIndex < fragment.Index && fragment.Index < subtreeIndex + subtreeLength) {
    222           if (fragmentLength >= subtreeLength) throw new Exception("Subtree contains fragment, so fragment length should be less than the subtree length.");
    223           fragmentNode.Content.Index = fragment.Index - subtreeIndex;
    224           graphNode = parentVertices[0];
    225           // debug check
    226           tree = graphNode.Content;
    227           subtree = tree.NodeAt(subtreeIndex);
    228           if (subtree.GetLength() <= fragmentNode.Content.Index) {
    229             throw new Exception("Index exceeds subtree length.");
    230           }
    231           // track subtree
    232           Trace(graphNode, subtree, fragmentGraph, fragmentNode);
    233 
    234           // track fragment
    235           if (parentVertices.Count > 1) {
    236             // save the index of the current fragment in the tree where it was swapped
    237             graphNode = parentVertices[1];
    238             tree = graphNode.Content;
    239             subtree = tree.NodeAt(fragment.OldIndex);
    240             continue;
    241           }
    242         } else {
    243           // fragment and subtree are completely distinct => we only track the subtree (contained by the root parent)
    244           // if the subtree comes AFTER the fragment in the prefix iteration of nodes,
    245           // then its index is affected by the difference in fragment size between current tree and parent
    246           graphNode = parentVertices[0];
    247           tree = graphNode.Content;
    248           if (subtreeIndex > fragment.Index) {
    249             var diff = tree.NodeAt(fragment.Index).GetLength() - fragmentLength;
    250             subtreeIndex += diff;
    251           }
    252           subtree = tree.NodeAt(subtreeIndex);
    253           continue;
    254         }
    255         break;
    256       }
    257     }
    258     #endregion
    259   }
    260 
    261   internal static class Util {
    262     private static string ViewAsText(this ISymbolicExpressionTreeNode root) {
    263       var writer = new StringWriter();
    264       SymbolicExpressionTreeHierarchicalFormatter.RenderNode(writer, root, string.Empty);
    265       return writer.ToString();
    266     }
    267     #region some helper methods for shortening the tracing code
    268 
    269     internal static void AddChild(this IGenealogyGraphNode parent, IGenealogyGraphNode child) {
    270       parent.AddForwardArc(child);
    271       child.AddReverseArc(parent);
    272       child.Rank = parent.Rank - 1;
    273     }
    274     internal static ISymbolicExpressionTreeNode NodeAt(this ISymbolicExpressionTree tree, int position) {
    275       return NodeAt(tree.Root, position);
    276     }
    277     internal static ISymbolicExpressionTreeNode NodeAt(this ISymbolicExpressionTreeNode root, int position) {
    278       return root.IterateNodesPrefix().ElementAt(position);
    279     }
    280     internal static int IndexOf(this ISymbolicExpressionTree tree, ISymbolicExpressionTreeNode node) {
    281       return IndexOf(tree.Root, node);
    282     }
    283     internal static int IndexOf(this ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode node) {
    284       int i = 0;
    285       foreach (var n in root.IterateNodesPrefix()) {
    286         if (n == node) return i;
    287         ++i;
    288       }
    289       return -1;
    290     }
    291     #endregion
    292161  }
    293162}
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj

    r10650 r10752  
    304304    <Compile Include="Tracking\SymbolicDataAnalysisExpressionAfterManipulatorOperator.cs" />
    305305    <Compile Include="Tracking\SymbolicDataAnalysisExpressionBeforeManipulatorOperator.cs" />
     306    <Compile Include="Tracking\SymbolicDataAnalysisExpressionTracing.cs" />
    306307    <None Include="HeuristicLab.snk" />
    307308    <None Include="Plugin.cs.frame" />
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SymbolicDataAnalysisExpressionAfterCrossoverOperator.cs

    r10677 r10752  
    2525        break;
    2626      }
    27       if (fragment == null) throw new Exception("Could not determine fragment!");
     27      // if the fragment is null then it means crossover did not do anything
     28      //      if (fragment == null) throw new Exception("Could not determine fragment!");
    2829
    2930      arcs[0].Data = null;
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SymbolicDataAnalysisExpressionBeforeCrossoverOperator.cs

    r10677 r10752  
    2929  public class SymbolicDataAnalysisExpressionBeforeCrossoverOperator : BeforeCrossoverOperator<ISymbolicExpressionTree> {
    3030    public override IOperation Apply() {
    31       var result = base.Apply(); // the child will be added to the graph before the crossover
     31      var result = base.Apply(); // the base operator will add the child to the graph before the actual crossover operation takes place
    3232      var parents = ParentsParameter.ActualValue.ToList();
    3333      var childVertex = (IGenealogyGraphNode<ISymbolicExpressionTree>)GenealogyGraph[parents[0]].Last(); // use the parent since it is actually the child before crossover (and the ChildParameter doesn't have a value yet)
     
    3838        arcs[i].Data = nodes;
    3939      }
    40       var parentVertices = childVertex.InArcs.Select(x => (IGenealogyGraphNode<ISymbolicExpressionTree>)x.Source).ToList();
     40      var parentVertices = childVertex.Parents.ToList();
    4141      if (parents[0].Length != parentVertices[0].Content.Length || parents[1].Length != parentVertices[1].Content.Length) {
    4242        throw new Exception("Inconsistency detected in GenealogyGraph.");
Note: See TracChangeset for help on using the changeset viewer.