Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/05/12 16:01:37 (12 years ago)
Author:
bburlacu
Message:

#1772: Refactored code in the GenealogyGraphChart and the SymbolicExpressionTreeGenealogyAnalyzer. Used gradient from HeuristicLab.Common to color the graph nodes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking.Views/3.4/GenealogyGraphChart.cs

    r8213 r8236  
    6767      if (_graph == null) return;
    6868      Chart.UpdateEnabled = false;
    69       var layers = Graph.Values.GroupBy(node => node.Rank).OrderBy(g => g.Key);
    70       // show elites in every graph level
    71       List<GenealogyGraphNode> currLayer = null, prevLayer = null;
    72       double currRank = 0.0;
    73       int count = layers.Count();
    74       for (int i = 0; i != count; ++i) {
    75         // propagate elites from successive layers
    76         bool f = currRank % 1 == 0;
    77         if (f) // if the rank is an integer, then we need to propagate elites
    78           prevLayer = currLayer;
    79         currRank = layers.ElementAt(i).Key;
    80         currLayer = layers.ElementAt(i).ToList();
    81         if (i > 0 && f) {
    82           // this following code injects elites that propagate from the previous generation into the current graph layer,
    83           // so that they can be represented as visual nodes
    84           int prevEliteCount = prevLayer.Count(node => node.IsElite);
    85           int currEliteCount = currLayer.Count(node => node.IsElite);
    86           for (int j = currEliteCount; j < prevEliteCount; ++j)
    87             currLayer.Add(prevLayer.ElementAt(j));
    88         }
    89         currLayer.Sort(); // uses the CompareTo() method inside the GenealogyGraphNode class
    90         foreach (var node in currLayer) {
     69
     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();
     72      List<GenealogyGraphNode> currentLayer = null;
     73      List<GenealogyGraphNode> previousLayer = null;
     74
     75      for (int i = 0; i != layers.Count(); ++i) {
     76        var layer = layers[i];
     77        double currentRank = layer.Item1;
     78        if (i > 0 && currentRank % 1 == 0) {
     79          previousLayer = currentLayer;
     80          currentLayer = layer.Item2;
     81          int d = previousLayer.Count - currentLayer.Count;
     82          if (d > 0)
     83            currentLayer.AddRange(previousLayer.Take(d));
     84        } else {
     85          currentLayer = layer.Item2;
     86        }
     87        currentLayer.Sort(); // sort descending by quality (using comparison defined in GenealogyGraphNode class)
     88
     89        x = 0; // reset horizontal coordinate
     90        // start laying out visual nodes
     91        foreach (var node in currentLayer) {
    9192          var pen = new Pen(Color.LightGray);
    9293          var nl = Environment.NewLine;
     
    102103            _visualNodeMap[node] = new List<VisualGenealogyGraphNode>();
    103104          _visualNodeMap[node].Add(visualNode);
    104           // connect visual nodes that actually represent the same individual
    105           // in the genealogy graph (this applies for elites) with a dashed line
    106           if (_visualNodeMap[node].Count > 1) {
    107             for (int c = _visualNodeMap[node].Count; c >= 2; --c) {
    108               var n1 = _visualNodeMap[node][c - 1];
    109               var n2 = _visualNodeMap[node][c - 2];
    110               var visualArc = AddArc(Chart, n1, n2, new Pen(Color.LightGray) { DashStyle = DashStyle.Dash });
    111               _visualArcMap[Tuple.Create(n1, n2)] = visualArc;
    112             }
    113           }
     105
    114106          x += Cx; // increment horizontal coordinate
    115           if (node.InEdges == null && _visualNodeMap.ContainsKey(node)) continue;
    116           // add visual edges
    117           foreach (var arc in node.InEdges) {
    118             var parent = arc.Target;
    119             // find the visual parent node that is closest to the current node in terms of vertical distance
    120             var visualParent = _visualNodeMap[parent].Where(p => p.Center.Y >= visualNode.Center.Y).OrderByDescending(p => p.Center.Y).Last();
    121             DashStyle style;
    122             if (visualParent.Data == visualNode.Data) { // if the graph nodes represent the same elite individual
    123               style = DashStyle.Dash;
    124             } else {
    125               style = node.InEdges.Count == 2 ? DashStyle.Solid : DashStyle.Dash;
    126             }
    127             var arcPen = new Pen(Color.Transparent) { DashStyle = style };
    128             _visualArcMap[Tuple.Create(visualParent, visualNode)] = AddArc(Chart, visualParent, visualNode, arcPen);
    129           }
    130         }
     107        }
     108
    131109        y -= Cy; // decrement vertical coordinate (because the origin is upside down)
    132         x = 0; // reset horizontal coordinate
     110      }
     111      // add arcs separately (to avoid some ordering problems)
     112      foreach (var node in _visualNodeMap.Keys) {
     113        var visualNode = _visualNodeMap[node][0];
     114        if (node.InEdges == null) continue;
     115
     116        foreach (var arc in node.InEdges) {
     117          var visualParent = _visualNodeMap[arc.Target][0];
     118          var pen = new Pen(Color.Transparent);
     119          _visualArcMap[Tuple.Create(visualParent, visualNode)] = AddArc(Chart, visualParent, visualNode, pen);
     120        }
     121      }
     122      // connect visual nodes representing the same elite individual with a line
     123      foreach (var list in _visualNodeMap.Values.Where(l => l.Count > 1)) {
     124        for (int i = 1; i != list.Count; ++i) {
     125          var pen = new Pen(Color.LightGray);
     126          _visualArcMap[Tuple.Create(list[i - 1], list[i])] = AddArc(Chart, list[i - 1], list[i], pen);
     127        }
    133128      }
    134129
     
    187182            node.Brush = new SolidBrush(node.ToColor());
    188183            if (node.IncomingArcs != null)
    189               foreach (var arc in node.IncomingArcs)
    190                 if (arc.Source.Data != arc.Target.Data && arc.Source == _visualNodeMap[arc.Source.Data][0]) {
     184              foreach (var arc in node.IncomingArcs) {
     185                // check if, in case of elites, the target node is the first visual representation of the elite
     186                // (for purposes of display consistency)
     187                bool isFirst = arc.Source == _visualNodeMap[arc.Source.Data][0];
     188                if (arc.Source.Data != arc.Target.Data && isFirst) {
    191189                  var start = new Point((int)arc.Start.X, (int)arc.Start.Y);
    192190                  var end = new Point((int)arc.End.X, (int)arc.End.Y);
    193191                  arc.Pen.Brush = new LinearGradientBrush(start, end, arc.Source.ToColor(), arc.Target.ToColor());
    194192                }
     193              }
    195194          }
    196195          // highlight descendants
     
    198197            node.Brush = new SolidBrush(node.ToColor());
    199198            if (node.OutgoingArcs != null)
    200               foreach (var arc in node.OutgoingArcs)
    201                 if (arc.Source.Data != arc.Target.Data && arc.Target == _visualNodeMap[arc.Target.Data][0]) {
     199              foreach (var arc in node.OutgoingArcs) {
     200                // check if, in case of elites, the target node is the first visual representation of the elite
     201                // (for purposes of display consistency)
     202                bool isFirst = arc.Target == _visualNodeMap[arc.Target.Data][0];
     203                if (arc.Source.Data != arc.Target.Data && isFirst) {
    202204                  var start = new Point((int)arc.Start.X, (int)arc.Start.Y);
    203205                  var end = new Point((int)arc.End.X, (int)arc.End.Y);
    204206                  arc.Pen.Brush = new LinearGradientBrush(start, end, arc.Source.ToColor(), arc.Target.ToColor());
    205207                }
     208              }
    206209          }
    207210        } else {
Note: See TracChangeset for help on using the changeset viewer.