Free cookie consent management tool by TermsFeed Policy Generator

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

#1772: Some more work on subtree sample counting.

Location:
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking
Files:
1 edited
1 copied

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/Analyzers/SymbolicDataAnalysisSubtreeSampleCountAnalyzer.cs

    r12225 r12265  
    1 #region License Information
     1#region License information
    22/* HeuristicLab
    33 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     
    2626using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2727using HeuristicLab.EvolutionTracking;
     28using HeuristicLab.Parameters;
    2829using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2930
     
    3233  [StorableClass]
    3334  public class SymbolicDataAnalysisSubtreeSampleCountAnalyzer : EvolutionTrackingAnalyzer<ISymbolicExpressionTree> {
     35    private const string CacheTraceNodesParameterName = "CacheTraceNodes";
    3436    private readonly TraceCalculator traceCalculator = new TraceCalculator { UpdateVertexWeights = true, UpdateSubtreeWeights = true, CacheTraceNodes = true };
    3537
     38    #region parameters
     39    public IFixedValueParameter<BoolValue> CacheTraceNodesParameter {
     40      get { return (IFixedValueParameter<BoolValue>)Parameters[CacheTraceNodesParameterName]; }
     41    }
     42    #endregion
     43
     44    #region parameter properties
     45    public bool CacheTraceNodes {
     46      get { return CacheTraceNodesParameter.Value.Value; }
     47      set { CacheTraceNodesParameter.Value.Value = value; }
     48    }
     49    #endregion
     50
    3651    public SymbolicDataAnalysisSubtreeSampleCountAnalyzer() {
    37       UpdateCounterParameter.ActualName = "TraceOverlapAnalyzerUpdateCounter";
     52      UpdateCounterParameter.ActualName = "SubtreeSampleCountAnalyzerUpdateCounter";
     53
     54      Parameters.Add(new FixedValueParameter<BoolValue>(CacheTraceNodesParameterName, new BoolValue(true)));
    3855    }
    3956
     
    7794        return base.Apply();
    7895
    79       double avgTraceOverlap = 0;
    80 
     96      traceCalculator.CacheTraceNodes = CacheTraceNodes;
    8197      if (Generation.Value > 0) {
    8298        var rank = PopulationGraph.GetByRank(Generation.Value).Cast<IGenealogyGraphNode<ISymbolicExpressionTree>>().ToList();
    83         foreach (var n in rank) {
    84           // during the trace the SymbolicExpressionTreeNode weights will be updated behind the scenes
    85           // the weights represent the sample count of each subtree
    86           var trace = traceCalculator.Trace(n, 2, false);
    87           foreach (var v in trace.Vertices) {
    88             var g = PopulationGraph.GetByContent(v.Data);
    89             g.Weight = v.Weight;
    90           }
     99        foreach (var n in rank)
     100          traceCalculator.Trace(n, 2, false); // during this call weights will be updated
     101
     102        foreach (var v in traceCalculator.TraceGraph.Vertices) {
     103          var g = PopulationGraph.GetByContent(v.Data);
     104          g.Weight = v.Weight;
    91105        }
    92106      }
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/TraceCalculator.cs

    r12231 r12265  
    155155                traceCache.Add(t1);
    156156              }
    157               // gather statistics about sampled individuals and sampled subtrees
    158               if (UpdateVertexWeights)
    159                 n.Weight++;
    160               if (UpdateSubtreeWeights) {
    161                 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
    164                 for (int i = 0; i < arcs.Count; ++i) {
    165                   var td = (TraceData)((IArc<IDeepCloneable>)arcs[i]).Data;
    166                   var p = (IGenealogyGraphNode<ISymbolicExpressionTree>)arcs[i].Source;
    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
    171                     break;
    172                   }
    173                 }
    174               }
    175157              break;
    176158            } else {
     
    197179              traceCache.Add(t);
    198180            }
    199             if (UpdateVertexWeights)
    200               n.Weight++;
    201181            break;
    202182          } else {
     
    257237    /// <param name="si">The index of the traced subtree</param>
    258238    /// <param name="fi">The index of the fragment</param>
    259     private void ConnectLast(IGenealogyGraphNode<ISymbolicExpressionTree> current,
    260       IGenealogyGraphNode<ISymbolicExpressionTree> last, int si, int fi) {
     239    private void ConnectLast(IGenealogyGraphNode<ISymbolicExpressionTree> current, IGenealogyGraphNode<ISymbolicExpressionTree> last, int si, int fi) {
    261240      var lastTraceData = traceMap[last];
    262241      int lastSi = lastTraceData.SubtreeIndex; // last subtree index (index of the traced subtree in the previous trace node)
    263242      int lastFi = lastTraceData.FragmentIndex; // last fragment index (index of the fragment in the previous trace node)
    264243      var td = new TraceData(si, fi, lastSi, lastFi); // trace data
    265       // using the inArcs seems to be slightly more efficient than using the outArcs
     244
    266245      // TODO: more testing
    267       var inArcs = (List<IArc>)((IVertex)last).InArcs;
     246      var inArcs = (List<IArc>)((IVertex)last).InArcs; // using the InArcs seems to be slightly more efficient than using the OutArcs
    268247      var arc = inArcs.FirstOrDefault(a => a.Source == current && ((IArc<IDeepCloneable>)a).Data.Equals(td));
    269248      if (arc == null) {
    270249        arc = new GenealogyGraphArc(current, last) { Data = td };
    271250        TraceGraph.AddArc(arc);
     251      } else {
     252        if (UpdateVertexWeights) {
     253          arc.Weight++;
     254          current.Weight++;
     255        }
     256        if (UpdateSubtreeWeights) {
     257          var subtree = NodeAt(current.Data, td.SubtreeIndex);
     258          foreach (var s in subtree.IterateNodesPrefix())
     259            s.NodeWeight++;
     260        }
    272261      }
    273262    }
Note: See TracChangeset for help on using the changeset viewer.