Free cookie consent management tool by TermsFeed Policy Generator

Changeset 8248


Ignore:
Timestamp:
07/06/12 11:24:34 (12 years ago)
Author:
bburlacu
Message:

#1772: Separated instance-specific attributes of graph node objects from the generic genealogy graph, into metadata objects kept by the specific graph class which corresponds to the specific problem instance (so for instance the SymbolicExpressionTreeGenealogyGraph will keep info about node ranks and qualities etc because that info is specific to symbolic data analysis problems).

Location:
branches/HeuristicLab.EvolutionaryTracking
Files:
7 edited

Legend:

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

    r8236 r8248  
    6969
    7070      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();
    7272      List<GenealogyGraphNode> currentLayer = null;
    7373      List<GenealogyGraphNode> previousLayer = null;
     
    7575      for (int i = 0; i != layers.Count(); ++i) {
    7676        var layer = layers[i];
    77         double currentRank = layer.Item1;
     77        double currentRank = layer.Rank;
    7878        if (i > 0 && currentRank % 1 == 0) {
    7979          previousLayer = currentLayer;
    80           currentLayer = layer.Item2;
     80          currentLayer = layer.Nodes;
    8181          int d = previousLayer.Count - currentLayer.Count;
    8282          if (d > 0)
    8383            currentLayer.AddRange(previousLayer.Take(d));
    8484        } 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)
    8894
    8995        x = 0; // reset horizontal coordinate
     
    95101            Data = node,
    96102            ToolTipText = "Id: " + node.Label + nl +
    97                           "Rank: " + node.Rank + nl +
    98                           "Quality: " + String.Format("{0:0.0000}", node.Quality) + nl +
    99                           "IsElite: " + node.IsElite
     103                          "Ranks: " + Graph[node].Ranks + nl +
     104                          "Quality: " + String.Format("{0:0.0000}", Graph[node].Quality) + nl +
     105                          "IsElite: " + Graph[node].IsElite
    100106          };
    101107          Chart.Group.Add(visualNode);
     
    171177          }
    172178          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];
    174180          // 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();
    176182          ancestors.Add(gNode);
    177183          // 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();
    179185          descendants.Add(gNode);
    180186          // highlight ancestors
    181187          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());
    183189            if (node.IncomingArcs != null)
    184190              foreach (var arc in node.IncomingArcs) {
     
    186192                // (for purposes of display consistency)
    187193                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());
    193198              }
    194199          }
    195200          // highlight descendants
    196201          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());
    198203            if (node.OutgoingArcs != null)
    199204              foreach (var arc in node.OutgoingArcs) {
     
    201206                // (for purposes of display consistency)
    202207                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());
    208212              }
    209213          }
     
    222226    public void ClearAllNodes() {
    223227      foreach (var primitive in Chart.Group.Primitives) {
    224         if (primitive.Brush != null)
    225           primitive.Brush = null;
     228        primitive.Brush = null;
    226229        if (primitive is VisualGenealogyGraphArc) {
    227230          var arc = primitive as VisualGenealogyGraphArc;
     
    234237    public void HighlightLineage(IEnumerable<GenealogyGraphNode> nodes) {
    235238      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());
    244246        }
    245247      }
     
    284286    }
    285287  }
     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  }
    286296}
  • branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking.Views/3.4/GenealogyGraphView.cs

    r8236 r8248  
    7171      else {
    7272        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();
    7474        symbolicExpressionTreeChart.Tree = (ISymbolicExpressionTree)best.Data;
    7575      }
  • branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking.Views/3.4/VisualGenealogyGraphNode.cs

    r8236 r8248  
    2323using System.Collections.Generic;
    2424using System.Drawing;
    25 using HeuristicLab.Common;
    2625using HeuristicLab.Visualization;
    2726
     
    7574      get { return new PointD((LowerLeft.X + UpperRight.X) / 2, (LowerLeft.Y + UpperRight.Y) / 2); }
    7675    }
    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     }
    9776  }
    9877}
  • branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Analyzers/SymbolicExpressionTreeGenealogyAnalyzer.cs

    r8236 r8248  
    228228        }
    229229        // get tree quality values (Key => Individual, Value => Quality)
    230         var qualities = (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 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();
    235235
    236236        // add all individuals to the evolutionary graph
    237237        int generation = Generations.Value;
    238238        // 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);
    242242          if (!graph.HasNode(indiv)) {
    243243            var node = new GenealogyGraphNode(indiv) {
    244               Quality = qualities[i].Item2,
    245244              Label = label,
    246               Rank = generation,
    247               IsElite = i < Elites.Value
    248245            };
    249246            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;
    250251          }
    251252          if (generation == 0) continue;
     
    256257            object data = ((GenericWrapper<SymbolicExpressionTreeNode>)GlobalFragmentMap[indiv]).Content;
    257258            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" };
    260260              graph.AddNode(node);
     261              graph[node].Quality = Evaluate(parent);
     262              graph[node].Ranks.Add(generation - 0.5);
    261263              var pp = GlobalTraceMap[parent].Cast<SymbolicExpressionTree>();
    262264              foreach (var p in pp) {
     
    302304
    303305        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);
    305307          string fillColor = String.Format("#{0:x2}{1:x2}{2:x2}", color.R, color.G, color.B);
    306308          string shape = "circle";
    307           if (node.IsElite)
     309          if (graph[node].IsElite)
    308310            shape = "doublecircle";
    309311          file.WriteLine("\t\"" + node.Id + "\" [shape=" + shape + ",fillcolor=\"" + fillColor + "\",label=\"" + node.Label + "\"];");
     
    316318          }
    317319        }
    318         foreach (var g in graph.Values.GroupBy(x => x.Rank)) {
     320        foreach (var g in graph.Values.GroupBy(x => graph[x].Ranks[0])) {
    319321          var sb = new StringBuilder();
    320322          sb.Append("\t{rank=same;");
  • branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/GenealogyGraph.cs

    r8236 r8248  
    3434    [Storable]
    3535    private readonly Dictionary<T, GenealogyGraphNode> _nodes;
     36    private readonly Dictionary<GenealogyGraphNode, List<double>> _nodeRanks;
    3637
    3738    public GenealogyGraph() {
    3839      _nodes = new Dictionary<T, GenealogyGraphNode>();
     40      _nodeRanks = new Dictionary<GenealogyGraphNode, List<double>>();
    3941    }
    4042
    4143    public GenealogyGraph(GenealogyGraph<T> g) {
    4244      _nodes = new Dictionary<T, GenealogyGraphNode>(g._nodes);
     45      _nodeRanks = new Dictionary<GenealogyGraphNode, List<double>>(g._nodeRanks);
    4346    }
    4447
     
    7275    /// Adds a node representing an individual
    7376    /// </summary>
    74     /// <param name="t">The data this node is supposed to represent in the graph</param>
    75     public void 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);
    7881    }
    7982
     
    8285    /// </summary>
    8386    /// <param name="node">The node to be added</param>
    84     public void AddNode(GenealogyGraphNode node) {
     87    public virtual void AddNode(GenealogyGraphNode node) {
    8588      var t = (T)node.Data;
    8689      if (HasNode(t))
     
    144147  }
    145148
    146   public class GenealogyGraphNode : IComparable {
     149  public class GenealogyGraphNode {
    147150    public string Id { get; private set; }
    148151    public string Label { get; set; }
    149     public bool IsElite { get; set; }
    150     public double Quality { get; set; }
    151     public double Rank { get; set; }
    152152    public List<GenealogyGraphArc> InEdges { get; set; }
    153153    public List<GenealogyGraphArc> OutEdges { get; set; }
     
    176176    public GenealogyGraphNode(GenealogyGraphNode node) {
    177177      Id = Guid.NewGuid().ToString();
    178       Rank = node.Rank;
    179       Quality = node.Quality;
    180       IsElite = node.IsElite;
    181178      Label = node.Label;
    182179      Data = node.Data;
     
    242239      else InEdges.Add(e);
    243240    }
    244 
    245     // comparable
    246     // may seem weird, it is actually implemented so higher quality means "less than" in terms of ordering
    247     // if quality is equal, the elite node takes precedence
    248     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     }
    258241  }
    259242
  • branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/SymbolicExpressionTreeGenealogyGraph.cs

    r8236 r8248  
    1111  [StorableClass]
    1212  public class SymbolicExpressionTreeGenealogyGraph : GenealogyGraph<ISymbolicExpressionTree> {
     13
     14    [Storable]
     15    private readonly Dictionary<GenealogyGraphNode, NodeMetadata> _nodeInfo = new Dictionary<GenealogyGraphNode, NodeMetadata>();
     16
    1317    public SymbolicExpressionTreeGenealogyGraph() {
    1418    }
     
    2731    }
    2832
     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
    2958    #region Fragment tracing
    3059    public IEnumerable<ISymbolicExpressionTree> TraceFragment(ISymbolicExpressionTreeNode fragment, int mode = 0) {
     
    3362    #endregion
    3463  }
     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  }
    3574}
  • branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Selection/3.3/Plugin.cs

    r8236 r8248  
    2626  /// Plugin class for HeuristicLab.Selection plugin.
    2727  /// </summary>
    28   [Plugin("HeuristicLab.Selection", "3.3.6.8213")]
     28  [Plugin("HeuristicLab.Selection", "3.3.6.8236")]
    2929  [PluginFile("HeuristicLab.Selection-3.3.dll", PluginFileType.Assembly)]
    3030  [PluginDependency("HeuristicLab.Collections", "3.3")]
Note: See TracChangeset for help on using the changeset viewer.