Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11750 was 11750, checked in by bburlacu, 9 years ago

#1772: Changed Ancestors and Descendants methods to not include the current graph node as it better fits the definition of what an ancestor (or descendant) is.

File size: 5.4 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) { }
[11253]35
[10267]36    public override IDeepCloneable Clone(Cloner cloner) {
37      return new GenealogyGraphNode(this, cloner);
38    }
[11253]39
[10267]40    protected GenealogyGraphNode(GenealogyGraphNode original, Cloner cloner)
41      : base(original, cloner) {
[11253]42      Quality = original.Quality;
43      Rank = original.Rank;
44      IsElite = original.IsElite;
45      Id = Guid.NewGuid().ToString();
[10267]46    }
47
[11253]48    public GenealogyGraphNode(IDeepCloneable data)
49      : base(data) {
[11233]50      Id = Guid.NewGuid().ToString();
51    }
[10267]52
[10677]53    public new IEnumerable<IGenealogyGraphArc> InArcs {
[11459]54      get { return base.InArcs.Select(x => (IGenealogyGraphArc)x); }
[10267]55    }
[11253]56
[10677]57    public new IEnumerable<IGenealogyGraphArc> OutArcs {
[11459]58      get { return base.OutArcs.Select(x => (IGenealogyGraphArc)x); }
[10267]59    }
[11253]60
[11750]61    public IEnumerable<IGenealogyGraphNode> Parents {
62      get { return InArcs.Select(a => a.Source); }
63    }
64    public IEnumerable<IGenealogyGraphNode> Children {
65      get { return OutArcs.Select(a => a.Target); }
66    }
67
[10677]68    public IEnumerable<IGenealogyGraphNode> Ancestors {
69      get {
[11750]70        if (!Parents.Any())
71          return Enumerable.Empty<IGenealogyGraphNode>();
[10677]72        // for performance, we use a hashset for lookup and a list for iteration
[11750]73        var nodes = new HashSet<IGenealogyGraphNode>(Parents);
74        var list = new List<IGenealogyGraphNode>(Parents);
[10677]75        int i = 0;
76        while (i != list.Count) {
[10267]77          foreach (var e in list[i].InArcs) {
78            if (nodes.Contains(e.Source)) continue;
79            nodes.Add(e.Source);
80            list.Add(e.Source);
81          }
[10677]82          ++i;
[10267]83        }
[10677]84        return list;
[10267]85      }
86    }
[11750]87
[10677]88    public IEnumerable<IGenealogyGraphNode> Descendants {
89      get {
[11750]90        var nodes = new HashSet<IGenealogyGraphNode>(Children);
91        var list = new List<IGenealogyGraphNode>(Children);
[10677]92        int i = 0;
93        while (i != list.Count) {
[10267]94          foreach (var e in list[i].OutArcs) {
95            if (nodes.Contains(e.Target)) continue;
96            nodes.Add(e.Target);
97            list.Add(e.Target);
98          }
[10677]99          ++i;
[10267]100        }
[10677]101        return list;
[10267]102      }
103    }
[11750]104
[10267]105    [Storable]
[11233]106    public string Id { get; private set; }
107
108    [Storable]
[10267]109    private double rank;
110    public double Rank {
111      get { return rank; }
112      set { rank = value; }
113    }
114    [Storable]
115    private double quality;
116    public double Quality {
117      get { return quality; }
118      set { quality = value; }
119    }
120    [Storable]
121    private bool isElite;
122    public bool IsElite {
123      get { return isElite; }
124      set { isElite = value; }
125    }
126    public int CompareTo(IGenealogyGraphNode other) {
127      return Quality.CompareTo(other.Quality);
128    }
129  }
130
131  [StorableClass]
132  [Item("GenealogyGraphNode", "A genealogy graph node which also has a Content")]
[10300]133  public class GenealogyGraphNode<T> : GenealogyGraphNode, IGenealogyGraphNode<T> where T : class, IItem {
[11253]134    public new T Data {
135      get { return (T)base.Data; }
136      set { base.Data = value; }
[10300]137    }
[10897]138
[11262]139    [StorableConstructor]
140    protected GenealogyGraphNode(bool deserializing) : base(deserializing) { }
[10897]141
[11253]142    public GenealogyGraphNode(IDeepCloneable content) : base(content) { }
143
[10300]144    protected GenealogyGraphNode(GenealogyGraphNode<T> original, Cloner cloner)
145      : base(original, cloner) {
146    }
147    public override IDeepCloneable Clone(Cloner cloner) {
148      return new GenealogyGraphNode<T>(this, cloner);
149    }
[10677]150    public new IEnumerable<IGenealogyGraphNode<T>> Parents {
[11459]151      get { return base.Parents.Select(x => (IGenealogyGraphNode<T>)x); }
[10677]152    }
153    public new IEnumerable<IGenealogyGraphNode<T>> Children {
[11459]154      get { return base.Children.Select(x => (IGenealogyGraphNode<T>)x); }
[10677]155    }
156    public new IEnumerable<IGenealogyGraphNode<T>> Ancestors {
[11459]157      get { return base.Ancestors.Select(x => (IGenealogyGraphNode<T>)x); }
[10677]158    }
159    public new IEnumerable<IGenealogyGraphNode<T>> Descendants {
[11459]160      get { return base.Descendants.Select(x => (IGenealogyGraphNode<T>)x); }
[10677]161    }
[10267]162  }
163}
Note: See TracBrowser for help on using the repository browser.