Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1772_HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/GenealogyGraphNode.cs @ 17434

Last change on this file since 17434 was 17434, checked in by bburlacu, 4 years ago

#1772: Merge trunk changes and fix all errors and compilation warnings.

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