Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1772: Worked towards integrating the new graph api with the tracking operators.

File size: 5.2 KB
RevLine 
[10278]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
[11233]22using System;
[10267]23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
[10271]29namespace HeuristicLab.EvolutionTracking {
[10267]30  [StorableClass]
31  [Item("GenealogGraphNode", "A class representing a node in the GenealogyGraph")]
32  public class GenealogyGraphNode : Vertex, IGenealogyGraphNode {
33    [StorableConstructor]
34    protected GenealogyGraphNode(bool deserializing) : base(deserializing) { }
35    public override IDeepCloneable Clone(Cloner cloner) {
36      return new GenealogyGraphNode(this, cloner);
37    }
38    protected GenealogyGraphNode(GenealogyGraphNode original, Cloner cloner)
39      : base(original, cloner) {
40    }
41
[11233]42    public GenealogyGraphNode(object content)
43      : base(content) {
44      Id = Guid.NewGuid().ToString();
45    }
[10267]46
[10677]47    public new IEnumerable<IGenealogyGraphArc> InArcs {
[10888]48      get { return base.InArcs.Cast<IGenealogyGraphArc>(); }
[10267]49    }
[10677]50    public new IEnumerable<IGenealogyGraphArc> OutArcs {
[10888]51      get { return base.OutArcs.Cast<IGenealogyGraphArc>(); }
[10267]52    }
[10677]53    public IEnumerable<IGenealogyGraphNode> Ancestors {
54      get {
55        // for performance, we use a hashset for lookup and a list for iteration
56        var nodes = new HashSet<IGenealogyGraphNode> { this };
57        var list = new List<IGenealogyGraphNode> { this };
58        int i = 0;
59        while (i != list.Count) {
[10267]60          foreach (var e in list[i].InArcs) {
61            if (nodes.Contains(e.Source)) continue;
62            nodes.Add(e.Source);
63            list.Add(e.Source);
64          }
[10677]65          ++i;
[10267]66        }
[10677]67        return list;
[10267]68      }
69    }
70    /// <summary>
71    /// Performs a downward-breath-traversal of the genealogy graph using the nodes OutArcs
72    /// </summary>
73    /// <returns>All the descendants of the current node</returns>
[10677]74    public IEnumerable<IGenealogyGraphNode> Descendants {
75      get {
76        var nodes = new HashSet<IGenealogyGraphNode> { this };
77        var list = new List<IGenealogyGraphNode> { this };
78        int i = 0;
79        while (i != list.Count) {
[10267]80          foreach (var e in list[i].OutArcs) {
81            if (nodes.Contains(e.Target)) continue;
82            nodes.Add(e.Target);
83            list.Add(e.Target);
84          }
[10677]85          ++i;
[10267]86        }
[10677]87        return list;
[10267]88      }
89    }
90    [Storable]
[11233]91    public string Id { get; private set; }
92
93    [Storable]
[10267]94    private double rank;
95    public double Rank {
96      get { return rank; }
97      set { rank = value; }
98    }
99    [Storable]
100    private double quality;
101    public double Quality {
102      get { return quality; }
103      set { quality = value; }
104    }
105    [Storable]
106    private bool isElite;
107    public bool IsElite {
108      get { return isElite; }
109      set { isElite = value; }
110    }
111    public int CompareTo(IGenealogyGraphNode other) {
112      return Quality.CompareTo(other.Quality);
113    }
[10677]114    public IEnumerable<IGenealogyGraphNode> Parents {
[10685]115      get { return InArcs.Select(a => a.Source); }
[10677]116    }
117    public IEnumerable<IGenealogyGraphNode> Children {
[10685]118      get { return OutArcs.Select(a => a.Target); }
[10677]119    }
[10267]120  }
121
122  [StorableClass]
123  [Item("GenealogyGraphNode", "A genealogy graph node which also has a Content")]
[10300]124  public class GenealogyGraphNode<T> : GenealogyGraphNode, IGenealogyGraphNode<T> where T : class, IItem {
125    public new T Content {
126      get { return (T)base.Content; }
127      set { base.Content = value; }
128    }
[10897]129
130    public GenealogyGraphNode(object content) : base(content) { }
131
[10300]132    protected GenealogyGraphNode(GenealogyGraphNode<T> original, Cloner cloner)
133      : base(original, cloner) {
[11233]134      // we do not clone the content
[10300]135      Content = original.Content;
136    }
137    public override IDeepCloneable Clone(Cloner cloner) {
138      return new GenealogyGraphNode<T>(this, cloner);
139    }
[10677]140    public new IEnumerable<IGenealogyGraphNode<T>> Parents {
[10888]141      get { return base.Parents.Cast<IGenealogyGraphNode<T>>(); }
[10677]142    }
143    public new IEnumerable<IGenealogyGraphNode<T>> Children {
[10888]144      get { return base.Children.Cast<IGenealogyGraphNode<T>>(); }
[10677]145    }
146    public new IEnumerable<IGenealogyGraphNode<T>> Ancestors {
[10888]147      get { return base.Ancestors.Cast<IGenealogyGraphNode<T>>(); }
[10677]148    }
149    public new IEnumerable<IGenealogyGraphNode<T>> Descendants {
[10888]150      get { return base.Descendants.Cast<IGenealogyGraphNode<T>>(); }
[10677]151    }
[10267]152  }
153}
Note: See TracBrowser for help on using the repository browser.