Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1772: Improve performance of the TraceCalculator by marking visited trace nodes to avoid duplicate effort.

File size: 13.2 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 Dictionary<IGenealogyGraphNode<ISymbolicExpressionTree>, TraceData> traceMap;
37    private Dictionary<ISymbolicExpressionTree, List<ISymbolicExpressionTreeNode>> nodeListCache;
38    private HashSet<Tuple<IGenealogyGraphNode<ISymbolicExpressionTree>, IGenealogyGraphNode<ISymbolicExpressionTree>, int>> traceCache;
39    public IGenealogyGraph<ISymbolicExpressionTree> TraceGraph { get; private set; }
40
41    public TraceCalculator() {
42      TraceGraph = new GenealogyGraph<ISymbolicExpressionTree>();
43      traceMap = new Dictionary<IGenealogyGraphNode<ISymbolicExpressionTree>, TraceData>();
44      nodeListCache = new Dictionary<ISymbolicExpressionTree, List<ISymbolicExpressionTreeNode>>();
45      traceCache = new HashSet<Tuple<IGenealogyGraphNode<ISymbolicExpressionTree>, IGenealogyGraphNode<ISymbolicExpressionTree>, int>>();
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(
57      IGenealogyGraphNode<ISymbolicExpressionTree> node, int subtreeIndex) {
58      var tc = new TraceCalculator();
59      tc.Trace(node, subtreeIndex);
60      return tc.TraceGraph;
61    }
62
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>>();
68      TraceRecursive(node, subtreeIndex);
69      return TraceGraph;
70    }
71
72    /// <summary>
73    /// This method starts from a given vertex in the genealogy graph and works its way
74    /// up the ancestry trying to track the structure of the subtree given by subtreeIndex.
75    /// This method will skip genealogy graph nodes that did not have an influence on the
76    /// structure of the tracked subtree.
77    ///
78    /// Only genealogy nodes which did have an influence are added (as copies) to the trace
79    /// and are consequently called 'trace nodes'.
80    ///
81    /// The arcs connecting trace nodes hold information about the locations of the subtrees
82    /// and fragments that have been swapped in the form of a tuple (si, fi, lastSi, lastFi),
83    /// where:
84    /// - si is the subtree index in the current trace node
85    /// - fi is the fragment index in the current trace node
86    /// - lastSi is the subtree index in the previous trace node
87    /// - lastFi is the subtree index in the previous trace node
88    /// </summary>
89    /// <param name="node">The current node in the genealogy graph</param>
90    /// <param name="subtreeIndex">The index of the traced subtree</param>
91    /// <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) {
94      var g = node;
95      int si = subtreeIndex; // subtree index
96      int fi = 0; // fragment index
97      while (((List<IArc>)((IVertex)g).InArcs).Count > 0) {
98        //      while (g.Parents.Any()) {
99        Debug.Assert(si < g.Data.Length);
100        var inArcs = (List<IArc>)((IVertex)g).InArcs;
101        var fragment = (IFragment<ISymbolicExpressionTreeNode>)((IGenealogyGraphArc)inArcs.Last()).Data;
102        if (fragment == null) {
103          // TODO: think about what the correct behavior should be here (seems good so far)
104          // the node is either an elite node or (in rare cases) no fragment was transferred
105          g = (IGenealogyGraphNode<ISymbolicExpressionTree>)inArcs[0].Source;
106          continue;
107        }
108
109        fi = fragment.Index1; // fragment index
110        int fl = fragment.Root.GetLength(); // fragment length
111        int sl = NodeAt(g.Data, si).GetLength(); // subtree length
112
113        #region trace crossover
114
115        if (inArcs.Count == 2) {
116          var parent0 = (IGenealogyGraphNode<ISymbolicExpressionTree>)inArcs[0].Source;
117          var parent1 = (IGenealogyGraphNode<ISymbolicExpressionTree>)inArcs[1].Source;
118          if (fi == si) {
119            g = parent1;
120            si = fragment.Index2;
121            continue;
122          }
123          if (fi < si) {
124            if (fi + fl > si) {
125              // fragment contains subtree
126              g = parent1;
127              si += fragment.Index2 - fi;
128            } else {
129              // fragment distinct from subtree
130              g = parent0;
131              si += NodeAt(g.Data, fi).GetLength() - fl;
132            }
133            continue;
134          }
135          if (fi > si) {
136            if (fi < si + sl) {
137              // subtree contains fragment => branching point in the fragment graph
138              var n = AddTraceNode(g, si, fi); // current node becomes "last" as we restart tracing from the parent
139              var t0 = new Tuple<IGenealogyGraphNode<ISymbolicExpressionTree>, IGenealogyGraphNode<ISymbolicExpressionTree>, int>(parent0, n, si);
140              if (!traceCache.Contains(t0)) {
141                TraceRecursive(parent0, si, n);
142                traceCache.Add(t0);
143              } else {
144                n.Weight++;
145              }
146              var t1 = new Tuple<IGenealogyGraphNode<ISymbolicExpressionTree>, IGenealogyGraphNode<ISymbolicExpressionTree>, int>(parent1, n, fragment.Index2);
147              if (!traceCache.Contains(t1)) {
148                TraceRecursive(parent1, fragment.Index2, n);
149                traceCache.Add(t1);
150              } else {
151                n.Weight++;
152              }
153              break;
154            } else {
155              // subtree and fragment are distinct.
156              g = parent0;
157              continue;
158            }
159          }
160        }
161        #endregion
162        #region trace mutation
163        // mutation is handled in a simple way: we branch every time there is an overlap between the subtree and the fragment
164        // (since mutation effects can be quite unpredictable: replace branch, change node, shake tree, etc)
165        if (inArcs.Count == 1) {
166          var parent0 = (IGenealogyGraphNode<ISymbolicExpressionTree>)inArcs[0].Source;
167          Debug.Assert(fragment.Index1 == fragment.Index2);
168          // check if the subtree and the fragment overlap => branch out
169          if ((si == fi) || (si < fi && fi < si + sl) || (fi < si && si < fi + fl)) {
170            var n = AddTraceNode(g, si, fi); // current node becomes "last" as we restart tracing from the parent
171            int i = si < fi ? si : fi;
172            var t = new Tuple<IGenealogyGraphNode<ISymbolicExpressionTree>, IGenealogyGraphNode<ISymbolicExpressionTree>, int>(parent0, n, i);
173            if (!traceCache.Contains(t)) {
174              TraceRecursive(parent0, i, n);
175              traceCache.Add(t);
176            } else {
177              n.Weight++;
178            }
179            break;
180          } else {
181            // if they don't overlap, go up
182            g = parent0;
183            if (fi < si)
184              si += NodeAt(g.Data, fi).GetLength() - fl;
185            continue;
186          }
187        }
188        #endregion
189        throw new InvalidOperationException("A node cannot have more than two parents");
190      }
191      // when we are out of the while the last vertex must be connected with the current one
192      // if there is no last vertex, it means the tracing reached the top of the genealogy graph
193      var current = AddTraceNode(g, si, fi);
194      if (last != null)
195        ConnectLast(current, last, si, fi);
196    }
197
198    /// <summary>
199    /// Get the trace node from the trace graph which corresponds to node g from the genealogy graph.
200    /// 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.
201    /// </summary>
202    /// <param name="g">The genealogy graph node</param>
203    /// <param name="si">The subtree index</param>
204    /// <param name="fi">The fragment index</param>
205    /// <returns></returns>
206    private IGenealogyGraphNode<ISymbolicExpressionTree> AddTraceNode(IGenealogyGraphNode<ISymbolicExpressionTree> g,
207      int si, int fi) {
208      var n = TraceGraph.GetByContent(g.Data);
209      if (n == null) {
210        n = g.Copy();
211        TraceGraph.AddVertex(n);
212        Debug.Assert(!traceMap.ContainsKey(n));
213        traceMap[n] = new TraceData(si, fi, -1, -1); // only the first two fields are needed
214      }
215      return n;
216    }
217
218    // caching node lists brings ~2.5-2.7x speed improvement (since graph nodes are visited multiple times)
219    // this caching will be even more effective with larger tree sizes
220    private ISymbolicExpressionTreeNode NodeAt(ISymbolicExpressionTree tree, int index) {
221      List<ISymbolicExpressionTreeNode> list;
222      nodeListCache.TryGetValue(tree, out list);
223      if (list == null) {
224        list = tree.IterateNodesPrefix().ToList();
225        nodeListCache[tree] = list;
226      }
227      return list[index];
228    }
229
230    /// <summary>
231    /// 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
232    /// of the genealogy graph node @g. 
233    /// </summary>
234    /// <param name="current">The current node in the genealogy graph</param>
235    /// <param name="last">The last added node in the trace graph</param>   
236    /// <param name="si">The index of the traced subtree</param>
237    /// <param name="fi">The index of the fragment</param>
238    private void ConnectLast(IGenealogyGraphNode<ISymbolicExpressionTree> current,
239      IGenealogyGraphNode<ISymbolicExpressionTree> last, int si, int fi) {
240      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)
245      var td = new TraceData(si, fi, lastSi, lastFi); // trace data
246      // using the inArcs seems to be slightly more efficient than using the outArcs
247      // TODO: more testing
248      var inArcs = (List<IArc>)((IVertex)last).InArcs;
249      var arc = inArcs.FirstOrDefault(a => a.Source == current && ((IArc<IDeepCloneable>)a).Data.Equals(td));
250      if (arc == null) {
251        arc = new GenealogyGraphArc(current, last) { Data = td };
252        TraceGraph.AddArc(arc);
253      }
254    }
255  }
256
257  public class TraceData : Tuple<int, int, int, int>, IDeepCloneable {
258    public TraceData(int currentSubtreeIndex, int currentFragmentIndex, int lastSubtreeIndex, int lastFragmentIndex)
259      : base(currentSubtreeIndex, currentFragmentIndex, lastSubtreeIndex, lastFragmentIndex) {
260    }
261
262    public int SubtreeIndex { get { return Item1; } }
263    public int FragmentIndex { get { return Item2; } }
264    public int LastSubtreeIndex { get { return Item3; } }
265    public int LastFragmentIndex { get { return Item4; } }
266    public object Clone() {
267      return new TraceData(SubtreeIndex, FragmentIndex, LastSubtreeIndex, LastFragmentIndex);
268    }
269
270    public IDeepCloneable Clone(Cloner cloner) {
271      return cloner.Clone(this);
272    }
273  }
274
275  internal static class Util {
276    // shallow node copy (does not clone the data or the arcs)
277    #region some helper methods for shortening the tracing code
278    public static IGenealogyGraphNode<ISymbolicExpressionTree> Copy(this IGenealogyGraphNode<ISymbolicExpressionTree> node) {
279      return new GenealogyGraphNode<ISymbolicExpressionTree>(node.Data) { Rank = node.Rank, Quality = node.Quality };
280    }
281    #endregion
282  }
283}
Note: See TracBrowser for help on using the repository browser.