source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/GenealogyGraphNode.cs @ 10886

Last change on this file since 10886 was 10886, checked in by bburlacu, 8 years ago

#1772: Simplified GenealogyGraph (and related components) API in an effort to improve speed and memory consumption (eg., by sharing the same arc when walking the graph in both directions: parent-child and child-parent).

File size: 5.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.EvolutionTracking {
29  [StorableClass]
30  [Item("GenealogGraphNode", "A class representing a node in the GenealogyGraph")]
31  public class GenealogyGraphNode : Vertex, IGenealogyGraphNode {
32    [StorableConstructor]
33    protected GenealogyGraphNode(bool deserializing) : base(deserializing) { }
34    public override IDeepCloneable Clone(Cloner cloner) {
35      return new GenealogyGraphNode(this, cloner);
36    }
37    protected GenealogyGraphNode(GenealogyGraphNode original, Cloner cloner)
38      : base(original, cloner) {
39    }
40
41    public GenealogyGraphNode() { }
42
43    public new IEnumerable<IGenealogyGraphArc> InArcs {
44      get { return base.InArcs.Cast<IGenealogyGraphArc>().ToList(); }
45      set { base.InArcs = value; }
46    }
47    public new IEnumerable<IGenealogyGraphArc> OutArcs {
48      get { return base.OutArcs.Cast<IGenealogyGraphArc>().ToList(); }
49      set { base.InArcs = value; }
50    }
51    public IEnumerable<IGenealogyGraphNode> Ancestors {
52      get {
53        // for performance, we use a hashset for lookup and a list for iteration
54        var nodes = new HashSet<IGenealogyGraphNode> { this };
55        var list = new List<IGenealogyGraphNode> { this };
56        int i = 0;
57        while (i != list.Count) {
58          foreach (var e in list[i].InArcs) {
59            if (nodes.Contains(e.Source)) continue;
60            nodes.Add(e.Source);
61            list.Add(e.Source);
62          }
63          ++i;
64        }
65        return list;
66      }
67    }
68    /// <summary>
69    /// Performs a downward-breath-traversal of the genealogy graph using the nodes OutArcs
70    /// </summary>
71    /// <returns>All the descendants of the current node</returns>
72    public IEnumerable<IGenealogyGraphNode> Descendants {
73      get {
74        var nodes = new HashSet<IGenealogyGraphNode> { this };
75        var list = new List<IGenealogyGraphNode> { this };
76        int i = 0;
77        while (i != list.Count) {
78          foreach (var e in list[i].OutArcs) {
79            if (nodes.Contains(e.Target)) continue;
80            nodes.Add(e.Target);
81            list.Add(e.Target);
82          }
83          ++i;
84        }
85        return list;
86      }
87    }
88    [Storable]
89    private double rank;
90    public double Rank {
91      get { return rank; }
92      set { rank = value; }
93    }
94    [Storable]
95    private double quality;
96    public double Quality {
97      get { return quality; }
98      set { quality = value; }
99    }
100    [Storable]
101    private bool isElite;
102    public bool IsElite {
103      get { return isElite; }
104      set { isElite = value; }
105    }
106    public int CompareTo(IGenealogyGraphNode other) {
107      return Quality.CompareTo(other.Quality);
108    }
109    public IEnumerable<IGenealogyGraphNode> Parents {
110      get { return InArcs.Select(a => a.Source); }
111    }
112    public IEnumerable<IGenealogyGraphNode> Children {
113      get { return OutArcs.Select(a => a.Target); }
114    }
115  }
116
117  [StorableClass]
118  [Item("GenealogyGraphNode", "A genealogy graph node which also has a Content")]
119  public class GenealogyGraphNode<T> : GenealogyGraphNode, IGenealogyGraphNode<T> where T : class, IItem {
120    public new T Content {
121      get { return (T)base.Content; }
122      set { base.Content = value; }
123    }
124    public GenealogyGraphNode() { }
125    protected GenealogyGraphNode(GenealogyGraphNode<T> original, Cloner cloner)
126      : base(original, cloner) {
127      Content = original.Content;
128    }
129    public override IDeepCloneable Clone(Cloner cloner) {
130      return new GenealogyGraphNode<T>(this, cloner);
131    }
132    public new IEnumerable<IGenealogyGraphNode<T>> Parents {
133      get { return base.Parents.Select(p => (IGenealogyGraphNode<T>)p); }
134    }
135    public new IEnumerable<IGenealogyGraphNode<T>> Children {
136      get { return base.Children.Select(c => (IGenealogyGraphNode<T>)c); }
137    }
138    public new IEnumerable<IGenealogyGraphNode<T>> Ancestors {
139      get { return base.Ancestors.Select(x => (IGenealogyGraphNode<T>)x); }
140    }
141    public new IEnumerable<IGenealogyGraphNode<T>> Descendants {
142      get { return base.Descendants.Select(x => (IGenealogyGraphNode<T>)x); }
143    }
144  }
145}
Note: See TracBrowser for help on using the repository browser.