Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/29/15 23:40:57 (8 years ago)
Author:
bburlacu
Message:

#1772: Some more work on subtree sample counting.

File:
1 edited

Legend:

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

    r12231 r12265  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    8484      nodeWeightLabel.Text = String.Format("{0:0.00}", graphNode.Weight);
    8585
     86      if (!graphNode.InArcs.Any()) return;
     87
    8688      if (openNew_CheckBox.Checked) {
    8789        // get the ancestors into a new view
     
    9698      }
    9799
    98       if (graphNode.InArcs.Any()) {
    99         var data = graphNode.InArcs.Last().Data;
    100         var fragment = data as IFragment<ISymbolicExpressionTreeNode>;
    101         var td = data as TraceData;
    102         //        double max = Content.Vertices.Max(x => x.Data.IterateNodesPrefix().Max(y => y.NodeWeight));
    103 
    104         // calculate max only in the current generation
    105         double max = Content.GetByRank(graphNode.Rank).Max(x => ((IGenealogyGraphNode<ISymbolicExpressionTree>)x).Data.IterateNodesPrefix().Max(y => y.NodeWeight));
    106         if (max.IsAlmost(0)) max = 1;
    107         int c = ColorGradient.Colors.Count - 1;
    108         const int alpha = 100;
    109         if (fragment != null) {
    110           // highlight the fragment with a blue line color
    111           foreach (var n in graphNode.Data.NodeAt(fragment.Index1).IterateNodesPrefix()) {
    112             treeChart_HighlightSubtree(n, Color.CornflowerBlue);
    113           }
    114           // highlight all the nodes according to their sample count
    115           var nodes = graphNode.Data.IterateNodesPrefix().ToList();
    116           foreach (var n in nodes.Skip(1)) {
    117             var i = (int)Math.Round(n.NodeWeight / max * c);
    118             var fillColor = Color.FromArgb(alpha, ColorGradient.Colors[i]);
    119             treeChart_HighlightSubtree(n, null, fillColor);
    120           }
    121         } else if (td != null) {
    122           var nodes = graphNode.Data.IterateNodesPrefix().ToList();
    123           foreach (var n in nodes.Skip(1)) { // skip program root symbol
    124             var i = (int)Math.Round(n.NodeWeight / max * c);
    125             var fillColor = Color.FromArgb(alpha, ColorGradient.Colors[i]);
    126             treeChart_HighlightSubtree(n, null, fillColor);
    127           }
    128           treeChart_HighlightSubtree(nodes[td.LastSubtreeIndex], Color.Orange);
    129           treeChart_HighlightSubtree(nodes[td.LastFragmentIndex], Color.CornflowerBlue);
    130         }
    131       }
    132     }
     100      var data = graphNode.InArcs.Last().Data;
     101      var fragment = data as IFragment<ISymbolicExpressionTreeNode>;
     102      var td = data as TraceData;
     103      // calculate max only in the current generation
     104      var max = Content.GetByRank(graphNode.Rank).Max(x => ((IGenealogyGraphNode<ISymbolicExpressionTree>)x).Data.IterateNodesPrefix().Max(y => y.NodeWeight));
     105      //      var max = Content.GetByRank(graphNode.Rank).Max(x => x.Weight);
     106
     107      if (max.IsAlmost(0))
     108        max = 1;
     109
     110      int colorCount = ColorGradient.Colors.Count - 1;
     111      const int alpha = 100;
     112      if (fragment != null) {
     113        // highlight the fragment with a blue line color
     114        foreach (var n in graphNode.Data.NodeAt(fragment.Index1).IterateNodesPrefix()) {
     115          treeChart_HighlightSubtree(n, Color.CornflowerBlue);
     116        }
     117        // highlight all the nodes according to their sample count
     118        var nodes = graphNode.Data.IterateNodesPrefix().ToList();
     119        foreach (var n in nodes.Skip(1)) {
     120          var i = (int)Math.Round(n.NodeWeight / max * colorCount);
     121          var fillColor = Color.FromArgb(alpha, ColorGradient.Colors[i]);
     122          treeChart_HighlightSubtree(n, null, fillColor);
     123        }
     124      } else if (td != null) {
     125        var nodes = graphNode.Data.IterateNodesPrefix().ToList();
     126        foreach (var n in nodes.Skip(1)) { // skip program root symbol
     127          var i = (int)Math.Round(n.NodeWeight / max * colorCount);
     128          var fillColor = Color.FromArgb(alpha, ColorGradient.Colors[i]);
     129          treeChart_HighlightSubtree(n, null, fillColor);
     130        }
     131        treeChart_HighlightSubtree(nodes[td.LastSubtreeIndex], Color.Orange);
     132        treeChart_HighlightSubtree(nodes[td.LastFragmentIndex], Color.CornflowerBlue);
     133      }
     134    }
     135
     136    #region specific symbolic expression tree node click actions
     137    private void OnTreeNodeLeftClicked(ISymbolicExpressionTreeNode node) {
     138      SelectedSubtree = node;
     139      bool trace = genealogyGraphChart.TraceFragments;
     140
     141      // check whether we are in 'trace' or 'match' mode
     142      if (trace) {
     143        // TODO: maybe highlight based on per generation maximum
     144        //        double max = Content.Vertices.Max(x => x.Data.IterateNodesPrefix().Average(y => y.NodeWeight));
     145        // perform fragment tracing
     146        var graphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)genealogyGraphChart.SelectedGraphNode;
     147        var max = Content.GetByRank(graphNode.Rank).Max(x => x.Weight);
     148        var subtreeIndex = graphNode.Data.IterateNodesPrefix().ToList().IndexOf(node);
     149        var traceGraph = TraceCalculator.TraceSubtree(graphNode, subtreeIndex, updateVertexWeights: false, updateSubtreeWeights: false, cacheTraceNodes: true);
     150        if (traceGraph.Vertices.Any()) {
     151          var vertices = traceGraph.Vertices.Select(x => Content.GetByContent(x.Data));
     152          genealogyGraphChart.UpdateEnabled = false;
     153          genealogyGraphChart.ClearPrimitives();
     154          foreach (var v in vertices) {
     155            var vNode = genealogyGraphChart.GetMappedNode(v);
     156            var wSum = v.Data.IterateNodesPrefix().Average(x => x.NodeWeight);
     157            // use a grayscale gradient
     158            var color = ColorGradient.GrayscaledColors[(int)Math.Round(wSum / max * (ColorGradient.GrayscaledColors.Count - 1))];
     159            vNode.Brush = new SolidBrush(color);
     160          }
     161          genealogyGraphChart.UpdateEnabled = true;
     162          genealogyGraphChart.EnforceUpdate();
     163
     164          if (openNew_CheckBox.Checked) {
     165            MainFormManager.MainForm.ShowContent(traceGraph); // display the fragment graph on the screen
     166          }
     167        }
     168      } else {
     169        // perform matching like it was done before
     170        // currently there is no possibility to specify the subtree matching criteria
     171        var trees = Content.Vertices.Select(x => x.Data);
     172        var matchingTrees = trees.Where(x => x.Root.ContainsSubtree(node, comparer));
     173
     174        var matchingVertices = matchingTrees.Select(x => Content.GetByContent(x));
     175        graphChart_HighlightMatchingVertices(matchingVertices);
     176      }
     177    }
     178
     179    private void OnTreeNodeMiddleClicked(ISymbolicExpressionTreeNode node) {
     180      var index = SelectedSubtree.IterateNodesPrefix().ToList().IndexOf(node);
     181      if (index > 0) {
     182        var s = ClonedSubtree.NodeAt(index);
     183        s.Parent.RemoveSubtree(s.Parent.IndexOfSubtree(s));
     184
     185        var trees = Content.Vertices.Select(x => x.Data);
     186        var matchingTrees = trees.Where(x => x.Root.ContainsSubtree(ClonedSubtree, comparer));
     187
     188        var matchingVertices = matchingTrees.Select(x => Content.GetByContent(x));
     189        treeChart_HighlightSubtree(node, Color.Black, Color.White);
     190        graphChart_HighlightMatchingVertices(matchingVertices);
     191      }
     192    }
     193    #endregion
    133194
    134195    public void treeChart_SymbolicExpressionTreeNodeClicked(object sender, MouseEventArgs args) {
     
    139200      treeChart_HighlightSubtree(subtree, Color.Black, Color.RoyalBlue);
    140201
    141       bool trace = genealogyGraphChart.TraceFragments; // check whether we are in 'trace' or 'match' mode
    142 
    143202      switch (args.Button) {
    144203        case MouseButtons.Left: {
    145             SelectedSubtree = subtree;
    146             if (trace) {
    147               // TODO: maybe highlight based on per generation maximum
    148               double max = Content.Vertices.Max(x => x.Data.IterateNodesPrefix().Average(y => y.NodeWeight));
    149               // perform fragment tracing
    150               var graphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)genealogyGraphChart.SelectedGraphNode;
    151               var subtreeIndex = graphNode.Data.IterateNodesPrefix().ToList().IndexOf(subtree);
    152               var traceGraph = TraceCalculator.TraceSubtree(graphNode, subtreeIndex, updateVertexWeights: false, updateSubtreeWeights: false, cacheTraceNodes: true);
    153               if (traceGraph.Vertices.Any()) {
    154                 var vertices = traceGraph.Vertices.Select(x => Content.GetByContent(x.Data));
    155                 genealogyGraphChart.UpdateEnabled = false;
    156                 genealogyGraphChart.ClearPrimitives();
    157                 foreach (var v in vertices) {
    158                   var vNode = genealogyGraphChart.GetMappedNode(v);
    159                   var wSum = v.Data.IterateNodesPrefix().Average(x => x.NodeWeight);
    160                   // use a grayscal gradient
    161                   var color = ColorGradient.GrayscaledColors[(int)Math.Round(wSum / max * (ColorGradient.GrayscaledColors.Count - 1))];
    162                   vNode.Brush = new SolidBrush(color);
    163                 }
    164                 genealogyGraphChart.UpdateEnabled = true;
    165                 genealogyGraphChart.EnforceUpdate();
    166 
    167                 if (openNew_CheckBox.Checked) {
    168                   MainFormManager.MainForm.ShowContent(traceGraph); // display the fragment graph on the screen
    169                 }
    170               }
    171             } else {
    172               // perform matching like it was done before
    173               // currently there is no possibility to specify the subtree matching criteria
    174               var trees = Content.Vertices.Select(x => x.Data);
    175               var matchingTrees = trees.Where(x => x.Root.ContainsSubtree(subtree, comparer));
    176 
    177               var matchingVertices = matchingTrees.Select(x => Content.GetByContent(x));
    178               graphChart_HighlightMatchingVertices(matchingVertices);
    179             }
     204            OnTreeNodeLeftClicked(subtree);
    180205            break;
    181206          }
    182207        case MouseButtons.Middle: {
    183             var index = SelectedSubtree.IterateNodesPrefix().ToList().IndexOf(subtree);
    184             if (index > 0) {
    185               var s = ClonedSubtree.NodeAt(index);
    186               s.Parent.RemoveSubtree(s.Parent.IndexOfSubtree(s));
    187 
    188               var trees = Content.Vertices.Select(x => x.Data);
    189               var matchingTrees = trees.Where(x => x.Root.ContainsSubtree(ClonedSubtree, comparer));
    190 
    191               var matchingVertices = matchingTrees.Select(x => Content.GetByContent(x));
    192               treeChart_HighlightSubtree(subtree, Color.Black, Color.White);
    193               graphChart_HighlightMatchingVertices(matchingVertices);
    194             }
     208            OnTreeNodeMiddleClicked(subtree);
    195209            break;
    196210          }
     
    260274    private void navigateRightButton_Click(object sender, EventArgs e) {
    261275      var graphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)genealogyGraphChart.SelectedGraphNode;
    262       var inArcs = graphNode.InArcs.ToList();
     276      var inArcs = (List<IArc>)((IVertex)graphNode).InArcs;
    263277      if (inArcs.Count > 0) {
    264         var data = inArcs.Last().Data;
    265         var fragment = (data as IFragment<ISymbolicExpressionTreeNode>);
    266         var td = data as TraceData;
     278        if (inArcs.Count == 1) {
     279          genealogyGraphChart.SelectedGraphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)inArcs[0].Source;
     280          return;
     281        }
     282        IGenealogyGraphNode<ISymbolicExpressionTree> source = null;
     283        var fragment = ((IArc<IDeepCloneable>)inArcs.Last()).Data as IFragment<ISymbolicExpressionTreeNode>;
     284        var traceData = ((IArc<IDeepCloneable>)inArcs[0]).Data as TraceData;
    267285        if (fragment != null) {
    268           genealogyGraphChart.SelectedGraphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)inArcs.Last().Source;
    269         } else if (td != null) {
    270           if (inArcs.Count == 1) {
    271             genealogyGraphChart.SelectedGraphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)inArcs[0].Source;
    272             return;
    273           }
    274           // the first arc from inArcs will always represent the connection to the root parent
    275           // (tracing the body of the traced subtree)
    276           // therefore, in order to follow the correct non-root parent, we have to find the correct arc
    277           // But, the FragmentIndex recorded in the TraceData of the first arc has to match the SubtreeIndex recorded in the TraceData of the searched arc
    278           td = (TraceData)inArcs[0].Data;
    279           var f1 = (graphNode.Data).NodeAt(td.LastFragmentIndex);
    280           for (int i = 1; i < inArcs.Count; ++i) {
    281             td = (TraceData)inArcs[i].Data;
    282             var f2 = ((IGenealogyGraphNode<ISymbolicExpressionTree>)inArcs[i].Source).Data.NodeAt(td.SubtreeIndex);
    283             // if the subtrees are the same we have found a match
    284             // note: this is not necessarily 100% correct if in the trace graph we have identical fragments on different arcs
    285             if (f1.Difference(f2) == null) {
    286               genealogyGraphChart.SelectedGraphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)inArcs[i].Source;
    287               return;
    288             }
    289           }
    290           throw new InvalidOperationException("Error calculating the next step.");
    291         }
     286          source = (IGenealogyGraphNode<ISymbolicExpressionTree>)inArcs.Last().Source;
     287        }
     288        if (traceData != null) {
     289          var f = graphNode.Data.NodeAt(traceData.LastFragmentIndex);
     290          var selected = inArcs.Skip(1).First(x => {
     291            var td = (TraceData)((IArc<IDeepCloneable>)x).Data;
     292            var s = (IGenealogyGraphNode<ISymbolicExpressionTree>)x.Source;
     293            return f.Difference(s.Data.NodeAt(td.SubtreeIndex)) == null;
     294          });
     295          source = (IGenealogyGraphNode<ISymbolicExpressionTree>)selected.Source;
     296        }
     297        if (source != null)
     298          genealogyGraphChart.SelectedGraphNode = source;
    292299      }
    293300    }
Note: See TracChangeset for help on using the changeset viewer.