Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/20/15 18:20:59 (10 years ago)
Author:
bburlacu
Message:

#1772: Changed the way node highlighting is performed (taking into account sampling count relative to current generation). Made NodeWeight field storable in the SymbolicExpressionTreeNode. Updated the statistics counting in the TraceCalculator.

Location:
branches/HeuristicLab.EvolutionTracking
Files:
1 deleted
6 edited
1 copied

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Interfaces/ISymbolicExpressionTreeNode.cs

    r12155 r12231  
    3434    int GetBranchLevel(ISymbolicExpressionTreeNode child);
    3535
     36    double NodeWeight { get; set; }
     37
    3638    IEnumerable<ISymbolicExpressionTreeNode> IterateNodesBreadth();
    3739    IEnumerable<ISymbolicExpressionTreeNode> IterateNodesPostfix();
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeNode.cs

    r12155 r12231  
    5050    }
    5151
     52    [Storable]
     53    public double NodeWeight { get; set; }
     54
    5255    [StorableConstructor]
    5356    protected SymbolicExpressionTreeNode(bool deserializing) { }
     
    5760      length = original.length;
    5861      depth = original.depth;
     62      NodeWeight = original.NodeWeight;
    5963      if (original.subtrees != null) {
    6064        subtrees = new List<ISymbolicExpressionTreeNode>(original.subtrees.Count);
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/Tracking/SymbolicDataAnalysisGenealogyGraphView.cs

    r12225 r12231  
    100100        var fragment = data as IFragment<ISymbolicExpressionTreeNode>;
    101101        var td = data as TraceData;
    102         //        double max = Content.Vertices.SelectMany(x => x.Data.IterateNodesPrefix()).Max(x => x.NodeWeight);
    103 
     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;
    104109        if (fragment != null) {
    105           //          treeChart_HighlightSubtree(graphNode.Data.NodeAt(fragment.Index1));
     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
    106115          var nodes = graphNode.Data.IterateNodesPrefix().ToList();
    107           int c = ColorGradient.Colors.Count - 1;
     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();
    108123          foreach (var n in nodes.Skip(1)) { // skip program root symbol
    109             //            var i = (int)Math.Round(n.NodeWeight / max * c);
    110             //            const int alpha = 50;
    111             //            var fillColor = Color.FromArgb(alpha, ColorGradient.Colors[i]);
    112 
    113             //            treeChart_HighlightSubtree(n, null, fillColor);
    114           }
    115         } else if (td != null) {
    116 
    117           var nodes = graphNode.Data.IterateNodesPrefix().ToList();
    118           int c = ColorGradient.Colors.Count - 1;
    119           foreach (var n in nodes.Skip(1)) { // skip program root symbol
    120             //            var i = (int)Math.Round(n.NodeWeight / max * c);
    121             //            const int alpha = 50;
    122             //            var fillColor = Color.FromArgb(alpha, ColorGradient.Colors[i]);
    123 
    124             //            treeChart_HighlightSubtree(n, null, fillColor);
    125           }
    126 
     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          }
    127128          treeChart_HighlightSubtree(nodes[td.LastSubtreeIndex], Color.Orange);
    128129          treeChart_HighlightSubtree(nodes[td.LastFragmentIndex], Color.CornflowerBlue);
     
    141142
    142143      switch (args.Button) {
    143         case MouseButtons.Left:
    144           SelectedSubtree = subtree;
    145           if (trace) {
    146             // perform fragment tracing
    147             var graphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)genealogyGraphChart.SelectedGraphNode;
    148             var subtreeIndex = graphNode.Data.IterateNodesPrefix().ToList().IndexOf(subtree);
    149             var traceGraph = TraceCalculator.TraceSubtree(graphNode, subtreeIndex, updateVertexWeights: true, updateSubtreeWeights: true, cacheTraceNodes: true);
    150             if (traceGraph.Vertices.Any()) {
    151               genealogyGraphChart.UpdateEnabled = false;
    152               genealogyGraphChart.ClearPrimitives();
    153               var traceVertices = traceGraph.Vertices.ToList();
    154               var genealogyNodes = traceVertices.Select(v => Content.GetByContent(v.Data)).ToList();
    155               // color graph vertices according to their weight
    156               double max = traceGraph.Vertices.Max(x => x.Data.IterateNodesPrefix().Max(n => x.Weight));
    157               for (int i = 0; i < genealogyNodes.Count; ++i) {
    158                 var g = genealogyNodes[i];
    159                 g.Weight = traceVertices[i].Weight;
    160                 var v = genealogyGraphChart.GetMappedNode(g);
    161                 if (v != null) {
    162                   v.Brush = new SolidBrush(ColorGradient.Colors[(int)Math.Round(g.Weight * (ColorGradient.Colors.Count - 1) / max)]);
     144        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
    163169                }
    164170              }
    165 
    166               genealogyGraphChart.UpdateEnabled = true;
    167               genealogyGraphChart.EnforceUpdate();
    168 
    169               if (openNew_CheckBox.Checked)
    170                 MainFormManager.MainForm.ShowContent(traceGraph); // display the fragment graph on the screen
     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);
    171179            }
    172           } else {
    173             // perform matching like it was done before
    174             // currently there is no possibility to specify the subtree matching criteria
    175             var trees = Content.Vertices.Select(x => x.Data);
    176             var matchingTrees = trees.Where(x => x.Root.ContainsSubtree(subtree, comparer));
    177 
    178             var matchingVertices = matchingTrees.Select(x => Content.GetByContent(x));
    179             graphChart_HighlightMatchingVertices(matchingVertices);
    180           }
    181           break;
    182         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           }
    195           break;
     180            break;
     181          }
     182        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            }
     195            break;
     196          }
    196197      }
    197198    }
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/Analyzers/SymbolicDataAnalysisSubtreeSampleCountAnalyzer.cs

    r12210 r12231  
    2020#endregion
    2121
    22 using System;
    2322using System.Linq;
    24 using HeuristicLab.Analysis;
    2523using HeuristicLab.Common;
    2624using HeuristicLab.Core;
     
    2826using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2927using HeuristicLab.EvolutionTracking;
    30 using HeuristicLab.Optimization;
    3128using HeuristicLab.Parameters;
    3229using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3330
    3431namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Tracking.Analyzers {
    35   [Item("SymbolicDataAnalysisTraceOverlapAnalyzer", "An analyzer for trace graph overlap.")]
     32  [Item("SymbolicDataAnalysisSubtreeSampleCountAnalyzer", "An analyzer for trace graph overlap.")]
    3633  [StorableClass]
    37   public class SymbolicDataAnalysisTraceOverlapAnalyzer : EvolutionTrackingAnalyzer<ISymbolicExpressionTree> {
    38     private const string PercentageOfBestSolutionsParameterName = "PercentageOfBestSolutions";
     34  public class SymbolicDataAnalysisSubtreeSampleCountAnalyzer : EvolutionTrackingAnalyzer<ISymbolicExpressionTree> {
     35    private const string CacheTraceNodesParameterName = "CacheTraceNodes";
     36    private readonly TraceCalculator traceCalculator = new TraceCalculator { UpdateVertexWeights = true, UpdateSubtreeWeights = true, CacheTraceNodes = true };
    3937
    4038    #region parameters
    41     public IFixedValueParameter<DoubleValue> PercentageOfBestSolutionsParameter {
    42       get { return (IFixedValueParameter<DoubleValue>)Parameters[PercentageOfBestSolutionsParameterName]; }
     39    public IFixedValueParameter<BoolValue> CacheTraceNodesParameter {
     40      get { return (IFixedValueParameter<BoolValue>)Parameters[CacheTraceNodesParameterName]; }
    4341    }
    4442    #endregion
    4543
    46     public SymbolicDataAnalysisTraceOverlapAnalyzer() {
    47       UpdateCounterParameter.ActualName = "TraceOverlapAnalyzerUpdateCounter";
    48       Parameters.Add(new FixedValueParameter<DoubleValue>(PercentageOfBestSolutionsParameterName, new DoubleValue(1.0)));
     44    #region parameter properties
     45    public bool CacheTraceNodes {
     46      get { return CacheTraceNodesParameter.Value.Value; }
     47      set { CacheTraceNodesParameter.Value.Value = value; }
     48    }
     49    #endregion
     50
     51    public SymbolicDataAnalysisSubtreeSampleCountAnalyzer() {
     52      UpdateCounterParameter.ActualName = "SubtreeSampleCountAnalyzerUpdateCounter";
     53
     54      Parameters.Add(new FixedValueParameter<BoolValue>(CacheTraceNodesParameterName, new BoolValue(true)));
    4955    }
    5056
    51     private SymbolicDataAnalysisTraceOverlapAnalyzer(SymbolicDataAnalysisTraceOverlapAnalyzer original, Cloner cloner)
     57    private SymbolicDataAnalysisSubtreeSampleCountAnalyzer(SymbolicDataAnalysisSubtreeSampleCountAnalyzer original, Cloner cloner)
    5258      : base(original, cloner) {
    5359    }
    5460
    5561    public override IDeepCloneable Clone(Cloner cloner) {
    56       return new SymbolicDataAnalysisTraceOverlapAnalyzer(this, cloner);
     62      return new SymbolicDataAnalysisSubtreeSampleCountAnalyzer(this, cloner);
     63    }
     64
     65    public override void ClearState() {
     66      base.ClearState();
     67      traceCalculator.ResetState();
     68    }
     69
     70    public override void InitializeState() {
     71      traceCalculator.ResetState();
    5772    }
    5873
    5974    [StorableConstructor]
    60     private SymbolicDataAnalysisTraceOverlapAnalyzer(bool deserializing) : base(deserializing) { }
     75    private SymbolicDataAnalysisSubtreeSampleCountAnalyzer(bool deserializing) : base(deserializing) { }
    6176
    6277    public override IOperation Apply() {
     
    7994        return base.Apply();
    8095
    81       var bestSolutionsPercentage = PercentageOfBestSolutionsParameter.Value.Value;
    82       int n = (int)Math.Round(bestSolutionsPercentage * PopulationSize.Value);
    83       double avgTraceOverlap = 0;
     96      traceCalculator.CacheTraceNodes = CacheTraceNodes;
     97      if (Generation.Value > 0) {
     98        var rank = PopulationGraph.GetByRank(Generation.Value).Cast<IGenealogyGraphNode<ISymbolicExpressionTree>>().ToList();
     99        foreach (var n in rank)
     100          traceCalculator.Trace(n, 2, false); // during this call weights will be updated
    84101
    85       if (Generation.Value > 0) {
    86         var nodes = PopulationGraph.GetByRank(Generation.Value).Cast<IGenealogyGraphNode<ISymbolicExpressionTree>>().OrderByDescending(x => x.Quality).Take(n);
    87         avgTraceOverlap = nodes.Average(x => TraceCalculator.TraceSubtree(x, 2).Vertices.Count());
    88       }
    89 
    90       DataTable table;
    91       if (!Results.ContainsKey("AverageTraceOverlap")) {
    92         table = new DataTable("Average trace overlap");
    93         var row = new DataRow("Average trace overlap") { VisualProperties = { StartIndexZero = true } };
    94         row.Values.Add(avgTraceOverlap);
    95         table.Rows.Add(row);
    96         Results.Add(new Result("AverageTraceOverlap", table));
    97       } else {
    98         table = (DataTable)Results["AverageTraceOverlap"].Value;
    99         table.Rows["Average trace overlap"].Values.Add(avgTraceOverlap);
     102        foreach (var v in traceCalculator.TraceGraph.Vertices) {
     103          var g = PopulationGraph.GetByContent(v.Data);
     104          g.Weight = v.Weight;
     105        }
    100106      }
    101107      return base.Apply();
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/TraceCalculator.cs

    r12225 r12231  
    160160              if (UpdateSubtreeWeights) {
    161161                var arcs = (List<IArc>)((IVertex)n).InArcs; // at this moment n will have been added as a child to the next trace node
     162
     163                // TODO: try to simplify the code below
    162164                for (int i = 0; i < arcs.Count; ++i) {
    163165                  var td = (TraceData)((IArc<IDeepCloneable>)arcs[i]).Data;
    164166                  var p = (IGenealogyGraphNode<ISymbolicExpressionTree>)arcs[i].Source;
    165                   // TODO: the first part of the check below represents a necessary but not sufficient condition
    166                   // TODO: therefore, a Difference check is also performed, it would be nice to make this code more ellegant
    167                   if (td.LastFragmentIndex == td.SubtreeIndex && fragment.Root.Difference(NodeAt(p.Data, td.SubtreeIndex)) == null) {
    168                     arcs[i].Weight++;
     167                  var s = NodeAt(p.Data, td.SubtreeIndex);
     168                  if (td.LastFragmentIndex == td.SubtreeIndex && fragment.Root.Difference(s) == null) {
     169                    foreach (var ss in s.IterateNodesPrefix()) ss.NodeWeight++; // the node weight will represent the total sample count for a given node
     170                    arcs[i].Weight++; // the arc weights (since there are multiple arcs) will sum up to the same count but give more detail
    169171                    break;
    170172                  }
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.TravelingSalesman

    • Property svn:mergeinfo changed (with no actual effect on merging)
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.TravelingSalesman.Views

    • Property svn:mergeinfo changed (with no actual effect on merging)
Note: See TracChangeset for help on using the changeset viewer.