[11493] | 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 |
|
---|
| 22 | using System;
|
---|
[11458] | 23 | using System.Collections.Generic;
|
---|
[11493] | 24 | using System.Diagnostics;
|
---|
[11458] | 25 | using System.Linq;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 29 | using HeuristicLab.EvolutionTracking;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 |
|
---|
| 32 | namespace 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 {
|
---|
[11751] | 36 | private IGenealogyGraph<ISymbolicExpressionTree> traceGraph;
|
---|
[11866] | 37 | private Dictionary<IGenealogyGraphNode<ISymbolicExpressionTree>, TraceData> traceMap;
|
---|
[11855] | 38 | private Dictionary<ISymbolicExpressionTree, List<ISymbolicExpressionTreeNode>> nodeListCache;
|
---|
[11458] | 39 |
|
---|
| 40 | public IGenealogyGraph<ISymbolicExpressionTree> TraceGraph { get { return traceGraph; } }
|
---|
| 41 |
|
---|
| 42 | public TraceCalculator() {
|
---|
| 43 | traceGraph = new GenealogyGraph<ISymbolicExpressionTree>();
|
---|
[11866] | 44 | traceMap = new Dictionary<IGenealogyGraphNode<ISymbolicExpressionTree>, TraceData>();
|
---|
[11855] | 45 | nodeListCache = new Dictionary<ISymbolicExpressionTree, List<ISymbolicExpressionTreeNode>>();
|
---|
[11458] | 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 |
|
---|
[11476] | 62 | public IGenealogyGraph<ISymbolicExpressionTree> Trace(IGenealogyGraphNode<ISymbolicExpressionTree> node, int subtreeIndex) {
|
---|
[11751] | 63 | traceGraph = new GenealogyGraph<ISymbolicExpressionTree>();
|
---|
[11866] | 64 | traceMap = new Dictionary<IGenealogyGraphNode<ISymbolicExpressionTree>, TraceData>();
|
---|
[11855] | 65 | nodeListCache = new Dictionary<ISymbolicExpressionTree, List<ISymbolicExpressionTreeNode>>();
|
---|
[11751] | 66 | TraceRecursive(node, subtreeIndex);
|
---|
[11476] | 67 | return traceGraph;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
[11473] | 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>
|
---|
[11751] | 87 | /// <param name="node">The current node in the genealogy graph</param>
|
---|
| 88 | /// <param name="subtreeIndex">The index of the traced subtree</param>
|
---|
[11473] | 89 | /// <param name="last">The last added node in the trace graph</param>
|
---|
[11751] | 90 | private void TraceRecursive(IGenealogyGraphNode<ISymbolicExpressionTree> node, int subtreeIndex, IGenealogyGraphNode<ISymbolicExpressionTree> last = null) {
|
---|
| 91 | var g = node;
|
---|
[11881] | 92 | int si = subtreeIndex; // subtree index
|
---|
| 93 | int fi = 0; // fragment index
|
---|
[11473] | 94 | while (g.Parents.Any()) {
|
---|
[11751] | 95 | Debug.Assert(si < g.Data.Length);
|
---|
[11858] | 96 | var inArcs = (List<IArc>)((IVertex)g).InArcs;
|
---|
| 97 | var fragment = (IFragment<ISymbolicExpressionTreeNode>)inArcs.Last().Data;
|
---|
[11458] | 98 | if (fragment == null) {
|
---|
[11855] | 99 | // TODO: think about what the correct behavior should be here (seems good so far)
|
---|
[11458] | 100 | // the node is either an elite node or (in rare cases) no fragment was transferred
|
---|
[11858] | 101 | g = (IGenealogyGraphNode<ISymbolicExpressionTree>)inArcs[0].Source;
|
---|
[11458] | 102 | continue;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[11881] | 105 | fi = fragment.Index1; // fragment index
|
---|
[11493] | 106 | int fl = fragment.Root.GetLength(); // fragment length
|
---|
[11855] | 107 | int sl = NodeAt(g.Data, si).GetLength(); // subtree length
|
---|
[11458] | 108 |
|
---|
| 109 | #region trace crossover
|
---|
[11858] | 110 | if (inArcs.Count == 2) {
|
---|
| 111 | var parent0 = (IGenealogyGraphNode<ISymbolicExpressionTree>)inArcs[0].Source;
|
---|
| 112 | var parent1 = (IGenealogyGraphNode<ISymbolicExpressionTree>)inArcs[1].Source;
|
---|
[11493] | 113 | if (fi == si) {
|
---|
[11858] | 114 | g = parent1;
|
---|
[11473] | 115 | si = fragment.Index2;
|
---|
[11458] | 116 | continue;
|
---|
| 117 | }
|
---|
[11493] | 118 | if (fi < si) {
|
---|
| 119 | if (fi + fl > si) {
|
---|
[11458] | 120 | // fragment contains subtree
|
---|
[11858] | 121 | g = parent1;
|
---|
[11493] | 122 | si += fragment.Index2 - fi;
|
---|
[11458] | 123 | } else {
|
---|
| 124 | // fragment distinct from subtree
|
---|
[11858] | 125 | g = parent0;
|
---|
[11855] | 126 | si += NodeAt(g.Data, fi).GetLength() - fl;
|
---|
[11458] | 127 | }
|
---|
| 128 | continue;
|
---|
| 129 | }
|
---|
[11493] | 130 | if (fi > si) {
|
---|
| 131 | if (fi < si + sl) {
|
---|
[11458] | 132 | // subtree contains fragment => branching point in the fragment graph
|
---|
[11881] | 133 | var n = AddTraceNode(g, si, fi); // current node becomes "last" as we restart tracing from the parent
|
---|
[11858] | 134 | TraceRecursive(parent0, si, n);
|
---|
| 135 | TraceRecursive(parent1, fragment.Index2, n);
|
---|
[11458] | 136 | break;
|
---|
| 137 | } else {
|
---|
| 138 | // subtree and fragment are distinct.
|
---|
[11858] | 139 | g = parent0;
|
---|
[11458] | 140 | continue;
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 | #endregion
|
---|
| 145 | #region trace mutation
|
---|
[11493] | 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)
|
---|
[11858] | 148 | if (inArcs.Count == 1) {
|
---|
| 149 | var parent0 = (IGenealogyGraphNode<ISymbolicExpressionTree>)inArcs[0].Source;
|
---|
[11493] | 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)) {
|
---|
[11881] | 153 | var n = AddTraceNode(g, si, fi); // current node becomes "last" as we restart tracing from the parent
|
---|
[11493] | 154 | int i = si < fi ? si : fi;
|
---|
[11858] | 155 | TraceRecursive(parent0, i, n);
|
---|
[11473] | 156 | break;
|
---|
[11493] | 157 | } else {
|
---|
| 158 | // if they don't overlap, go up
|
---|
[11858] | 159 | g = parent0;
|
---|
[11493] | 160 | if (fi < si)
|
---|
[11855] | 161 | si += NodeAt(g.Data, fi).GetLength() - fl;
|
---|
[11473] | 162 | continue;
|
---|
[11458] | 163 | }
|
---|
[11473] | 164 | }
|
---|
[11458] | 165 | #endregion
|
---|
[11473] | 166 | throw new InvalidOperationException("A node cannot have more than two parents");
|
---|
[11458] | 167 | }
|
---|
[11473] | 168 | // when we are out of the while the last vertex must be connected with the current one
|
---|
[11881] | 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);
|
---|
[11458] | 173 | }
|
---|
[11473] | 174 |
|
---|
| 175 | /// <summary>
|
---|
[11493] | 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>
|
---|
[11881] | 183 | private IGenealogyGraphNode<ISymbolicExpressionTree> AddTraceNode(IGenealogyGraphNode<ISymbolicExpressionTree> g, int si, int fi) {
|
---|
[11493] | 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));
|
---|
[11866] | 189 | traceMap[n] = new TraceData(si, fi, -1, -1); // only the first two fields are needed
|
---|
[11493] | 190 | }
|
---|
| 191 | return n;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
[11855] | 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 |
|
---|
[11493] | 206 | /// <summary>
|
---|
[11473] | 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>
|
---|
[11881] | 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>
|
---|
[11473] | 212 | /// <param name="si">The index of the traced subtree</param>
|
---|
[11881] | 213 | /// <param name="fi">The index of the fragment</param>
|
---|
| 214 | private void ConnectLast(IGenealogyGraphNode<ISymbolicExpressionTree> current, IGenealogyGraphNode<ISymbolicExpressionTree> last, int si, int fi) {
|
---|
[11473] | 215 | var lastTraceData = traceMap[last];
|
---|
[11866] | 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
|
---|
[11881] | 219 | var arc = current.OutArcs.SingleOrDefault(a => a.Target == last && a.Data.Equals(td));
|
---|
[11503] | 220 | if (arc == null) {
|
---|
[11881] | 221 | arc = new GenealogyGraphArc(current, last) { Data = td };
|
---|
[11503] | 222 | traceGraph.AddArc(arc);
|
---|
| 223 | }
|
---|
[11473] | 224 | }
|
---|
[11458] | 225 | }
|
---|
[11866] | 226 | // this class is here to help clarify the semantics
|
---|
[11881] | 227 | public class TraceData : Tuple<int, int, int, int> {
|
---|
[11866] | 228 | public TraceData(int currentSubtreeIndex, int currentFragmentIndex, int lastSubtreeIndex, int lastFragmentIndex)
|
---|
| 229 | : base(currentSubtreeIndex, currentFragmentIndex, lastSubtreeIndex, lastFragmentIndex) {
|
---|
| 230 | }
|
---|
[11458] | 231 |
|
---|
[11866] | 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 |
|
---|
[11458] | 238 | internal static class Util {
|
---|
[11473] | 239 | // shallow node copy (does not clone the data or the arcs)
|
---|
[11751] | 240 | #region some helper methods for shortening the tracing code
|
---|
[11458] | 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 | }
|
---|