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

Last change on this file since 11253 was 11253, checked in by bburlacu, 7 years ago

#1772: Ported the rest of the changes to the DirectedGraph and Vertex to the GenealogyGraph and GenealogyGraphNode. Adapted tracking operators, analyzers and views.

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