Changeset 10752
- Timestamp:
- 04/15/14 17:08:35 (11 years ago)
- Location:
- branches/HeuristicLab.EvolutionTracking
- Files:
-
- 1 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionTreeTile.cs
r10746 r10752 142 142 143 143 RectangularPrimitiveBase rectangularPrimitive; 144 var label = ShortenLabel(node); 144 145 if (node.SubtreeCount == 0) { 145 rectangularPrimitive = new Rectangle(Chart, lowerLeft, upperRight) { Font = font, Text = visualNode.Content.ToString()};146 rectangularPrimitive = new Rectangle(Chart, lowerLeft, upperRight) { Font = font, Text = label }; 146 147 } else { 147 rectangularPrimitive = new Ellipse(Chart, lowerLeft, upperRight) { Font = font, Text = visualNode.Content.ToString()};148 rectangularPrimitive = new Ellipse(Chart, lowerLeft, upperRight) { Font = font, Text = label }; 148 149 } 149 150 … … 202 203 base.Draw(graphics); 203 204 } 205 206 private string ShortenLabel(ISymbolicExpressionTreeNode node) { 207 var term = node as SymbolicExpressionTreeTerminalNode; 208 if (term != null) { 209 var parts = term.ToString().Split(' '); // split by space 210 if (parts.Length == 1) { 211 return parts[0].Substring(0, 5); 212 } else { 213 return parts[0].Substring(0, 5) + parts[1]; 214 } 215 } 216 return node.Symbol.ToString().Substring(0, 3); 217 } 204 218 } 205 219 } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/Tracking/FragmentGraphView.cs
r10746 r10752 93 93 var aPos = aTile.Position; 94 94 95 // mark swapped fragment 96 if (node.Content.Index != 0) { 95 if (node.Content.Index > 0) { 97 96 var subtree = node.Content.Root.NodeAt(node.Content.Index); 98 97 foreach (var s in subtree.IterateNodesPrefix()) { … … 102 101 if (rpb != null) { 103 102 rpb.Pen = new Pen(Color.RoyalBlue); 104 }105 }106 }107 }108 109 // mark the subtree that was replaced by the fragment110 if (node.Children.Any() && node.Content.Index != 0) {111 var child = node.Children.First();112 {113 var subtree = child.Content.Root.NodeAt(node.Content.Index);114 var tile = tileDictionary[child].Tile;115 foreach (var s in subtree.IterateNodesPrefix()) {116 var primitive = tile.GetPrimitive(s);117 if (primitive != null) {118 var rpb = primitive as RectangularPrimitiveBase;119 if (rpb != null) {120 rpb.Pen = new Pen(Color.DarkOrange);121 }122 103 } 123 104 } … … 133 114 symbolicExpressionChartControl.Add(line); 134 115 135 116 if (child == node.Children.First()) { 117 if (node.Content.Index > 0) { 118 var subtree = child.Content.Root.NodeAt(node.Content.Index); 119 foreach (var s in subtree.IterateNodesPrefix()) { 120 var primitive = bTile.GetPrimitive(s); 121 if (primitive != null) { 122 var rpb = primitive as RectangularPrimitiveBase; 123 if (rpb != null) { 124 rpb.Pen = new Pen(Color.DarkOrange); 125 } 126 } 127 } 128 } 129 } 136 130 } 137 131 } … … 173 167 } 174 168 169 internal static class Util { 170 internal static ISymbolicExpressionTreeNode NodeAt(this ISymbolicExpressionTree tree, int position) { 171 return NodeAt(tree.Root, position); 172 } 173 internal static ISymbolicExpressionTreeNode NodeAt(this ISymbolicExpressionTreeNode root, int position) { 174 return root.IterateNodesPrefix().ElementAt(position); 175 } 176 } 177 175 178 internal class TileLayoutNode { 176 179 public SymbolicExpressionTreeTile Tile { get; set; } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/Tracking/SymboldDataAnalysisGenealogyView.cs
r10746 r10752 20 20 #endregion 21 21 22 using System;23 22 using System.Collections.Generic; 24 23 using System.Drawing; 25 using System.IO;26 24 using System.Linq; 27 25 using System.Windows.Forms; … … 33 31 using HeuristicLab.MainForm; 34 32 35 using FragmentGraph = HeuristicLab.EvolutionTracking.IGenealogyGraph<HeuristicLab.EvolutionTracking.IFragment<HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ISymbolicExpressionTreeNode>>;36 using FragmentNode = HeuristicLab.EvolutionTracking.GenealogyGraphNode<HeuristicLab.EvolutionTracking.IFragment<HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ISymbolicExpressionTreeNode>>;37 33 38 34 namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views { … … 84 80 if (graphNode.InArcs.Any()) { 85 81 var fragment = (IFragment<ISymbolicExpressionTreeNode>)graphNode.InArcs.Last().Data; 86 treeChart_HighlightSubtree(fragment.Root); 82 if (fragment != null) { 83 treeChart_HighlightSubtree(fragment.Root); 84 } 87 85 } 88 86 } … … 100 98 if (trace) { 101 99 // perform fragment tracing 102 var fragmentGraph = TraceSubtree(subtree); 100 var graphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)genealogyGraphChart.SelectedGraphNode; 101 var subtreeIndex = graphNode.Content.IterateNodesPrefix().ToList().IndexOf(subtree); 102 var fragmentGraph = SymbolicDataAnalysisExpressionTracing.TraceSubtree(graphNode, subtreeIndex); 103 103 MainFormManager.MainForm.ShowContent(fragmentGraph); // display the fragment graph on the screen 104 104 } else { … … 159 159 } 160 160 #endregion 161 162 #region fragment tracing163 private FragmentGraph TraceSubtree(ISymbolicExpressionTreeNode subtree) {164 var graphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)genealogyGraphChart.SelectedGraphNode;165 var graph = new GenealogyGraph<IFragment<ISymbolicExpressionTreeNode>>();166 Trace(graphNode, subtree, graph);167 return graph;168 }169 170 private static void Trace(IGenealogyGraphNode<ISymbolicExpressionTree> graphNode, ISymbolicExpressionTreeNode subtree, FragmentGraph fragmentGraph, FragmentNode parentNode = null) {171 while (true) {172 if (!graphNode.InArcs.Any()) return;173 174 var parentVertices = graphNode.Parents.ToList();175 // the subtree must belong to the currently displayed tree which in turn must belong to the currently selected graph node176 var tree = graphNode.Content;177 var subtreeIndex = tree.IndexOf(subtree);178 var subtreeLength = subtree.GetLength();179 180 var fragment = (IFragment<ISymbolicExpressionTreeNode>)graphNode.InArcs.Last().Data;181 if (fragment == null) return;182 var fragmentLength = fragment.Root.GetLength();183 184 var fragmentNode = new FragmentNode {185 Content = new Fragment<ISymbolicExpressionTreeNode> {186 Root = subtree,187 },188 Rank = graphNode.Rank189 };190 191 if (parentNode != null) {192 if (parentNode == fragmentNode) {193 throw new Exception("Node cannot be a child of itself!");194 }195 var arc = new GenealogyGraphArc { Source = parentNode, Target = fragmentNode };196 parentNode.AddForwardArc(arc);197 }198 fragmentGraph.AddVertex(fragmentNode);199 200 parentNode = fragmentNode;201 202 // if the selected subtree is the actual fragment203 if (fragment.Index == subtreeIndex) {204 if (fragmentLength != subtreeLength) throw new Exception("Fragment and subtree lengths should be the same!");205 graphNode = parentVertices.Last();206 tree = graphNode.Content;207 subtree = tree.NodeAt(fragment.OldIndex);208 continue;209 }210 // if the fragment contains the selected subtree => track fragment, then track subtree211 if (fragment.Index < subtreeIndex && subtreeIndex < fragment.Index + fragmentLength) {212 if (subtreeLength >= fragmentLength) throw new Exception("Fragment contains subtree, so subtree length should be less than the fragment length.");213 214 graphNode = parentVertices.Last();215 tree = graphNode.Content;216 var i = fragment.Root.IndexOf(subtree); // get the index of the selected subtree, relative to the fragment root217 subtree = tree.NodeAt(fragment.OldIndex + i);218 continue;219 }220 // if the selected subtree contains the fragment => track fragment and subtree221 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 fragmentNode.Content.Index = fragment.Index - subtreeIndex;224 graphNode = parentVertices[0];225 // debug check226 tree = graphNode.Content;227 subtree = tree.NodeAt(subtreeIndex);228 if (subtree.GetLength() <= fragmentNode.Content.Index) {229 throw new Exception("Index exceeds subtree length.");230 }231 // track subtree232 Trace(graphNode, subtree, fragmentGraph, fragmentNode);233 234 // track fragment235 if (parentVertices.Count > 1) {236 // save the index of the current fragment in the tree where it was swapped237 graphNode = parentVertices[1];238 tree = graphNode.Content;239 subtree = tree.NodeAt(fragment.OldIndex);240 continue;241 }242 } else {243 // fragment and subtree are completely distinct => we only track the subtree (contained by the root parent)244 // if the subtree comes AFTER the fragment in the prefix iteration of nodes,245 // then its index is affected by the difference in fragment size between current tree and parent246 graphNode = parentVertices[0];247 tree = graphNode.Content;248 if (subtreeIndex > fragment.Index) {249 var diff = tree.NodeAt(fragment.Index).GetLength() - fragmentLength;250 subtreeIndex += diff;251 }252 subtree = tree.NodeAt(subtreeIndex);253 continue;254 }255 break;256 }257 }258 #endregion259 }260 261 internal static class Util {262 private static string ViewAsText(this ISymbolicExpressionTreeNode root) {263 var writer = new StringWriter();264 SymbolicExpressionTreeHierarchicalFormatter.RenderNode(writer, root, string.Empty);265 return writer.ToString();266 }267 #region some helper methods for shortening the tracing code268 269 internal static void AddChild(this IGenealogyGraphNode parent, IGenealogyGraphNode child) {270 parent.AddForwardArc(child);271 child.AddReverseArc(parent);272 child.Rank = parent.Rank - 1;273 }274 internal static ISymbolicExpressionTreeNode NodeAt(this ISymbolicExpressionTree tree, int position) {275 return NodeAt(tree.Root, position);276 }277 internal static ISymbolicExpressionTreeNode NodeAt(this ISymbolicExpressionTreeNode root, int position) {278 return root.IterateNodesPrefix().ElementAt(position);279 }280 internal static int IndexOf(this ISymbolicExpressionTree tree, ISymbolicExpressionTreeNode node) {281 return IndexOf(tree.Root, node);282 }283 internal static int IndexOf(this ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode node) {284 int i = 0;285 foreach (var n in root.IterateNodesPrefix()) {286 if (n == node) return i;287 ++i;288 }289 return -1;290 }291 #endregion292 161 } 293 162 } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj
r10650 r10752 304 304 <Compile Include="Tracking\SymbolicDataAnalysisExpressionAfterManipulatorOperator.cs" /> 305 305 <Compile Include="Tracking\SymbolicDataAnalysisExpressionBeforeManipulatorOperator.cs" /> 306 <Compile Include="Tracking\SymbolicDataAnalysisExpressionTracing.cs" /> 306 307 <None Include="HeuristicLab.snk" /> 307 308 <None Include="Plugin.cs.frame" /> -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SymbolicDataAnalysisExpressionAfterCrossoverOperator.cs
r10677 r10752 25 25 break; 26 26 } 27 if (fragment == null) throw new Exception("Could not determine fragment!"); 27 // if the fragment is null then it means crossover did not do anything 28 // if (fragment == null) throw new Exception("Could not determine fragment!"); 28 29 29 30 arcs[0].Data = null; -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SymbolicDataAnalysisExpressionBeforeCrossoverOperator.cs
r10677 r10752 29 29 public class SymbolicDataAnalysisExpressionBeforeCrossoverOperator : BeforeCrossoverOperator<ISymbolicExpressionTree> { 30 30 public override IOperation Apply() { 31 var result = base.Apply(); // the child will be added to the graph before the crossover31 var result = base.Apply(); // the base operator will add the child to the graph before the actual crossover operation takes place 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) … … 38 38 arcs[i].Data = nodes; 39 39 } 40 var parentVertices = childVertex. InArcs.Select(x => (IGenealogyGraphNode<ISymbolicExpressionTree>)x.Source).ToList();40 var parentVertices = childVertex.Parents.ToList(); 41 41 if (parents[0].Length != parentVertices[0].Content.Length || parents[1].Length != parentVertices[1].Content.Length) { 42 42 throw new Exception("Inconsistency detected in GenealogyGraph.");
Note: See TracChangeset
for help on using the changeset viewer.