Changeset 8248
- Timestamp:
- 07/06/12 11:24:34 (12 years ago)
- Location:
- branches/HeuristicLab.EvolutionaryTracking
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking.Views/3.4/GenealogyGraphChart.cs
r8236 r8248 69 69 70 70 var genealogyGraphNodes = Graph.Values.ToList(); 71 var layers = genealogyGraphNodes.GroupBy(n => n.Rank).OrderBy(g => g.Key).Select(g => Tuple.Create(g.Key, g.ToList())).ToList();71 var layers = genealogyGraphNodes.GroupBy(n => Graph[n].Ranks[0]).OrderBy(g => g.Key).Select(g => new { Rank = g.Key, Nodes = g.ToList() }).ToList(); 72 72 List<GenealogyGraphNode> currentLayer = null; 73 73 List<GenealogyGraphNode> previousLayer = null; … … 75 75 for (int i = 0; i != layers.Count(); ++i) { 76 76 var layer = layers[i]; 77 double currentRank = layer. Item1;77 double currentRank = layer.Rank; 78 78 if (i > 0 && currentRank % 1 == 0) { 79 79 previousLayer = currentLayer; 80 currentLayer = layer. Item2;80 currentLayer = layer.Nodes; 81 81 int d = previousLayer.Count - currentLayer.Count; 82 82 if (d > 0) 83 83 currentLayer.AddRange(previousLayer.Take(d)); 84 84 } else { 85 currentLayer = layer.Item2; 86 } 87 currentLayer.Sort(); // sort descending by quality (using comparison defined in GenealogyGraphNode class) 85 currentLayer = layer.Nodes; 86 } 87 currentLayer.Sort((b, a) => { 88 if (Graph[a].Quality.Equals(Graph[b].Quality)) { 89 if (Graph[a].IsElite) return 1; 90 if (Graph[b].IsElite) return -1; 91 } 92 return Graph[a].Quality.CompareTo(Graph[b].Quality); 93 }); // sort descending by quality (using comparison defined in GenealogyGraphNode class) 88 94 89 95 x = 0; // reset horizontal coordinate … … 95 101 Data = node, 96 102 ToolTipText = "Id: " + node.Label + nl + 97 "Rank : " + node.Rank+ nl +98 "Quality: " + String.Format("{0:0.0000}", node.Quality) + nl +99 "IsElite: " + node.IsElite103 "Ranks: " + Graph[node].Ranks + nl + 104 "Quality: " + String.Format("{0:0.0000}", Graph[node].Quality) + nl + 105 "IsElite: " + Graph[node].IsElite 100 106 }; 101 107 Chart.Group.Add(visualNode); … … 171 177 } 172 178 var gNode = _selectedGenealogyGraphNode.Data; // genealogy graph node (representing an individual in the population) 173 double rank = gNode.Rank;179 double rank = Graph[gNode].Ranks[0]; 174 180 // ancestors 175 var ancestors = gNode.Ancestors().Where( a => a.Rank< rank).ToList();181 var ancestors = gNode.Ancestors().Where(n => Graph[n].Ranks.Last() < rank).ToList(); 176 182 ancestors.Add(gNode); 177 183 // descendants 178 var descendants = gNode.Descendants().Where( a => a.Rank> rank).ToList();184 var descendants = gNode.Descendants().Where(n => Graph[n].Ranks.Last() > rank).ToList(); 179 185 descendants.Add(gNode); 180 186 // highlight ancestors 181 187 foreach (var node in ancestors.Select(n => _visualNodeMap[n][0])) { 182 node.Brush = new SolidBrush( node.ToColor());188 node.Brush = new SolidBrush(Graph[node.Data].GetColor()); 183 189 if (node.IncomingArcs != null) 184 190 foreach (var arc in node.IncomingArcs) { … … 186 192 // (for purposes of display consistency) 187 193 bool isFirst = arc.Source == _visualNodeMap[arc.Source.Data][0]; 188 if (arc.Source.Data != arc.Target.Data && isFirst) { 189 var start = new Point((int)arc.Start.X, (int)arc.Start.Y); 190 var end = new Point((int)arc.End.X, (int)arc.End.Y); 191 arc.Pen.Brush = new LinearGradientBrush(start, end, arc.Source.ToColor(), arc.Target.ToColor()); 192 } 194 if (arc.Source.Data == arc.Target.Data || !isFirst) continue; 195 var start = new Point((int)arc.Start.X, (int)arc.Start.Y); 196 var end = new Point((int)arc.End.X, (int)arc.End.Y); 197 arc.Pen.Brush = new LinearGradientBrush(start, end, Graph[arc.Source.Data].GetColor(), Graph[arc.Target.Data].GetColor()); 193 198 } 194 199 } 195 200 // highlight descendants 196 201 foreach (var node in descendants.Select(n => _visualNodeMap[n][0])) { 197 node.Brush = new SolidBrush( node.ToColor());202 node.Brush = new SolidBrush(Graph[node.Data].GetColor()); 198 203 if (node.OutgoingArcs != null) 199 204 foreach (var arc in node.OutgoingArcs) { … … 201 206 // (for purposes of display consistency) 202 207 bool isFirst = arc.Target == _visualNodeMap[arc.Target.Data][0]; 203 if (arc.Source.Data != arc.Target.Data && isFirst) { 204 var start = new Point((int)arc.Start.X, (int)arc.Start.Y); 205 var end = new Point((int)arc.End.X, (int)arc.End.Y); 206 arc.Pen.Brush = new LinearGradientBrush(start, end, arc.Source.ToColor(), arc.Target.ToColor()); 207 } 208 if (arc.Source.Data == arc.Target.Data || !isFirst) continue; 209 var start = new Point((int)arc.Start.X, (int)arc.Start.Y); 210 var end = new Point((int)arc.End.X, (int)arc.End.Y); 211 arc.Pen.Brush = new LinearGradientBrush(start, end, Graph[arc.Source.Data].GetColor(), Graph[arc.Target.Data].GetColor()); 208 212 } 209 213 } … … 222 226 public void ClearAllNodes() { 223 227 foreach (var primitive in Chart.Group.Primitives) { 224 if (primitive.Brush != null) 225 primitive.Brush = null; 228 primitive.Brush = null; 226 229 if (primitive is VisualGenealogyGraphArc) { 227 230 var arc = primitive as VisualGenealogyGraphArc; … … 234 237 public void HighlightLineage(IEnumerable<GenealogyGraphNode> nodes) { 235 238 foreach (var node in nodes.SelectMany(n => _visualNodeMap[n])) { 236 node.Brush = new SolidBrush(node.ToColor()); 237 if (node.IncomingArcs != null && node == _visualNodeMap[node.Data][0]) { 238 foreach (var arc in node.IncomingArcs) { 239 if (arc.Source.Data == node.Data) continue; 240 var start = new Point((int)arc.Start.X, (int)arc.Start.Y); 241 var end = new Point((int)arc.End.X, (int)arc.End.Y); 242 arc.Pen.Brush = new LinearGradientBrush(start, end, arc.Source.ToColor(), arc.Target.ToColor()); 243 } 239 node.Brush = new SolidBrush(Graph[node.Data].GetColor()); 240 if (node.IncomingArcs == null || node != _visualNodeMap[node.Data][0]) continue; 241 foreach (var arc in node.IncomingArcs) { 242 if (arc.Source.Data == node.Data) continue; 243 var start = new Point((int)arc.Start.X, (int)arc.Start.Y); 244 var end = new Point((int)arc.End.X, (int)arc.End.Y); 245 arc.Pen.Brush = new LinearGradientBrush(start, end, Graph[arc.Source.Data].GetColor(), Graph[arc.Target.Data].GetColor()); 244 246 } 245 247 } … … 284 286 } 285 287 } 288 289 internal static class Util { 290 public static Color GetColor(this NodeMetadata nm) { 291 var colorIndex = (int)(nm.Quality * ColorGradient.Colors.Count); 292 if (colorIndex >= ColorGradient.Colors.Count) --colorIndex; 293 return ColorGradient.Colors[colorIndex]; 294 } 295 } 286 296 } -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking.Views/3.4/GenealogyGraphView.cs
r8236 r8248 71 71 else { 72 72 genealogyGraphChart.Graph = Content; 73 var best = Content.Values.OrderByDescending(x => x.Quality).First();73 var best = Content.Values.OrderByDescending(x => genealogyGraphChart.Graph[x].Quality).First(); 74 74 symbolicExpressionTreeChart.Tree = (ISymbolicExpressionTree)best.Data; 75 75 } -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking.Views/3.4/VisualGenealogyGraphNode.cs
r8236 r8248 23 23 using System.Collections.Generic; 24 24 using System.Drawing; 25 using HeuristicLab.Common;26 25 using HeuristicLab.Visualization; 27 26 … … 75 74 get { return new PointD((LowerLeft.X + UpperRight.X) / 2, (LowerLeft.Y + UpperRight.Y) / 2); } 76 75 } 77 78 public Color ToColor() {79 // return Color.FromArgb((int)((1 - Data.Quality) * 255), (int)(Data.Quality * 255), 0);80 int i = (int)(Data.Quality * ColorGradient.Colors.Count);81 if (i == ColorGradient.Colors.Count) --i;82 return ColorGradient.Colors[i];83 }84 85 public override void Draw(Graphics graphics) {86 if (Data.IsElite) {87 Point p = Chart.TransformWorldToPixel(new PointD(LowerLeft.X, LowerLeft.Y + Size.Height));88 Size s = Chart.TransformWorldToPixel(Size);89 if (Brush != null)90 graphics.FillEllipse(Brush, p.X, p.Y, s.Width, s.Height);91 graphics.DrawEllipse(Pen, p.X, p.Y, s.Width, s.Height);92 graphics.DrawEllipse(Pen, p.X + 2, p.Y + 2, s.Width - 4, s.Height - 4);93 } else {94 base.Draw(graphics);95 }96 }97 76 } 98 77 } -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Analyzers/SymbolicExpressionTreeGenealogyAnalyzer.cs
r8236 r8248 228 228 } 229 229 // get tree quality values (Key => Individual, Value => Quality) 230 var qualities = (from s in gScope.SubScopes231 232 233 234 select Tuple.Create((ISymbolicExpressionTree)individual, quality.Value)).ToList();230 var scopes = (from s in gScope.SubScopes 231 let individual = s.Variables.First().Value 232 let quality = (DoubleValue)s.Variables["Quality"].Value 233 orderby quality.Value descending 234 select new { Tree = (ISymbolicExpressionTree)individual, Quality = quality.Value }).ToList(); 235 235 236 236 // add all individuals to the evolutionary graph 237 237 int generation = Generations.Value; 238 238 // add nodes to genealogy graph 239 for (int i = 0; i != qualities.Count; ++i) {240 var indiv = qualities[i].Item1;241 var label = (generation * qualities.Count + i + 1).ToString(CultureInfo.InvariantCulture);239 for (int i = 0; i != scopes.Count; ++i) { 240 var indiv = scopes[i].Tree; 241 var label = (generation * scopes.Count + i + 1).ToString(CultureInfo.InvariantCulture); 242 242 if (!graph.HasNode(indiv)) { 243 243 var node = new GenealogyGraphNode(indiv) { 244 Quality = qualities[i].Item2,245 244 Label = label, 246 Rank = generation,247 IsElite = i < Elites.Value248 245 }; 249 246 graph.AddNode(node); 247 // node metadata 248 graph[node].Ranks.Add(generation); 249 graph[node].Quality = scopes[i].Quality; 250 graph[node].IsElite = i < Elites.Value; 250 251 } 251 252 if (generation == 0) continue; … … 256 257 object data = ((GenericWrapper<SymbolicExpressionTreeNode>)GlobalFragmentMap[indiv]).Content; 257 258 if (GlobalTraceMap.ContainsKey(parent)) { 258 double quality = Evaluate(parent); 259 var node = new GenealogyGraphNode(parent) { Quality = quality, Label = "X", Rank = generation - 0.5 }; 259 var node = new GenealogyGraphNode(parent) { Label = "X" }; 260 260 graph.AddNode(node); 261 graph[node].Quality = Evaluate(parent); 262 graph[node].Ranks.Add(generation - 0.5); 261 263 var pp = GlobalTraceMap[parent].Cast<SymbolicExpressionTree>(); 262 264 foreach (var p in pp) { … … 302 304 303 305 foreach (var node in graph.Values) { 304 var color = Color.FromArgb((int)((1 - node.Quality) * 255), (int)(node.Quality * 255), 0);306 var color = Color.FromArgb((int)((1 - graph[node].Quality) * 255), (int)(graph[node].Quality * 255), 0); 305 307 string fillColor = String.Format("#{0:x2}{1:x2}{2:x2}", color.R, color.G, color.B); 306 308 string shape = "circle"; 307 if ( node.IsElite)309 if (graph[node].IsElite) 308 310 shape = "doublecircle"; 309 311 file.WriteLine("\t\"" + node.Id + "\" [shape=" + shape + ",fillcolor=\"" + fillColor + "\",label=\"" + node.Label + "\"];"); … … 316 318 } 317 319 } 318 foreach (var g in graph.Values.GroupBy(x => x.Rank)) {320 foreach (var g in graph.Values.GroupBy(x => graph[x].Ranks[0])) { 319 321 var sb = new StringBuilder(); 320 322 sb.Append("\t{rank=same;"); -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/GenealogyGraph.cs
r8236 r8248 34 34 [Storable] 35 35 private readonly Dictionary<T, GenealogyGraphNode> _nodes; 36 private readonly Dictionary<GenealogyGraphNode, List<double>> _nodeRanks; 36 37 37 38 public GenealogyGraph() { 38 39 _nodes = new Dictionary<T, GenealogyGraphNode>(); 40 _nodeRanks = new Dictionary<GenealogyGraphNode, List<double>>(); 39 41 } 40 42 41 43 public GenealogyGraph(GenealogyGraph<T> g) { 42 44 _nodes = new Dictionary<T, GenealogyGraphNode>(g._nodes); 45 _nodeRanks = new Dictionary<GenealogyGraphNode, List<double>>(g._nodeRanks); 43 46 } 44 47 … … 72 75 /// Adds a node representing an individual 73 76 /// </summary> 74 /// <param name="t ">The data this node is supposed to represent in the graph</param>75 public v oid AddNode(T t) {76 if (HasNode(t )) return;77 _nodes[t ] = new GenealogyGraphNode(t) { Quality = 0.0, Label = "", IsElite = false, Rank = 0.0 };77 /// <param name="tree">The data this node is supposed to represent in the graph</param> 78 public virtual void AddNode(T tree) { 79 if (HasNode(tree)) return; 80 _nodes[tree] = new GenealogyGraphNode(tree); 78 81 } 79 82 … … 82 85 /// </summary> 83 86 /// <param name="node">The node to be added</param> 84 public v oid AddNode(GenealogyGraphNode node) {87 public virtual void AddNode(GenealogyGraphNode node) { 85 88 var t = (T)node.Data; 86 89 if (HasNode(t)) … … 144 147 } 145 148 146 public class GenealogyGraphNode : IComparable{149 public class GenealogyGraphNode { 147 150 public string Id { get; private set; } 148 151 public string Label { get; set; } 149 public bool IsElite { get; set; }150 public double Quality { get; set; }151 public double Rank { get; set; }152 152 public List<GenealogyGraphArc> InEdges { get; set; } 153 153 public List<GenealogyGraphArc> OutEdges { get; set; } … … 176 176 public GenealogyGraphNode(GenealogyGraphNode node) { 177 177 Id = Guid.NewGuid().ToString(); 178 Rank = node.Rank;179 Quality = node.Quality;180 IsElite = node.IsElite;181 178 Label = node.Label; 182 179 Data = node.Data; … … 242 239 else InEdges.Add(e); 243 240 } 244 245 // comparable246 // may seem weird, it is actually implemented so higher quality means "less than" in terms of ordering247 // if quality is equal, the elite node takes precedence248 public int CompareTo(object obj) {249 if (obj == null) return 1;250 var other = obj as GenealogyGraphNode;251 if (other == null) return 1;252 if (Quality.Equals(other.Quality)) {253 if (IsElite) return -1;254 return other.IsElite ? 1 : -1;255 }256 return other.Quality.CompareTo(Quality);257 }258 241 } 259 242 -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/SymbolicExpressionTreeGenealogyGraph.cs
r8236 r8248 11 11 [StorableClass] 12 12 public class SymbolicExpressionTreeGenealogyGraph : GenealogyGraph<ISymbolicExpressionTree> { 13 14 [Storable] 15 private readonly Dictionary<GenealogyGraphNode, NodeMetadata> _nodeInfo = new Dictionary<GenealogyGraphNode, NodeMetadata>(); 16 13 17 public SymbolicExpressionTreeGenealogyGraph() { 14 18 } … … 27 31 } 28 32 33 public override void AddNode(ISymbolicExpressionTree tree) { 34 if (HasNode(tree)) return; 35 var node = new GenealogyGraphNode(tree); 36 _nodeInfo[node] = new NodeMetadata(); 37 base.AddNode(node); 38 } 39 40 public override void AddNode(GenealogyGraphNode node) { 41 var tree = (ISymbolicExpressionTree)node.Data; 42 if (HasNode(tree)) return; 43 _nodeInfo[node] = new NodeMetadata(); 44 base.AddNode(node); 45 } 46 47 public NodeMetadata this[GenealogyGraphNode node] { 48 get { 49 NodeMetadata value; 50 _nodeInfo.TryGetValue(node, out value); 51 return value; 52 } 53 set { 54 _nodeInfo[node] = value; 55 } 56 } 57 29 58 #region Fragment tracing 30 59 public IEnumerable<ISymbolicExpressionTree> TraceFragment(ISymbolicExpressionTreeNode fragment, int mode = 0) { … … 33 62 #endregion 34 63 } 64 65 public class NodeMetadata { 66 public double Quality { get; set; } 67 public List<double> Ranks { get; set; } 68 public bool IsElite { get; set; } 69 70 public NodeMetadata() { 71 Ranks = new List<double>(); 72 } 73 } 35 74 } -
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Selection/3.3/Plugin.cs
r8236 r8248 26 26 /// Plugin class for HeuristicLab.Selection plugin. 27 27 /// </summary> 28 [Plugin("HeuristicLab.Selection", "3.3.6.82 13")]28 [Plugin("HeuristicLab.Selection", "3.3.6.8236")] 29 29 [PluginFile("HeuristicLab.Selection-3.3.dll", PluginFileType.Assembly)] 30 30 [PluginDependency("HeuristicLab.Collections", "3.3")]
Note: See TracChangeset
for help on using the changeset viewer.