Changeset 12225 for branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking
- Timestamp:
- 03/19/15 01:10:21 (10 years ago)
- Location:
- branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/TraceCalculator.cs
r11979 r12225 39 39 public IGenealogyGraph<ISymbolicExpressionTree> TraceGraph { get; private set; } 40 40 41 public bool UpdateVertexWeights { get; set; } 42 public bool UpdateSubtreeWeights { get; set; } 43 public bool CacheTraceNodes { get; set; } 44 41 45 public TraceCalculator() { 46 ResetState(); 47 } 48 49 protected TraceCalculator(TraceCalculator original, Cloner cloner) 50 : base(original, cloner) { 51 } 52 53 public override IDeepCloneable Clone(Cloner cloner) { 54 return new TraceCalculator(this, cloner); 55 } 56 57 public void ResetState() { 42 58 TraceGraph = new GenealogyGraph<ISymbolicExpressionTree>(); 43 59 traceMap = new Dictionary<IGenealogyGraphNode<ISymbolicExpressionTree>, TraceData>(); … … 46 62 } 47 63 48 protected TraceCalculator(TraceCalculator original, Cloner cloner) 49 : base(original, cloner) { 50 } 51 52 public override IDeepCloneable Clone(Cloner cloner) { 53 return new TraceCalculator(this, cloner); 54 } 55 56 public static IGenealogyGraph<ISymbolicExpressionTree> TraceSubtree( 57 IGenealogyGraphNode<ISymbolicExpressionTree> node, int subtreeIndex) { 58 var tc = new TraceCalculator(); 64 public static IGenealogyGraph<ISymbolicExpressionTree> TraceSubtree(IGenealogyGraphNode<ISymbolicExpressionTree> node, int subtreeIndex, bool updateVertexWeights = false, bool updateSubtreeWeights = false, bool cacheTraceNodes = true) { 65 var tc = new TraceCalculator { 66 UpdateVertexWeights = updateSubtreeWeights, 67 UpdateSubtreeWeights = updateSubtreeWeights, 68 CacheTraceNodes = cacheTraceNodes 69 }; 59 70 tc.Trace(node, subtreeIndex); 60 71 return tc.TraceGraph; 61 72 } 62 73 63 public IGenealogyGraph<ISymbolicExpressionTree> Trace(IGenealogyGraphNode<ISymbolicExpressionTree> node, int subtreeIndex) { 64 TraceGraph = new GenealogyGraph<ISymbolicExpressionTree>(); 65 traceMap = new Dictionary<IGenealogyGraphNode<ISymbolicExpressionTree>, TraceData>(); 66 nodeListCache = new Dictionary<ISymbolicExpressionTree, List<ISymbolicExpressionTreeNode>>(); 67 traceCache = new HashSet<Tuple<IGenealogyGraphNode<ISymbolicExpressionTree>, IGenealogyGraphNode<ISymbolicExpressionTree>, int>>(); 74 public IGenealogyGraph<ISymbolicExpressionTree> Trace(IGenealogyGraphNode<ISymbolicExpressionTree> node, int subtreeIndex, bool resetState = true) { 75 if (resetState) ResetState(); 68 76 TraceRecursive(node, subtreeIndex); 69 77 return TraceGraph; … … 90 98 /// <param name="subtreeIndex">The index of the traced subtree</param> 91 99 /// <param name="last">The last added node in the trace graph</param> 92 private void TraceRecursive(IGenealogyGraphNode<ISymbolicExpressionTree> node, int subtreeIndex, 93 IGenealogyGraphNode<ISymbolicExpressionTree> last = null) { 100 private void TraceRecursive(IGenealogyGraphNode<ISymbolicExpressionTree> node, int subtreeIndex, IGenealogyGraphNode<ISymbolicExpressionTree> last = null) { 94 101 var g = node; 95 102 int si = subtreeIndex; // subtree index 96 103 int fi = 0; // fragment index 97 104 while (((List<IArc>)((IVertex)g).InArcs).Count > 0) { 98 // while (g.Parents.Any()) {99 105 Debug.Assert(si < g.Data.Length); 100 106 var inArcs = (List<IArc>)((IVertex)g).InArcs; … … 138 144 var n = AddTraceNode(g, si, fi); // current node becomes "last" as we restart tracing from the parent 139 145 var t0 = new Tuple<IGenealogyGraphNode<ISymbolicExpressionTree>, IGenealogyGraphNode<ISymbolicExpressionTree>, int>(parent0, n, si); 140 if (! traceCache.Contains(t0)) {146 if (!(CacheTraceNodes && traceCache.Contains(t0))) { 141 147 TraceRecursive(parent0, si, n); 142 148 traceCache.Add(t0); 143 } else { 149 } 150 if (UpdateVertexWeights) 144 151 n.Weight++; 145 }146 152 var t1 = new Tuple<IGenealogyGraphNode<ISymbolicExpressionTree>, IGenealogyGraphNode<ISymbolicExpressionTree>, int>(parent1, n, fragment.Index2); 147 if (! traceCache.Contains(t1)) {153 if (!(CacheTraceNodes && traceCache.Contains(t1))) { 148 154 TraceRecursive(parent1, fragment.Index2, n); 149 155 traceCache.Add(t1); 150 } else { 156 } 157 // gather statistics about sampled individuals and sampled subtrees 158 if (UpdateVertexWeights) 151 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 for (int i = 0; i < arcs.Count; ++i) { 163 var td = (TraceData)((IArc<IDeepCloneable>)arcs[i]).Data; 164 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++; 169 break; 170 } 171 } 152 172 } 153 173 break; … … 171 191 int i = si < fi ? si : fi; 172 192 var t = new Tuple<IGenealogyGraphNode<ISymbolicExpressionTree>, IGenealogyGraphNode<ISymbolicExpressionTree>, int>(parent0, n, i); 173 if (! traceCache.Contains(t)) {193 if (!(CacheTraceNodes && traceCache.Contains(t))) { 174 194 TraceRecursive(parent0, i, n); 175 195 traceCache.Add(t); 176 } else { 196 } 197 if (UpdateVertexWeights) 177 198 n.Weight++; 178 }179 199 break; 180 200 } else { … … 204 224 /// <param name="fi">The fragment index</param> 205 225 /// <returns></returns> 206 private IGenealogyGraphNode<ISymbolicExpressionTree> AddTraceNode(IGenealogyGraphNode<ISymbolicExpressionTree> g, 207 int si, int fi) { 226 private IGenealogyGraphNode<ISymbolicExpressionTree> AddTraceNode(IGenealogyGraphNode<ISymbolicExpressionTree> g, int si, int fi) { 208 227 var n = TraceGraph.GetByContent(g.Data); 209 228 if (n == null) { … … 239 258 IGenealogyGraphNode<ISymbolicExpressionTree> last, int si, int fi) { 240 259 var lastTraceData = traceMap[last]; 241 int lastSi = lastTraceData.SubtreeIndex; 242 // last subtree index (index of the traced subtree in the previous trace node) 243 int lastFi = lastTraceData.FragmentIndex; 244 // last fragment index (index of the fragment in the previous trace node) 260 int lastSi = lastTraceData.SubtreeIndex; // last subtree index (index of the traced subtree in the previous trace node) 261 int lastFi = lastTraceData.FragmentIndex; // last fragment index (index of the fragment in the previous trace node) 245 262 var td = new TraceData(si, fi, lastSi, lastFi); // trace data 246 263 // using the inArcs seems to be slightly more efficient than using the outArcs
Note: See TracChangeset
for help on using the changeset viewer.