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
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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.EvolutionTracking {
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
42    public GenealogyGraphNode(object content)
43      : base(content) {
44      Id = Guid.NewGuid().ToString();
45    }
46
47    public new IEnumerable<IGenealogyGraphArc> InArcs {
48      get { return base.InArcs.Cast<IGenealogyGraphArc>(); }
49    }
50    public new IEnumerable<IGenealogyGraphArc> OutArcs {
51      get { return base.OutArcs.Cast<IGenealogyGraphArc>(); }
52    }
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) {
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          }
65          ++i;
66        }
67        return list;
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>
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) {
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          }
85          ++i;
86        }
87        return list;
88      }
89    }
90    [Storable]
91    public string Id { get; private set; }
92
93    [Storable]
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    }
114    public IEnumerable<IGenealogyGraphNode> Parents {
115      get { return InArcs.Select(a => a.Source); }
116    }
117    public IEnumerable<IGenealogyGraphNode> Children {
118      get { return OutArcs.Select(a => a.Target); }
119    }
120  }
121
122  [StorableClass]
123  [Item("GenealogyGraphNode", "A genealogy graph node which also has a Content")]
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    }
129
130    public GenealogyGraphNode(object content) : base(content) { }
131
132    protected GenealogyGraphNode(GenealogyGraphNode<T> original, Cloner cloner)
133      : base(original, cloner) {
134      // we do not clone the content
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.Cast<IGenealogyGraphNode<T>>(); }
142    }
143    public new IEnumerable<IGenealogyGraphNode<T>> Children {
144      get { return base.Children.Cast<IGenealogyGraphNode<T>>(); }
145    }
146    public new IEnumerable<IGenealogyGraphNode<T>> Ancestors {
147      get { return base.Ancestors.Cast<IGenealogyGraphNode<T>>(); }
148    }
149    public new IEnumerable<IGenealogyGraphNode<T>> Descendants {
150      get { return base.Descendants.Cast<IGenealogyGraphNode<T>>(); }
151    }
152  }
153}
Note: See TracBrowser for help on using the repository browser.