Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/TraceCalculator.cs @ 11881

Last change on this file since 11881 was 11881, checked in by bburlacu, 9 years ago

#1772:

  • TraceGraph: Improved saving of trace data, changed GetTraceNode method to AddTraceNode and made the code more clear.
  • SymbolicDataAnalysisGenealogyGraphView: added highlighting of trace information (when visualizing trace graphs)
  • GenealogyGraphChart: removed useless code
File size: 11.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Diagnostics;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.EvolutionTracking;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
33  [Item("TraceCalculator", "Walks a genealogy graph and produces a trace of the specified subtree")]
34  [StorableClass]
35  public class TraceCalculator : Item {
36    private IGenealogyGraph<ISymbolicExpressionTree> traceGraph;
37    private Dictionary<IGenealogyGraphNode<ISymbolicExpressionTree>, TraceData> traceMap;
38    private Dictionary<ISymbolicExpressionTree, List<ISymbolicExpressionTreeNode>> nodeListCache;
39
40    public IGenealogyGraph<ISymbolicExpressionTree> TraceGraph { get { return traceGraph; } }
41
42    public TraceCalculator() {
43      traceGraph = new GenealogyGraph<ISymbolicExpressionTree>();
44      traceMap = new Dictionary<IGenealogyGraphNode<ISymbolicExpressionTree>, TraceData>();
45      nodeListCache = new Dictionary<ISymbolicExpressionTree, List<ISymbolicExpressionTreeNode>>();
46    }
47
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(IGenealogyGraphNode<ISymbolicExpressionTree> node, int subtreeIndex) {
57      var tc = new TraceCalculator();
58      tc.Trace(node, subtreeIndex);
59      return tc.TraceGraph;
60    }
61
62    public IGenealogyGraph<ISymbolicExpressionTree> Trace(IGenealogyGraphNode<ISymbolicExpressionTree> node, int subtreeIndex) {
63      traceGraph = new GenealogyGraph<ISymbolicExpressionTree>();
64      traceMap = new Dictionary<IGenealogyGraphNode<ISymbolicExpressionTree>, TraceData>();
65      nodeListCache = new Dictionary<ISymbolicExpressionTree, List<ISymbolicExpressionTreeNode>>();
66      TraceRecursive(node, subtreeIndex);
67      return traceGraph;
68    }
69
70    /// <summary>
71    /// This method starts from a given vertex in the genealogy graph and works its way
72    /// up the ancestry trying to track the structure of the subtree given by subtreeIndex.
73    /// This method will skip genealogy graph nodes that did not have an influence on the
74    /// structure of the tracked subtree.
75    ///
76    /// Only genealogy nodes which did have an influence are added (as copies) to the trace
77    /// and are consequently called 'trace nodes'.
78    ///
79    /// The arcs connecting trace nodes hold information about the locations of the subtrees
80    /// and fragments that have been swapped in the form of a tuple (si, fi, lastSi, lastFi),
81    /// where:
82    /// - si is the subtree index in the current trace node
83    /// - fi is the fragment index in the current trace node
84    /// - lastSi is the subtree index in the previous trace node
85    /// - lastFi is the subtree index in the previous trace node
86    /// </summary>
87    /// <param name="node">The current node in the genealogy graph</param>
88    /// <param name="subtreeIndex">The index of the traced subtree</param>
89    /// <param name="last">The last added node in the trace graph</param>
90    private void TraceRecursive(IGenealogyGraphNode<ISymbolicExpressionTree> node, int subtreeIndex, IGenealogyGraphNode<ISymbolicExpressionTree> last = null) {
91      var g = node;
92      int si = subtreeIndex; // subtree index
93      int fi = 0; // fragment index
94      while (g.Parents.Any()) {
95        Debug.Assert(si < g.Data.Length);
96        var inArcs = (List<IArc>)((IVertex)g).InArcs;
97        var fragment = (IFragment<ISymbolicExpressionTreeNode>)inArcs.Last().Data;
98        if (fragment == null) {
99          // TODO: think about what the correct behavior should be here (seems good so far)
100          // the node is either an elite node or (in rare cases) no fragment was transferred
101          g = (IGenealogyGraphNode<ISymbolicExpressionTree>)inArcs[0].Source;
102          continue;
103        }
104
105        fi = fragment.Index1;               // fragment index
106        int fl = fragment.Root.GetLength();     // fragment length
107        int sl = NodeAt(g.Data, si).GetLength(); // subtree length
108
109        #region trace crossover
110        if (inArcs.Count == 2) {
111          var parent0 = (IGenealogyGraphNode<ISymbolicExpressionTree>)inArcs[0].Source;
112          var parent1 = (IGenealogyGraphNode<ISymbolicExpressionTree>)inArcs[1].Source;
113          if (fi == si) {
114            g = parent1;
115            si = fragment.Index2;
116            continue;
117          }
118          if (fi < si) {
119            if (fi + fl > si) {
120              // fragment contains subtree
121              g = parent1;
122              si += fragment.Index2 - fi;
123            } else {
124              // fragment distinct from subtree
125              g = parent0;
126              si += NodeAt(g.Data, fi).GetLength() - fl;
127            }
128            continue;
129          }
130          if (fi > si) {
131            if (fi < si + sl) {
132              // subtree contains fragment => branching point in the fragment graph
133              var n = AddTraceNode(g, si, fi); // current node becomes "last" as we restart tracing from the parent
134              TraceRecursive(parent0, si, n);
135              TraceRecursive(parent1, fragment.Index2, n);
136              break;
137            } else {
138              // subtree and fragment are distinct.
139              g = parent0;
140              continue;
141            }
142          }
143        }
144        #endregion
145        #region trace mutation
146        // mutation is handled in a simple way: we branch every time there is an overlap between the subtree and the fragment
147        // (since mutation effects can be quite unpredictable: replace branch, change node, shake tree, etc)
148        if (inArcs.Count == 1) {
149          var parent0 = (IGenealogyGraphNode<ISymbolicExpressionTree>)inArcs[0].Source;
150          Debug.Assert(fragment.Index1 == fragment.Index2);
151          // check if the subtree and the fragment overlap => branch out
152          if ((si == fi) || (si < fi && fi < si + sl) || (fi < si && si < fi + fl)) {
153            var n = AddTraceNode(g, si, fi); // current node becomes "last" as we restart tracing from the parent
154            int i = si < fi ? si : fi;
155            TraceRecursive(parent0, i, n);
156            break;
157          } else {
158            // if they don't overlap, go up
159            g = parent0;
160            if (fi < si)
161              si += NodeAt(g.Data, fi).GetLength() - fl;
162            continue;
163          }
164        }
165        #endregion
166        throw new InvalidOperationException("A node cannot have more than two parents");
167      }
168      // when we are out of the while the last vertex must be connected with the current one
169      // if there is no last vertex, it means the tracing reached the top of the genealogy graph
170      var current = AddTraceNode(g, si, fi);
171      if (last != null)
172        ConnectLast(current, last, si, fi);
173    }
174
175    /// <summary>
176    /// Get the trace node from the trace graph which corresponds to node g from the genealogy graph.
177    /// If the trace graph does not contain such a node, one is created by performing a shallow copy of g, then inserted into the trace graph.
178    /// </summary>
179    /// <param name="g">The genealogy graph node</param>
180    /// <param name="si">The subtree index</param>
181    /// <param name="fi">The fragment index</param>
182    /// <returns></returns>
183    private IGenealogyGraphNode<ISymbolicExpressionTree> AddTraceNode(IGenealogyGraphNode<ISymbolicExpressionTree> g, int si, int fi) {
184      var n = traceGraph.GetByContent(g.Data);
185      if (n == null) {
186        n = g.Copy();
187        traceGraph.AddVertex(n);
188        Debug.Assert(!traceMap.ContainsKey(n));
189        traceMap[n] = new TraceData(si, fi, -1, -1); // only the first two fields are needed
190      }
191      return n;
192    }
193
194    // caching node lists brings ~2.5-2.7x speed improvement (since graph nodes are visited multiple times)
195    // this caching will be even more effective with larger tree sizes
196    private ISymbolicExpressionTreeNode NodeAt(ISymbolicExpressionTree tree, int index) {
197      List<ISymbolicExpressionTreeNode> list;
198      nodeListCache.TryGetValue(tree, out list);
199      if (list == null) {
200        list = tree.IterateNodesPrefix().ToList();
201        nodeListCache[tree] = list;
202      }
203      return list[index];
204    }
205
206    /// <summary>
207    /// Connect the current node of the trace graph with the node that was previously added (@last). The current node of the trace graph is determined by the content
208    /// of the genealogy graph node @g. 
209    /// </summary>
210    /// <param name="current">The current node in the genealogy graph</param>
211    /// <param name="last">The last added node in the trace graph</param>   
212    /// <param name="si">The index of the traced subtree</param>
213    /// <param name="fi">The index of the fragment</param>
214    private void ConnectLast(IGenealogyGraphNode<ISymbolicExpressionTree> current, IGenealogyGraphNode<ISymbolicExpressionTree> last, int si, int fi) {
215      var lastTraceData = traceMap[last];
216      int lastSi = lastTraceData.SubtreeIndex; // last subtree index (index of the traced subtree in the previous trace node)
217      int lastFi = lastTraceData.FragmentIndex; // last fragment index (index of the fragment in the previous trace node)
218      var td = new TraceData(si, fi, lastSi, lastFi); // trace data
219      var arc = current.OutArcs.SingleOrDefault(a => a.Target == last && a.Data.Equals(td));
220      if (arc == null) {
221        arc = new GenealogyGraphArc(current, last) { Data = td };
222        traceGraph.AddArc(arc);
223      }
224    }
225  }
226  // this class is here to help clarify the semantics
227  public class TraceData : Tuple<int, int, int, int> {
228    public TraceData(int currentSubtreeIndex, int currentFragmentIndex, int lastSubtreeIndex, int lastFragmentIndex)
229      : base(currentSubtreeIndex, currentFragmentIndex, lastSubtreeIndex, lastFragmentIndex) {
230    }
231
232    public int SubtreeIndex { get { return Item1; } }
233    public int FragmentIndex { get { return Item2; } }
234    public int LastSubtreeIndex { get { return Item3; } }
235    public int LastFragmentIndex { get { return Item4; } }
236  }
237
238  internal static class Util {
239    // shallow node copy (does not clone the data or the arcs)
240    #region some helper methods for shortening the tracing code
241    public static IGenealogyGraphNode<ISymbolicExpressionTree> Copy(this IGenealogyGraphNode<ISymbolicExpressionTree> node) {
242      return new GenealogyGraphNode<ISymbolicExpressionTree>(node.Data) { Rank = node.Rank, Quality = node.Quality };
243    }
244    #endregion
245  }
246}
Note: See TracBrowser for help on using the repository browser.