Changeset 10677
- Timestamp:
- 03/28/14 16:23:40 (11 years ago)
- Location:
- branches/HeuristicLab.EvolutionTracking
- Files:
-
- 16 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionTreeTile.cs
r10655 r10677 58 58 } 59 59 public ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode> LayoutEngine { get; set; } 60 private Dictionary<IPrimitive, ISymbolicExpressionTreeNode> primitiveMap;60 private readonly Dictionary<IPrimitive, ISymbolicExpressionTreeNode> primitiveMap; 61 61 62 62 public SymbolicExpressionTreeTile(IChart chart) 63 63 : base(chart) { 64 64 primitiveMap = new Dictionary<IPrimitive, ISymbolicExpressionTreeNode>(); 65 66 Group = new Group(chart); 65 67 } 66 68 public SymbolicExpressionTreeTile(IChart chart, ISymbolicExpressionTree tree) 67 69 : this(chart) { 68 SymbolicExpressionTree = tree;69 70 PreferredNodeWidth = 80; 70 71 PreferredNodeHeight = 40; 72 SymbolicExpressionTree = tree; 71 73 } 72 74 private void GeneratePrimitives(double preferredNodeWidth, double preferredNodeHeight) { 73 75 Clear(); 74 ISymbolicExpressionTreeNode root = Root;75 if ( root.Symbol is ProgramRootSymbol && root.SubtreeCount == 1) { root = root.GetSubtree(0); }76 var visualNodes = LayoutEngine.CalculateLayout( root);76 var actualRoot = Root; 77 if (Root.Symbol is ProgramRootSymbol && Root.SubtreeCount == 1) { actualRoot = Root.GetSubtree(0); } 78 var visualNodes = LayoutEngine.CalculateLayout(actualRoot); 77 79 78 80 var font = new Font(FontFamily.GenericSansSerif, 10, GraphicsUnit.Pixel); -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Analyzers/GenealogyAnalyzer.cs
r10675 r10677 230 230 previousVertex.AddForwardArc(vertex); 231 231 vertex.AddReverseArc(previousVertex); 232 vertex.InArcs.Last().Data = previousVertex.InArcs.Last().Data; // save the fragment in case there is one 233 vertex.Id = previousVertex.Id; 232 vertex.Id = previousVertex.Id; // another way would be to introduce the vertex.Id into the scope of the elite 233 if (previousVertex.InArcs.Any()) { 234 vertex.InArcs.Last().Data = previousVertex.InArcs.Last().Data; // save the fragment in case there is one 235 } 234 236 } 235 237 } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/DirectedGraph/DirectedGraph.cs
r10300 r10677 77 77 public virtual void Clear() { 78 78 nodes.Clear(); 79 contentMap.Clear(); 79 80 } 80 81 public virtual void AddVertex(IVertex vertex) { -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/DirectedGraph/Interfaces/IVertex.cs
r10650 r10677 26 26 public interface IVertex : IItem { 27 27 string Id { get; } 28 List<IArc> InArcs { get; }29 List<IArc> OutArcs { get; }28 IEnumerable<IArc> InArcs { get; set; } 29 IEnumerable<IArc> OutArcs { get; set; } 30 30 31 31 int InDegree { get; } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/DirectedGraph/Vertex.cs
r10300 r10677 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Linq; 24 25 using HeuristicLab.Common; 25 26 using HeuristicLab.Core; … … 43 44 [Storable] 44 45 private List<IArc> inArcs; 45 public List<IArc> InArcs { get { return inArcs; } protected set { inArcs = value; } } 46 public IEnumerable<IArc> InArcs { 47 get { return inArcs ?? Enumerable.Empty<IArc>(); } 48 set { inArcs = value.ToList(); } 49 } 46 50 [Storable] 47 51 private List<IArc> outArcs; 48 public List<IArc> OutArcs { get { return outArcs; } protected set { outArcs = value; } } 52 public IEnumerable<IArc> OutArcs { 53 get { return outArcs ?? Enumerable.Empty<IArc>(); } 54 set { outArcs = value.ToList(); } 55 } 49 56 50 57 [StorableConstructor] … … 61 68 Id = Guid.NewGuid().ToString(); 62 69 Label = original.Label; 63 InArcs = new List<IArc>(original.InArcs);64 OutArcs = new List<IArc>(original.OutArcs);70 inArcs = new List<IArc>(original.InArcs); 71 outArcs = new List<IArc>(original.OutArcs); 65 72 } 66 73 … … 81 88 public void AddForwardArc(IVertex target, double w = 0.0, object data = null) { 82 89 var arc = new Arc { Source = this, Target = target, Data = data, Weight = w }; 83 if ( OutArcs == null) OutArcs = new List<IArc> { arc };84 else OutArcs.Add(arc);90 if (outArcs == null) outArcs = new List<IArc> { arc }; 91 else outArcs.Add(arc); 85 92 } 86 93 public void AddForwardArc(IArc arc) { 87 94 if (arc.Source == null) { arc.Source = this; } 88 95 if (arc.Source != this) { throw new Exception("AddForwardArc: Source should be equal to this."); } 89 if (OutArcs == null) { OutArcs = new List<IArc> { arc }; } else { OutArcs.Add(arc); } 96 if (outArcs == null) { outArcs = new List<IArc> { arc }; } else { outArcs.Add(arc); } 97 } 98 public void AddReverseArc(IVertex source, double w = 0.0, object data = null) { 99 var arc = new Arc { Source = source, Target = this, Data = data, Weight = w }; 100 if (inArcs == null) inArcs = new List<IArc> { arc }; 101 else inArcs.Add(arc); 90 102 } 91 103 public void AddReverseArc(IArc arc) { 92 104 if (arc.Target == null) { arc.Target = this; } 93 105 if (arc.Target != this) { throw new Exception("AddReverseArc: Target should be equal to this."); }; 94 if (InArcs == null) { InArcs = new List<IArc> { arc }; } else { InArcs.Add(arc); } 95 } 96 public void AddReverseArc(IVertex source, double w = 0.0, object data = null) { 97 var arc = new Arc { Source = source, Target = this, Data = data, Weight = w }; 98 if (InArcs == null) InArcs = new List<IArc> { arc }; 99 else InArcs.Add(arc); 106 if (inArcs == null) { inArcs = new List<IArc> { arc }; } else { inArcs.Add(arc); } 100 107 } 101 108 102 public int InDegree { get { return InArcs == null ? 0 : InArcs.Count; } }103 public int OutDegree { get { return OutArcs == null ? 0 : OutArcs.Count; } }109 public int InDegree { get { return InArcs.Count(); } } 110 public int OutDegree { get { return OutArcs.Count(); } } 104 111 public int Degree { get { return InDegree + OutDegree; } } 105 112 } 106 107 113 108 114 [StorableClass] -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/GenealogyGraph.cs
r10650 r10677 37 37 set { ranks = value; } 38 38 } 39 public new IEnumerable<IGenealogyGraphNode> Nodes { 40 get { return from n in base.Nodes select (IGenealogyGraphNode)n; } 41 } 39 42 protected GenealogyGraph(GenealogyGraph original, Cloner cloner) 40 43 : base(original, cloner) { … … 74 77 } 75 78 } 79 80 public override void Clear() { 81 base.Clear(); 82 ranks.Clear(); 83 } 76 84 } 77 85 … … 79 87 [Item("GenealogyGraph", "")] 80 88 public class GenealogyGraph<T> : DirectedGraph, IGenealogyGraph<T> where T : class, IItem { 89 // members and properties 81 90 [Storable] 82 91 private Dictionary<double, LinkedList<IGenealogyGraphNode>> ranks; … … 85 94 set { ranks = value; } 86 95 } 96 public new IEnumerable<IGenealogyGraphNode<T>> Nodes { 97 get { return from n in base.Nodes select (IGenealogyGraphNode<T>)n; } 98 } 99 // contructors 87 100 protected GenealogyGraph(GenealogyGraph<T> original, Cloner cloner) 88 101 : base(original, cloner) { … … 96 109 Ranks = new Dictionary<double, LinkedList<IGenealogyGraphNode>>(); 97 110 } 111 // methods 98 112 public override void AddVertex(IVertex vertex) { 99 113 var node = (IGenealogyGraphNode<T>)vertex; … … 110 124 base.RemoveVertex(vertex); 111 125 } 126 127 IEnumerable<IGenealogyGraphNode> IGenealogyGraph.Nodes { 128 get { return Nodes; } 129 } 130 112 131 public event EventHandler GraphUpdated; 113 132 private void OnGraphUpdated(object sender, EventArgs args) { … … 115 134 if (updated != null) updated(sender, args); 116 135 } 117 118 136 public new List<IGenealogyGraphNode<T>> this[object content] { 119 137 get { -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/GenealogyGraphNode.cs
r10300 r10677 41 41 public GenealogyGraphNode() { } 42 42 43 public new List<IGenealogyGraphArc> InArcs {43 public new IEnumerable<IGenealogyGraphArc> InArcs { 44 44 get { 45 return base.InArcs == null ? null : base.InArcs.Cast<IGenealogyGraphArc>().ToList();45 return base.InArcs.Cast<IGenealogyGraphArc>().ToList(); 46 46 } 47 set { base.InArcs = value; } 47 48 } 48 public new List<IGenealogyGraphArc> OutArcs {49 public new IEnumerable<IGenealogyGraphArc> OutArcs { 49 50 get { 50 return base.OutArcs == null ? null : base.OutArcs.Cast<IGenealogyGraphArc>().ToList();51 return base.OutArcs.Cast<IGenealogyGraphArc>().ToList(); 51 52 } 53 set { base.InArcs = value; } 52 54 } 53 public IEnumerable<IGenealogyGraphNode> Ancestors (){54 // for performance, we use a hashset for lookup and a list for iteration55 var nodes = new HashSet<IGenealogyGraphNode> { this };56 var list = new List<IGenealogyGraphNode> { this };57 int i = 0;58 while (i != list.Count) {59 if (list[i].InArcs != null) {55 public IEnumerable<IGenealogyGraphNode> Ancestors { 56 get { 57 // for performance, we use a hashset for lookup and a list for iteration 58 var nodes = new HashSet<IGenealogyGraphNode> { this }; 59 var list = new List<IGenealogyGraphNode> { this }; 60 int i = 0; 61 while (i != list.Count) { 60 62 foreach (var e in list[i].InArcs) { 61 63 if (nodes.Contains(e.Source)) continue; … … 63 65 list.Add(e.Source); 64 66 } 67 ++i; 65 68 } 66 ++i;69 return list; 67 70 } 68 return list;69 71 } 70 72 /// <summary> … … 72 74 /// </summary> 73 75 /// <returns>All the descendants of the current node</returns> 74 public IEnumerable<IGenealogyGraphNode> Descendants (){75 var nodes = new HashSet<IGenealogyGraphNode> { this };76 var list = new List<IGenealogyGraphNode> { this };77 int i = 0;78 while (i != list.Count) {79 if (list[i].OutArcs != null) {76 public IEnumerable<IGenealogyGraphNode> Descendants { 77 get { 78 var nodes = new HashSet<IGenealogyGraphNode> { this }; 79 var list = new List<IGenealogyGraphNode> { this }; 80 int i = 0; 81 while (i != list.Count) { 80 82 foreach (var e in list[i].OutArcs) { 81 83 if (nodes.Contains(e.Target)) continue; … … 83 85 list.Add(e.Target); 84 86 } 87 ++i; 85 88 } 86 ++i;89 return list; 87 90 } 88 return list;89 91 } 90 92 [Storable] … … 118 120 base.AddReverseArc(arc); 119 121 } 122 123 public IEnumerable<IGenealogyGraphNode> Parents { 124 get { 125 return InArcs == null ? Enumerable.Empty<IGenealogyGraphNode>() : InArcs.Select(a => a.Source); 126 } 127 } 128 129 public IEnumerable<IGenealogyGraphNode> Children { 130 get { return OutArcs == null ? Enumerable.Empty<IGenealogyGraphNode>() : OutArcs.Select(a => a.Source); } 131 } 120 132 } 121 133 … … 135 147 return new GenealogyGraphNode<T>(this, cloner); 136 148 } 149 150 public new IEnumerable<IGenealogyGraphNode<T>> Parents { 151 get { 152 return base.Parents.Select(p => (IGenealogyGraphNode<T>)p); 153 } 154 } 155 156 public new IEnumerable<IGenealogyGraphNode<T>> Children { 157 get { 158 return base.Children.Select(c => (IGenealogyGraphNode<T>)c); 159 } 160 } 161 162 public new IEnumerable<IGenealogyGraphNode<T>> Ancestors { 163 get { return base.Ancestors.Select(x => (IGenealogyGraphNode<T>)x); } 164 } 165 166 public new IEnumerable<IGenealogyGraphNode<T>> Descendants { 167 get { return base.Descendants.Select(x => (IGenealogyGraphNode<T>)x); } 168 } 137 169 } 138 170 } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/Interfaces/IGenealogyGraph.cs
r10650 r10677 26 26 public interface IGenealogyGraph : IDirectedGraph { 27 27 Dictionary<double, LinkedList<IGenealogyGraphNode>> Ranks { get; } 28 new IEnumerable<IGenealogyGraphNode> Nodes { get; } 28 29 } 29 30 30 public interface IGenealogyGraph<T> : IGenealogyGraph where T : class,IItem { } 31 public interface IGenealogyGraph<T> : IGenealogyGraph where T : class, IItem { 32 new IEnumerable<IGenealogyGraphNode<T>> Nodes { get; } 33 } 31 34 } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/Interfaces/IGenealogyGraphNode.cs
r10650 r10677 22 22 using System; 23 23 using System.Collections.Generic; 24 using HeuristicLab.Core;25 24 26 25 namespace HeuristicLab.EvolutionTracking { 27 26 public interface IGenealogyGraphNode : IVertex, IComparable<IGenealogyGraphNode> { 28 IEnumerable<IGenealogyGraphNode> Ancestors();29 IEnumerable<IGenealogyGraphNode> Descendants();30 new List<IGenealogyGraphArc> InArcs { get; }31 new List<IGenealogyGraphArc> OutArcs { get; }32 33 27 double Rank { get; set; } 34 28 double Quality { get; set; } 35 29 bool IsElite { get; set; } 30 31 IEnumerable<IGenealogyGraphNode> Parents { get; } 32 IEnumerable<IGenealogyGraphNode> Children { get; } 33 IEnumerable<IGenealogyGraphNode> Ancestors { get; } 34 IEnumerable<IGenealogyGraphNode> Descendants { get; } 35 new IEnumerable<IGenealogyGraphArc> InArcs { get; set; } 36 new IEnumerable<IGenealogyGraphArc> OutArcs { get; set; } 36 37 } 37 38 38 public interface IGenealogyGraphNode<T> : IGenealogyGraphNode, IVertex<T> where T : class { } 39 public interface IGenealogyGraphNode<T> : IGenealogyGraphNode, IVertex<T> where T : class { 40 new IEnumerable<IGenealogyGraphNode<T>> Parents { get; } 41 new IEnumerable<IGenealogyGraphNode<T>> Children { get; } 42 43 new IEnumerable<IGenealogyGraphNode<T>> Ancestors { get; } 44 new IEnumerable<IGenealogyGraphNode<T>> Descendants { get; } 45 } 39 46 } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Operators/BeforeManipulatorOperator.cs
r10650 r10677 20 20 #endregion 21 21 22 using System.Collections.Generic; 22 23 using System.Linq; 23 24 using HeuristicLab.Common; … … 55 56 // adjust parent-child(clone) relationship in the graph 56 57 var parents = vChild.InArcs.Select(a => a.Source); 57 vChild.InArcs .Clear();58 vChild.InArcs = new List<IGenealogyGraphArc>(); 58 59 foreach (var p in parents) { 59 60 foreach (var a in p.OutArcs) { -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/FragmentGraphView.cs
r10656 r10677 20 20 public new IGenealogyGraph<IFragment<ISymbolicExpressionTreeNode>> Content { 21 21 get { return (IGenealogyGraph<IFragment<ISymbolicExpressionTreeNode>>)base.Content; } 22 set { 23 base.Content = value; 24 MakeTiles(); 25 Draw(); 26 } 22 set { base.Content = value; } 27 23 } 28 24 … … 38 34 var chart = symbolicExpressionChartControl.Chart; 39 35 tileDictionary.Clear(); 40 foreach (var node in Content.Nodes .Cast<IGenealogyGraphNode<IFragment<ISymbolicExpressionTreeNode>>>()) {36 foreach (var node in Content.Nodes) { 41 37 var tile = new SymbolicExpressionTreeTile(chart); 42 38 tile.LayoutEngine = symbolicExpressionEngine; … … 48 44 } 49 45 50 foreach (var node in Content.Nodes.Where(n => n.InArcs != null && n.InArcs.Count > 0)) { 51 var graphNode = (IGenealogyGraphNode<IFragment<ISymbolicExpressionTreeNode>>)node;52 var layoutNode = tileDictionary[ graphNode];46 47 foreach (var node in Content.Nodes.Where(n => n.Children.Any())) { 48 var layoutNode = tileDictionary[node]; 53 49 layoutNode.Children = new List<TileLayoutNode>(); 54 foreach (var c in node.InArcs.Select(x => x.Source as IGenealogyGraphNode<IFragment<ISymbolicExpressionTreeNode>>)) {55 layoutNode.Children.Add(tileDictionary[c ]);50 foreach (var child in node.Children) { 51 layoutNode.Children.Add(tileDictionary[child]); 56 52 } 57 53 } … … 59 55 60 56 private void Draw() { 61 var root = tileDictionary[(IGenealogyGraphNode<IFragment<ISymbolicExpressionTreeNode>>)Content.Nodes[0]]; 62 var visualNodes = layoutEngine.CalculateLayout(root); 57 var root = Content.Nodes.First(); 58 59 var fragmentRoot = tileDictionary[root]; 60 var visualNodes = layoutEngine.CalculateLayout(fragmentRoot); 63 61 64 62 symbolicExpressionChartControl.UpdateEnabled = false; … … 82 80 83 81 #region Event Handlers (Content) 84 85 82 // TODO: Put event handlers of the content here 86 87 #endregion88 89 83 protected override void OnContentChanged() { 90 84 base.OnContentChanged(); 91 if (Content == null) { 92 // TODO: Add code when content has been changed and is null 93 } else { 94 // TODO: Add code when content has been changed and is not null 85 if (Content != null) { 86 MakeTiles(); 87 Draw(); 95 88 } 96 89 } 97 90 #endregion 98 91 99 92 protected override void SetEnabledStateOfControls() { -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/SymboldDataAnalysisGenealogyView.cs
r10674 r10677 23 23 using System.Collections.Generic; 24 24 using System.Drawing; 25 using System.IO; 25 26 using System.Linq; 26 27 using System.Windows.Forms; … … 34 35 using FragmentGraph = HeuristicLab.EvolutionTracking.IGenealogyGraph<HeuristicLab.EvolutionTracking.IFragment<HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ISymbolicExpressionTreeNode>>; 35 36 36 37 37 using FragmentNode = HeuristicLab.EvolutionTracking.GenealogyGraphNode<HeuristicLab.EvolutionTracking.IFragment<HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ISymbolicExpressionTreeNode>>; 38 38 … … 102 102 // perform fragment tracing 103 103 var fragmentGraph = TraceSubtree(subtree); 104 var fragmentGraphView = MainFormManager.CreateView(typeof(FragmentGraphView)); 105 fragmentGraphView.Content = fragmentGraph; 106 fragmentGraphView.Show(); 104 MainFormManager.MainForm.ShowContent(fragmentGraph); 105 // var fragmentGraphView = MainFormManager.MainForm.ShowContent(fragmentGraph); 106 // fragmentGraphView.Content = fragmentGraph; 107 // fragmentGraphView.Show(); 107 108 108 109 // open a FragmentGraphView displaying this fragmentGraph … … 166 167 167 168 #region fragment tracing 169 #region helper methods to shorten the code 170 171 #endregion 172 168 173 private FragmentGraph TraceSubtree(ISymbolicExpressionTreeNode subtree) { 169 174 var graphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)genealogyGraphChart.SelectedGraphNode; … … 174 179 175 180 private void Trace(IGenealogyGraphNode<ISymbolicExpressionTree> graphNode, ISymbolicExpressionTreeNode subtree, FragmentGraph fragmentGraph, FragmentNode parentNode = null) { 176 if (graphNode.InArcs == null) return; 177 // the subtree must belong to the currently displayed tree which in turn must belong to the currently selected graph node 178 var tree = graphNode.Content; 179 var subtreeIndex = tree.IterateNodesPrefix().ToList().IndexOf(subtree); 180 var subtreeLength = subtree.GetLength(); 181 182 var fragment = (IFragment<ISymbolicExpressionTreeNode>)graphNode.InArcs.Last().Data; 183 if (fragment == null) return; 184 var fragmentLength = fragment.Root.GetLength(); 185 186 FragmentNode node = new FragmentNode { Content = new Fragment<ISymbolicExpressionTreeNode> { Root = subtree }, Rank = graphNode.Rank }; 187 if (parentNode != null) { 188 AddChild(parentNode, node); 189 } 190 fragmentGraph.AddVertex(node); 191 192 // below, we consider three cases: 193 // 1) the selected subtree is the same as the fragment 194 // 2) the fragment contains the selected subtree 195 // 3) the fragment is contained by the selected subtree 196 // In each case, the idea is to isolate and individually track the fragment and the rest of the subtree (for cases 2 and 3) 197 if (fragment.Index == subtreeIndex) { 198 // the selected subtree is the actual fragment 199 if (fragmentLength != subtreeLength) throw new Exception("Fragment and subtree lengths should be the same!"); 200 201 var g = (IGenealogyGraphNode<ISymbolicExpressionTree>)graphNode.InArcs.Last().Source; 202 var s = g.Content.IterateNodesPrefix().ToList()[fragment.OldIndex]; 203 Trace(g, s, fragmentGraph, node); 204 205 } else if (fragment.Index < subtreeIndex && subtreeIndex < fragment.Index + fragmentLength) { 206 // the fragment contains the selected subtree 207 if (subtreeLength >= fragmentLength) throw new Exception("Fragment contains subtree, so subtree length should be less than the fragment length."); 208 209 var g = (IGenealogyGraphNode<ISymbolicExpressionTree>)graphNode.InArcs.Last().Source; 210 var i = fragment.Root.IterateNodesPrefix().ToList().IndexOf(subtree); // get the index of the selected subtree, relative to the fragment root 211 var s = g.Content.IterateNodesPrefix().ToList()[fragment.OldIndex + i]; 212 Trace(g, s, fragmentGraph, node); 213 214 } else if (subtreeIndex < fragment.Index && fragment.Index < subtreeIndex + subtreeLength) { 215 // the selected subtree contains the fragment 216 if (fragmentLength >= subtreeLength) throw new Exception("Subtree contains fragment, so fragment length should be less than the subtree length."); 217 218 var g0 = (IGenealogyGraphNode<ISymbolicExpressionTree>)graphNode.InArcs[0].Source; 219 var s0 = g0.Content.IterateNodesPrefix().ToList()[subtreeIndex]; 220 Trace(g0, s0, fragmentGraph, node); 221 222 if (graphNode.InArcs.Count > 1) { 223 var g1 = (IGenealogyGraphNode<ISymbolicExpressionTree>)graphNode.InArcs[1].Source; 224 var s1 = g1.Content.IterateNodesPrefix().ToList()[fragment.OldIndex]; 225 Trace(g1, s1, fragmentGraph, node); 226 } 227 } else { 228 // fragment and subtree are completely distinct, therefore we will track the subtree 229 var g = (IGenealogyGraphNode<ISymbolicExpressionTree>)graphNode.InArcs[0].Source; 230 var s = graphNode.Content.IterateNodesPrefix().ToList()[subtreeIndex]; 231 Trace(g, s, fragmentGraph, node); 181 while (true) { 182 if (!graphNode.InArcs.Any()) return; 183 184 var parentVertices = graphNode.Parents.ToList(); 185 // the subtree must belong to the currently displayed tree which in turn must belong to the currently selected graph node 186 var tree = graphNode.Content; 187 var subtreeIndex = tree.IndexOf(subtree); 188 var subtreeLength = subtree.GetLength(); 189 190 var fragment = (IFragment<ISymbolicExpressionTreeNode>)graphNode.InArcs.Last().Data; 191 if (fragment == null) return; 192 var fragmentLength = fragment.Root.GetLength(); 193 194 FragmentNode node = new FragmentNode { Content = new Fragment<ISymbolicExpressionTreeNode> { Root = subtree }, Rank = graphNode.Rank }; 195 if (parentNode != null) { 196 AddChild(parentNode, node); 197 } 198 fragmentGraph.AddVertex(node); 199 200 // if the selected subtree is the actual fragment 201 if (fragment.Index == subtreeIndex) { 202 if (fragmentLength != subtreeLength) throw new Exception("Fragment and subtree lengths should be the same!"); 203 graphNode = parentVertices.Last(); 204 tree = graphNode.Content; 205 subtree = tree.NodeAt(fragment.OldIndex); 206 parentNode = node; 207 continue; 208 } 209 // if the fragment contains the selected subtree => track fragment, then track subtree 210 if (fragment.Index < subtreeIndex && subtreeIndex < fragment.Index + fragmentLength) { 211 if (subtreeLength >= fragmentLength) throw new Exception("Fragment contains subtree, so subtree length should be less than the fragment length."); 212 213 graphNode = parentVertices.Last(); 214 tree = graphNode.Content; 215 var i = fragment.Root.IndexOf(subtree); // get the index of the selected subtree, relative to the fragment root 216 subtree = tree.NodeAt(fragment.OldIndex + i); 217 parentNode = node; 218 continue; 219 } 220 // if the selected subtree contains the fragment => track fragment and subtree 221 if (subtreeIndex < fragment.Index && fragment.Index < subtreeIndex + subtreeLength) { 222 if (fragmentLength >= subtreeLength) throw new Exception("Subtree contains fragment, so fragment length should be less than the subtree length."); 223 224 graphNode = parentVertices[0]; 225 tree = graphNode.Content; 226 subtree = tree.NodeAt(subtreeIndex); 227 // track subtree 228 Trace(graphNode, subtree, fragmentGraph, node); 229 230 // track fragment 231 if (parentVertices.Count > 1) { 232 graphNode = parentVertices[1]; 233 tree = graphNode.Content; 234 subtree = tree.NodeAt(fragment.OldIndex); 235 parentNode = node; 236 continue; 237 } 238 } else { 239 // fragment and subtree are completely distinct => we only track the subtree 240 graphNode = parentVertices[0]; 241 tree = graphNode.Content; 242 subtree = tree.NodeAt(subtreeIndex); 243 parentNode = node; 244 continue; 245 } 246 break; 232 247 } 233 248 } … … 240 255 #endregion 241 256 } 257 258 internal static class Util { 259 private static string ViewAsText(this ISymbolicExpressionTreeNode root) { 260 var writer = new StringWriter(); 261 SymbolicExpressionTreeHierarchicalFormatter.RenderNode(writer, root, string.Empty); 262 return writer.ToString(); 263 } 264 internal static ISymbolicExpressionTreeNode NodeAt(this ISymbolicExpressionTree tree, int position) { 265 return NodeAt(tree.Root, position); 266 } 267 internal static ISymbolicExpressionTreeNode NodeAt(this ISymbolicExpressionTreeNode root, int position) { 268 return root.IterateNodesPrefix().ElementAt(position); 269 } 270 internal static int IndexOf(this ISymbolicExpressionTree tree, ISymbolicExpressionTreeNode node) { 271 return IndexOf(tree.Root, node); 272 } 273 internal static int IndexOf(this ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode node) { 274 return root.IterateNodesPrefix().ToList().IndexOf(node); // not too worried about efficiency here 275 } 276 } 242 277 } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SymbolicDataAnalysisExpressionAfterCrossoverOperator.cs
r10654 r10677 10 10 public override IOperation Apply() { 11 11 var child = ChildParameter.ActualValue; 12 var childVertex = GenealogyGraph[child].Last(); 13 var arc0 = (IGenealogyGraphArc)childVertex.InArcs[0]; 14 var arc1 = (IGenealogyGraphArc)childVertex.InArcs[1]; 15 var nodes0 = (List<ISymbolicExpressionTreeNode>)arc0.Data; 16 var nodes1 = (List<ISymbolicExpressionTreeNode>)arc1.Data; 12 var childVertex = (IGenealogyGraphNode)GenealogyGraph[child].Last(); 13 var arcs = childVertex.InArcs.ToList(); 14 var nodes0 = (List<ISymbolicExpressionTreeNode>)arcs[0].Data; 15 var nodes1 = (List<ISymbolicExpressionTreeNode>)arcs[1].Data; 17 16 var childNodes = child.IterateNodesPrefix().ToList(); 18 17 IFragment<ISymbolicExpressionTreeNode> fragment = null; … … 28 27 if (fragment == null) throw new Exception("Could not determine fragment!"); 29 28 30 arc 0.Data = null;31 arc 1.Data = fragment;29 arcs[0].Data = null; 30 arcs[1].Data = fragment; 32 31 33 32 return base.Apply(); -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SymbolicDataAnalysisExpressionAfterManipulatorOperator.cs
r10650 r10677 20 20 public override IOperation Apply() { 21 21 var vChild = (IGenealogyGraphNode<ISymbolicExpressionTree>)GenealogyGraph[ChildParameter.ActualValue].First(); 22 var nodesBefore = (List<ISymbolicExpressionTreeNode>)vChild.InArcs [0].Data;22 var nodesBefore = (List<ISymbolicExpressionTreeNode>)vChild.InArcs.First().Data; 23 23 var nodesAfter = ChildParameter.ActualValue.IterateNodesBreadth().ToList(); 24 24 IFragment<ISymbolicExpressionTreeNode> fragment = null; … … 32 32 } 33 33 34 vChild.InArcs [0].Data = fragment;34 vChild.InArcs.First().Data = fragment; 35 35 return base.Apply(); 36 36 } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SymbolicDataAnalysisExpressionBeforeCrossoverOperator.cs
r10674 r10677 32 32 var parents = ParentsParameter.ActualValue.ToList(); 33 33 var childVertex = (IGenealogyGraphNode<ISymbolicExpressionTree>)GenealogyGraph[parents[0]].Last(); // use the parent since it is actually the child before crossover (and the ChildParameter doesn't have a value yet) 34 var arcs = childVertex.InArcs.ToList(); 34 35 35 36 for (int i = 0; i < parents.Count; ++i) { 36 37 var nodes = parents[i].IterateNodesPrefix().ToList(); 37 var arc = childVertex.InArcs[i]; 38 arc.Data = nodes; 38 arcs[i].Data = nodes; 39 39 } 40 40 var parentVertices = childVertex.InArcs.Select(x => (IGenealogyGraphNode<ISymbolicExpressionTree>)x.Source).ToList(); -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SymbolicDataAnalysisExpressionBeforeManipulatorOperator.cs
r10347 r10677 10 10 11 11 var vChild = (IGenealogyGraphNode<ISymbolicExpressionTree>)GenealogyGraph[ChildParameter.ActualValue].Last(); 12 vChild.InArcs [0].Data = vChild.Content.IterateNodesBreadth().ToList();12 vChild.InArcs.First().Data = vChild.Content.IterateNodesBreadth().ToList(); 13 13 14 14 return result;
Note: See TracChangeset
for help on using the changeset viewer.