Changeset 12265 for branches/HeuristicLab.EvolutionTracking
- Timestamp:
- 03/29/15 23:40:57 (10 years ago)
- Location:
- branches/HeuristicLab.EvolutionTracking
- Files:
-
- 1 deleted
- 3 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Analyzers/GenealogyAnalyzer.cs
r11769 r12265 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 4Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/Tracking/SymbolicDataAnalysisGenealogyGraphView.cs
r12231 r12265 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 4Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 84 84 nodeWeightLabel.Text = String.Format("{0:0.00}", graphNode.Weight); 85 85 86 if (!graphNode.InArcs.Any()) return; 87 86 88 if (openNew_CheckBox.Checked) { 87 89 // get the ancestors into a new view … … 96 98 } 97 99 98 if (graphNode.InArcs.Any()) { 99 var data = graphNode.InArcs.Last().Data; 100 var fragment = data as IFragment<ISymbolicExpressionTreeNode>; 101 var td = data as TraceData; 102 // double max = Content.Vertices.Max(x => x.Data.IterateNodesPrefix().Max(y => y.NodeWeight)); 103 104 // calculate max only in the current generation 105 double max = Content.GetByRank(graphNode.Rank).Max(x => ((IGenealogyGraphNode<ISymbolicExpressionTree>)x).Data.IterateNodesPrefix().Max(y => y.NodeWeight)); 106 if (max.IsAlmost(0)) max = 1; 107 int c = ColorGradient.Colors.Count - 1; 108 const int alpha = 100; 109 if (fragment != null) { 110 // highlight the fragment with a blue line color 111 foreach (var n in graphNode.Data.NodeAt(fragment.Index1).IterateNodesPrefix()) { 112 treeChart_HighlightSubtree(n, Color.CornflowerBlue); 113 } 114 // highlight all the nodes according to their sample count 115 var nodes = graphNode.Data.IterateNodesPrefix().ToList(); 116 foreach (var n in nodes.Skip(1)) { 117 var i = (int)Math.Round(n.NodeWeight / max * c); 118 var fillColor = Color.FromArgb(alpha, ColorGradient.Colors[i]); 119 treeChart_HighlightSubtree(n, null, fillColor); 120 } 121 } else if (td != null) { 122 var nodes = graphNode.Data.IterateNodesPrefix().ToList(); 123 foreach (var n in nodes.Skip(1)) { // skip program root symbol 124 var i = (int)Math.Round(n.NodeWeight / max * c); 125 var fillColor = Color.FromArgb(alpha, ColorGradient.Colors[i]); 126 treeChart_HighlightSubtree(n, null, fillColor); 127 } 128 treeChart_HighlightSubtree(nodes[td.LastSubtreeIndex], Color.Orange); 129 treeChart_HighlightSubtree(nodes[td.LastFragmentIndex], Color.CornflowerBlue); 130 } 131 } 132 } 100 var data = graphNode.InArcs.Last().Data; 101 var fragment = data as IFragment<ISymbolicExpressionTreeNode>; 102 var td = data as TraceData; 103 // calculate max only in the current generation 104 var max = Content.GetByRank(graphNode.Rank).Max(x => ((IGenealogyGraphNode<ISymbolicExpressionTree>)x).Data.IterateNodesPrefix().Max(y => y.NodeWeight)); 105 // var max = Content.GetByRank(graphNode.Rank).Max(x => x.Weight); 106 107 if (max.IsAlmost(0)) 108 max = 1; 109 110 int colorCount = ColorGradient.Colors.Count - 1; 111 const int alpha = 100; 112 if (fragment != null) { 113 // highlight the fragment with a blue line color 114 foreach (var n in graphNode.Data.NodeAt(fragment.Index1).IterateNodesPrefix()) { 115 treeChart_HighlightSubtree(n, Color.CornflowerBlue); 116 } 117 // highlight all the nodes according to their sample count 118 var nodes = graphNode.Data.IterateNodesPrefix().ToList(); 119 foreach (var n in nodes.Skip(1)) { 120 var i = (int)Math.Round(n.NodeWeight / max * colorCount); 121 var fillColor = Color.FromArgb(alpha, ColorGradient.Colors[i]); 122 treeChart_HighlightSubtree(n, null, fillColor); 123 } 124 } else if (td != null) { 125 var nodes = graphNode.Data.IterateNodesPrefix().ToList(); 126 foreach (var n in nodes.Skip(1)) { // skip program root symbol 127 var i = (int)Math.Round(n.NodeWeight / max * colorCount); 128 var fillColor = Color.FromArgb(alpha, ColorGradient.Colors[i]); 129 treeChart_HighlightSubtree(n, null, fillColor); 130 } 131 treeChart_HighlightSubtree(nodes[td.LastSubtreeIndex], Color.Orange); 132 treeChart_HighlightSubtree(nodes[td.LastFragmentIndex], Color.CornflowerBlue); 133 } 134 } 135 136 #region specific symbolic expression tree node click actions 137 private void OnTreeNodeLeftClicked(ISymbolicExpressionTreeNode node) { 138 SelectedSubtree = node; 139 bool trace = genealogyGraphChart.TraceFragments; 140 141 // check whether we are in 'trace' or 'match' mode 142 if (trace) { 143 // TODO: maybe highlight based on per generation maximum 144 // double max = Content.Vertices.Max(x => x.Data.IterateNodesPrefix().Average(y => y.NodeWeight)); 145 // perform fragment tracing 146 var graphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)genealogyGraphChart.SelectedGraphNode; 147 var max = Content.GetByRank(graphNode.Rank).Max(x => x.Weight); 148 var subtreeIndex = graphNode.Data.IterateNodesPrefix().ToList().IndexOf(node); 149 var traceGraph = TraceCalculator.TraceSubtree(graphNode, subtreeIndex, updateVertexWeights: false, updateSubtreeWeights: false, cacheTraceNodes: true); 150 if (traceGraph.Vertices.Any()) { 151 var vertices = traceGraph.Vertices.Select(x => Content.GetByContent(x.Data)); 152 genealogyGraphChart.UpdateEnabled = false; 153 genealogyGraphChart.ClearPrimitives(); 154 foreach (var v in vertices) { 155 var vNode = genealogyGraphChart.GetMappedNode(v); 156 var wSum = v.Data.IterateNodesPrefix().Average(x => x.NodeWeight); 157 // use a grayscale gradient 158 var color = ColorGradient.GrayscaledColors[(int)Math.Round(wSum / max * (ColorGradient.GrayscaledColors.Count - 1))]; 159 vNode.Brush = new SolidBrush(color); 160 } 161 genealogyGraphChart.UpdateEnabled = true; 162 genealogyGraphChart.EnforceUpdate(); 163 164 if (openNew_CheckBox.Checked) { 165 MainFormManager.MainForm.ShowContent(traceGraph); // display the fragment graph on the screen 166 } 167 } 168 } else { 169 // perform matching like it was done before 170 // currently there is no possibility to specify the subtree matching criteria 171 var trees = Content.Vertices.Select(x => x.Data); 172 var matchingTrees = trees.Where(x => x.Root.ContainsSubtree(node, comparer)); 173 174 var matchingVertices = matchingTrees.Select(x => Content.GetByContent(x)); 175 graphChart_HighlightMatchingVertices(matchingVertices); 176 } 177 } 178 179 private void OnTreeNodeMiddleClicked(ISymbolicExpressionTreeNode node) { 180 var index = SelectedSubtree.IterateNodesPrefix().ToList().IndexOf(node); 181 if (index > 0) { 182 var s = ClonedSubtree.NodeAt(index); 183 s.Parent.RemoveSubtree(s.Parent.IndexOfSubtree(s)); 184 185 var trees = Content.Vertices.Select(x => x.Data); 186 var matchingTrees = trees.Where(x => x.Root.ContainsSubtree(ClonedSubtree, comparer)); 187 188 var matchingVertices = matchingTrees.Select(x => Content.GetByContent(x)); 189 treeChart_HighlightSubtree(node, Color.Black, Color.White); 190 graphChart_HighlightMatchingVertices(matchingVertices); 191 } 192 } 193 #endregion 133 194 134 195 public void treeChart_SymbolicExpressionTreeNodeClicked(object sender, MouseEventArgs args) { … … 139 200 treeChart_HighlightSubtree(subtree, Color.Black, Color.RoyalBlue); 140 201 141 bool trace = genealogyGraphChart.TraceFragments; // check whether we are in 'trace' or 'match' mode142 143 202 switch (args.Button) { 144 203 case MouseButtons.Left: { 145 SelectedSubtree = subtree; 146 if (trace) { 147 // TODO: maybe highlight based on per generation maximum 148 double max = Content.Vertices.Max(x => x.Data.IterateNodesPrefix().Average(y => y.NodeWeight)); 149 // perform fragment tracing 150 var graphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)genealogyGraphChart.SelectedGraphNode; 151 var subtreeIndex = graphNode.Data.IterateNodesPrefix().ToList().IndexOf(subtree); 152 var traceGraph = TraceCalculator.TraceSubtree(graphNode, subtreeIndex, updateVertexWeights: false, updateSubtreeWeights: false, cacheTraceNodes: true); 153 if (traceGraph.Vertices.Any()) { 154 var vertices = traceGraph.Vertices.Select(x => Content.GetByContent(x.Data)); 155 genealogyGraphChart.UpdateEnabled = false; 156 genealogyGraphChart.ClearPrimitives(); 157 foreach (var v in vertices) { 158 var vNode = genealogyGraphChart.GetMappedNode(v); 159 var wSum = v.Data.IterateNodesPrefix().Average(x => x.NodeWeight); 160 // use a grayscal gradient 161 var color = ColorGradient.GrayscaledColors[(int)Math.Round(wSum / max * (ColorGradient.GrayscaledColors.Count - 1))]; 162 vNode.Brush = new SolidBrush(color); 163 } 164 genealogyGraphChart.UpdateEnabled = true; 165 genealogyGraphChart.EnforceUpdate(); 166 167 if (openNew_CheckBox.Checked) { 168 MainFormManager.MainForm.ShowContent(traceGraph); // display the fragment graph on the screen 169 } 170 } 171 } else { 172 // perform matching like it was done before 173 // currently there is no possibility to specify the subtree matching criteria 174 var trees = Content.Vertices.Select(x => x.Data); 175 var matchingTrees = trees.Where(x => x.Root.ContainsSubtree(subtree, comparer)); 176 177 var matchingVertices = matchingTrees.Select(x => Content.GetByContent(x)); 178 graphChart_HighlightMatchingVertices(matchingVertices); 179 } 204 OnTreeNodeLeftClicked(subtree); 180 205 break; 181 206 } 182 207 case MouseButtons.Middle: { 183 var index = SelectedSubtree.IterateNodesPrefix().ToList().IndexOf(subtree); 184 if (index > 0) { 185 var s = ClonedSubtree.NodeAt(index); 186 s.Parent.RemoveSubtree(s.Parent.IndexOfSubtree(s)); 187 188 var trees = Content.Vertices.Select(x => x.Data); 189 var matchingTrees = trees.Where(x => x.Root.ContainsSubtree(ClonedSubtree, comparer)); 190 191 var matchingVertices = matchingTrees.Select(x => Content.GetByContent(x)); 192 treeChart_HighlightSubtree(subtree, Color.Black, Color.White); 193 graphChart_HighlightMatchingVertices(matchingVertices); 194 } 208 OnTreeNodeMiddleClicked(subtree); 195 209 break; 196 210 } … … 260 274 private void navigateRightButton_Click(object sender, EventArgs e) { 261 275 var graphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)genealogyGraphChart.SelectedGraphNode; 262 var inArcs = graphNode.InArcs.ToList();276 var inArcs = (List<IArc>)((IVertex)graphNode).InArcs; 263 277 if (inArcs.Count > 0) { 264 var data = inArcs.Last().Data; 265 var fragment = (data as IFragment<ISymbolicExpressionTreeNode>); 266 var td = data as TraceData; 278 if (inArcs.Count == 1) { 279 genealogyGraphChart.SelectedGraphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)inArcs[0].Source; 280 return; 281 } 282 IGenealogyGraphNode<ISymbolicExpressionTree> source = null; 283 var fragment = ((IArc<IDeepCloneable>)inArcs.Last()).Data as IFragment<ISymbolicExpressionTreeNode>; 284 var traceData = ((IArc<IDeepCloneable>)inArcs[0]).Data as TraceData; 267 285 if (fragment != null) { 268 genealogyGraphChart.SelectedGraphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)inArcs.Last().Source; 269 } else if (td != null) { 270 if (inArcs.Count == 1) { 271 genealogyGraphChart.SelectedGraphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)inArcs[0].Source; 272 return; 273 } 274 // the first arc from inArcs will always represent the connection to the root parent 275 // (tracing the body of the traced subtree) 276 // therefore, in order to follow the correct non-root parent, we have to find the correct arc 277 // But, the FragmentIndex recorded in the TraceData of the first arc has to match the SubtreeIndex recorded in the TraceData of the searched arc 278 td = (TraceData)inArcs[0].Data; 279 var f1 = (graphNode.Data).NodeAt(td.LastFragmentIndex); 280 for (int i = 1; i < inArcs.Count; ++i) { 281 td = (TraceData)inArcs[i].Data; 282 var f2 = ((IGenealogyGraphNode<ISymbolicExpressionTree>)inArcs[i].Source).Data.NodeAt(td.SubtreeIndex); 283 // if the subtrees are the same we have found a match 284 // note: this is not necessarily 100% correct if in the trace graph we have identical fragments on different arcs 285 if (f1.Difference(f2) == null) { 286 genealogyGraphChart.SelectedGraphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)inArcs[i].Source; 287 return; 288 } 289 } 290 throw new InvalidOperationException("Error calculating the next step."); 291 } 286 source = (IGenealogyGraphNode<ISymbolicExpressionTree>)inArcs.Last().Source; 287 } 288 if (traceData != null) { 289 var f = graphNode.Data.NodeAt(traceData.LastFragmentIndex); 290 var selected = inArcs.Skip(1).First(x => { 291 var td = (TraceData)((IArc<IDeepCloneable>)x).Data; 292 var s = (IGenealogyGraphNode<ISymbolicExpressionTree>)x.Source; 293 return f.Difference(s.Data.NodeAt(td.SubtreeIndex)) == null; 294 }); 295 source = (IGenealogyGraphNode<ISymbolicExpressionTree>)selected.Source; 296 } 297 if (source != null) 298 genealogyGraphChart.SelectedGraphNode = source; 292 299 } 293 300 } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/Analyzers/SymbolicDataAnalysisSubtreeSampleCountAnalyzer.cs
r12225 r12265 1 #region License Information1 #region License information 2 2 /* HeuristicLab 3 3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL) … … 26 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 27 27 using HeuristicLab.EvolutionTracking; 28 using HeuristicLab.Parameters; 28 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 29 30 … … 32 33 [StorableClass] 33 34 public class SymbolicDataAnalysisSubtreeSampleCountAnalyzer : EvolutionTrackingAnalyzer<ISymbolicExpressionTree> { 35 private const string CacheTraceNodesParameterName = "CacheTraceNodes"; 34 36 private readonly TraceCalculator traceCalculator = new TraceCalculator { UpdateVertexWeights = true, UpdateSubtreeWeights = true, CacheTraceNodes = true }; 35 37 38 #region parameters 39 public IFixedValueParameter<BoolValue> CacheTraceNodesParameter { 40 get { return (IFixedValueParameter<BoolValue>)Parameters[CacheTraceNodesParameterName]; } 41 } 42 #endregion 43 44 #region parameter properties 45 public bool CacheTraceNodes { 46 get { return CacheTraceNodesParameter.Value.Value; } 47 set { CacheTraceNodesParameter.Value.Value = value; } 48 } 49 #endregion 50 36 51 public SymbolicDataAnalysisSubtreeSampleCountAnalyzer() { 37 UpdateCounterParameter.ActualName = "TraceOverlapAnalyzerUpdateCounter"; 52 UpdateCounterParameter.ActualName = "SubtreeSampleCountAnalyzerUpdateCounter"; 53 54 Parameters.Add(new FixedValueParameter<BoolValue>(CacheTraceNodesParameterName, new BoolValue(true))); 38 55 } 39 56 … … 77 94 return base.Apply(); 78 95 79 double avgTraceOverlap = 0; 80 96 traceCalculator.CacheTraceNodes = CacheTraceNodes; 81 97 if (Generation.Value > 0) { 82 98 var rank = PopulationGraph.GetByRank(Generation.Value).Cast<IGenealogyGraphNode<ISymbolicExpressionTree>>().ToList(); 83 foreach (var n in rank) { 84 // during the trace the SymbolicExpressionTreeNode weights will be updated behind the scenes 85 // the weights represent the sample count of each subtree 86 var trace = traceCalculator.Trace(n, 2, false); 87 foreach (var v in trace.Vertices) { 88 var g = PopulationGraph.GetByContent(v.Data); 89 g.Weight = v.Weight; 90 } 99 foreach (var n in rank) 100 traceCalculator.Trace(n, 2, false); // during this call weights will be updated 101 102 foreach (var v in traceCalculator.TraceGraph.Vertices) { 103 var g = PopulationGraph.GetByContent(v.Data); 104 g.Weight = v.Weight; 91 105 } 92 106 } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/TraceCalculator.cs
r12231 r12265 155 155 traceCache.Add(t1); 156 156 } 157 // gather statistics about sampled individuals and sampled subtrees158 if (UpdateVertexWeights)159 n.Weight++;160 if (UpdateSubtreeWeights) {161 var arcs = (List<IArc>)((IVertex)n).InArcs; // at this moment n will have been added as a child to the next trace node162 163 // TODO: try to simplify the code below164 for (int i = 0; i < arcs.Count; ++i) {165 var td = (TraceData)((IArc<IDeepCloneable>)arcs[i]).Data;166 var p = (IGenealogyGraphNode<ISymbolicExpressionTree>)arcs[i].Source;167 var s = NodeAt(p.Data, td.SubtreeIndex);168 if (td.LastFragmentIndex == td.SubtreeIndex && fragment.Root.Difference(s) == null) {169 foreach (var ss in s.IterateNodesPrefix()) ss.NodeWeight++; // the node weight will represent the total sample count for a given node170 arcs[i].Weight++; // the arc weights (since there are multiple arcs) will sum up to the same count but give more detail171 break;172 }173 }174 }175 157 break; 176 158 } else { … … 197 179 traceCache.Add(t); 198 180 } 199 if (UpdateVertexWeights)200 n.Weight++;201 181 break; 202 182 } else { … … 257 237 /// <param name="si">The index of the traced subtree</param> 258 238 /// <param name="fi">The index of the fragment</param> 259 private void ConnectLast(IGenealogyGraphNode<ISymbolicExpressionTree> current, 260 IGenealogyGraphNode<ISymbolicExpressionTree> last, int si, int fi) { 239 private void ConnectLast(IGenealogyGraphNode<ISymbolicExpressionTree> current, IGenealogyGraphNode<ISymbolicExpressionTree> last, int si, int fi) { 261 240 var lastTraceData = traceMap[last]; 262 241 int lastSi = lastTraceData.SubtreeIndex; // last subtree index (index of the traced subtree in the previous trace node) 263 242 int lastFi = lastTraceData.FragmentIndex; // last fragment index (index of the fragment in the previous trace node) 264 243 var td = new TraceData(si, fi, lastSi, lastFi); // trace data 265 // using the inArcs seems to be slightly more efficient than using the outArcs 244 266 245 // TODO: more testing 267 var inArcs = (List<IArc>)((IVertex)last).InArcs; 246 var inArcs = (List<IArc>)((IVertex)last).InArcs; // using the InArcs seems to be slightly more efficient than using the OutArcs 268 247 var arc = inArcs.FirstOrDefault(a => a.Source == current && ((IArc<IDeepCloneable>)a).Data.Equals(td)); 269 248 if (arc == null) { 270 249 arc = new GenealogyGraphArc(current, last) { Data = td }; 271 250 TraceGraph.AddArc(arc); 251 } else { 252 if (UpdateVertexWeights) { 253 arc.Weight++; 254 current.Weight++; 255 } 256 if (UpdateSubtreeWeights) { 257 var subtree = NodeAt(current.Data, td.SubtreeIndex); 258 foreach (var s in subtree.IterateNodesPrefix()) 259 s.NodeWeight++; 260 } 272 261 } 273 262 }
Note: See TracChangeset
for help on using the changeset viewer.