Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10685 was 10685, checked in by bburlacu, 10 years ago

#1772: Tracking building blocks: worked on tracking logic and on the FragmentView.

File size: 5.6 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 new void AddForwardArc(IVertex target, double w = 0.0, object data = null) {
110      var arc = new GenealogyGraphArc { Source = this, Target = (IGenealogyGraphNode)target, Data = data, Weight = w };
111      base.AddForwardArc(arc);
112    }
113    public new void AddReverseArc(IVertex source, double w = 0.0, object data = null) {
114      var arc = new GenealogyGraphArc { Source = (IGenealogyGraphNode)source, Target = this, Data = data, Weight = w };
115      base.AddReverseArc(arc);
116    }
117    public IEnumerable<IGenealogyGraphNode> Parents {
118      get { return InArcs.Select(a => a.Source); }
119    }
120    public IEnumerable<IGenealogyGraphNode> Children {
121      get { return OutArcs.Select(a => a.Target); }
122    }
123  }
124
125  [StorableClass]
126  [Item("GenealogyGraphNode", "A genealogy graph node which also has a Content")]
127  public class GenealogyGraphNode<T> : GenealogyGraphNode, IGenealogyGraphNode<T> where T : class, IItem {
128    public new T Content {
129      get { return (T)base.Content; }
130      set { base.Content = value; }
131    }
132    public GenealogyGraphNode() { }
133    protected GenealogyGraphNode(GenealogyGraphNode<T> original, Cloner cloner)
134      : base(original, cloner) {
135      Content = original.Content;
136    }
137    public override IDeepCloneable Clone(Cloner cloner) {
138      return new GenealogyGraphNode<T>(this, cloner);
139    }
140    public new IEnumerable<IGenealogyGraphNode<T>> Parents {
141      get { return base.Parents.Select(p => (IGenealogyGraphNode<T>)p); }
142    }
143    public new IEnumerable<IGenealogyGraphNode<T>> Children {
144      get { return base.Children.Select(c => (IGenealogyGraphNode<T>)c); }
145    }
146    public new IEnumerable<IGenealogyGraphNode<T>> Ancestors {
147      get { return base.Ancestors.Select(x => (IGenealogyGraphNode<T>)x); }
148    }
149    public new IEnumerable<IGenealogyGraphNode<T>> Descendants {
150      get { return base.Descendants.Select(x => (IGenealogyGraphNode<T>)x); }
151    }
152  }
153}
Note: See TracBrowser for help on using the repository browser.