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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.EvolutionTracking {
|
---|
30 | [StorableClass]
|
---|
31 | [Item("GenealogGraphNode", "A class representing a node in the GenealogyGraph")]
|
---|
32 | public class GenealogyGraphNode : Vertex<IDeepCloneable>, 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 | protected GenealogyGraphNode(GenealogyGraphNode original, Cloner cloner)
|
---|
41 | : base(original, cloner) {
|
---|
42 | Quality = original.Quality;
|
---|
43 | Rank = original.Rank;
|
---|
44 | IsElite = original.IsElite;
|
---|
45 | Id = Guid.NewGuid().ToString();
|
---|
46 | }
|
---|
47 |
|
---|
48 | public GenealogyGraphNode() {
|
---|
49 | Id = Guid.NewGuid().ToString();
|
---|
50 | }
|
---|
51 |
|
---|
52 | public GenealogyGraphNode(IDeepCloneable data)
|
---|
53 | : this() {
|
---|
54 | Data = data;
|
---|
55 | }
|
---|
56 |
|
---|
57 | public new IEnumerable<IGenealogyGraphArc> InArcs {
|
---|
58 | get { return base.InArcs.Select(x => (IGenealogyGraphArc)x); }
|
---|
59 | }
|
---|
60 |
|
---|
61 | public new IEnumerable<IGenealogyGraphArc> OutArcs {
|
---|
62 | get { return base.OutArcs.Select(x => (IGenealogyGraphArc)x); }
|
---|
63 | }
|
---|
64 |
|
---|
65 | public IEnumerable<IGenealogyGraphNode> Parents {
|
---|
66 | get { return base.InArcs.Select(x => (IGenealogyGraphNode)x.Source); }
|
---|
67 | }
|
---|
68 |
|
---|
69 | public IEnumerable<IGenealogyGraphNode> Children {
|
---|
70 | get { return base.OutArcs.Select(x => (IGenealogyGraphNode)x.Target); }
|
---|
71 | }
|
---|
72 |
|
---|
73 | public IEnumerable<IGenealogyGraphNode> Ancestors {
|
---|
74 | get {
|
---|
75 | if (!Parents.Any())
|
---|
76 | return Enumerable.Empty<IGenealogyGraphNode>();
|
---|
77 | // for performance, we use a hashset for lookup and a list for iteration
|
---|
78 | var nodes = new HashSet<IGenealogyGraphNode>(Parents);
|
---|
79 | var list = new List<IGenealogyGraphNode>(nodes);
|
---|
80 | int i = 0;
|
---|
81 | while (i != list.Count) {
|
---|
82 | foreach (var arc in list[i].InArcs) {
|
---|
83 | if (nodes.Add(arc.Source))
|
---|
84 | list.Add(arc.Source);
|
---|
85 | }
|
---|
86 | ++i;
|
---|
87 | }
|
---|
88 | return list;
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | public IEnumerable<IGenealogyGraphNode> Descendants {
|
---|
93 | get {
|
---|
94 | var nodes = new HashSet<IGenealogyGraphNode>(Children);
|
---|
95 | var list = new List<IGenealogyGraphNode>(nodes);
|
---|
96 | int i = 0;
|
---|
97 | while (i != list.Count) {
|
---|
98 | foreach (var e in list[i].OutArcs) {
|
---|
99 | if (nodes.Add(e.Target))
|
---|
100 | list.Add(e.Target);
|
---|
101 | }
|
---|
102 | ++i;
|
---|
103 | }
|
---|
104 | return list;
|
---|
105 | }
|
---|
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 | }
|
---|
133 |
|
---|
134 | [StorableClass]
|
---|
135 | [Item("GenealogyGraphNode", "A genealogy graph node which also has a Content")]
|
---|
136 | public class GenealogyGraphNode<T> : GenealogyGraphNode, IGenealogyGraphNode<T> where T : class, IItem {
|
---|
137 | public new T Data {
|
---|
138 | get { return (T)base.Data; }
|
---|
139 | set { base.Data = value; }
|
---|
140 | }
|
---|
141 |
|
---|
142 | [StorableConstructor]
|
---|
143 | protected GenealogyGraphNode(bool deserializing) : base(deserializing) { }
|
---|
144 |
|
---|
145 | public GenealogyGraphNode(IDeepCloneable content) : base(content) { }
|
---|
146 |
|
---|
147 | protected GenealogyGraphNode(GenealogyGraphNode<T> original, Cloner cloner)
|
---|
148 | : base(original, cloner) {
|
---|
149 | }
|
---|
150 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
151 | return new GenealogyGraphNode<T>(this, cloner);
|
---|
152 | }
|
---|
153 | public new IEnumerable<IGenealogyGraphNode<T>> Parents {
|
---|
154 | get { return base.Parents.Select(x => (IGenealogyGraphNode<T>)x); }
|
---|
155 | }
|
---|
156 | public new IEnumerable<IGenealogyGraphNode<T>> Children {
|
---|
157 | get { return base.Children.Select(x => (IGenealogyGraphNode<T>)x); }
|
---|
158 | }
|
---|
159 | public new IEnumerable<IGenealogyGraphNode<T>> Ancestors {
|
---|
160 | get { return base.Ancestors.Select(x => (IGenealogyGraphNode<T>)x); }
|
---|
161 | }
|
---|
162 | public new IEnumerable<IGenealogyGraphNode<T>> Descendants {
|
---|
163 | get { return base.Descendants.Select(x => (IGenealogyGraphNode<T>)x); }
|
---|
164 | }
|
---|
165 | }
|
---|
166 | }
|
---|