[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] | 22 | using System;
|
---|
[10267] | 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 |
|
---|
[10271] | 29 | namespace 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 |
|
---|
[10677] | 61 | public IEnumerable<IGenealogyGraphNode> Ancestors {
|
---|
| 62 | get {
|
---|
| 63 | // for performance, we use a hashset for lookup and a list for iteration
|
---|
| 64 | var nodes = new HashSet<IGenealogyGraphNode> { this };
|
---|
| 65 | var list = new List<IGenealogyGraphNode> { this };
|
---|
| 66 | int i = 0;
|
---|
| 67 | while (i != list.Count) {
|
---|
[10267] | 68 | foreach (var e in list[i].InArcs) {
|
---|
| 69 | if (nodes.Contains(e.Source)) continue;
|
---|
| 70 | nodes.Add(e.Source);
|
---|
| 71 | list.Add(e.Source);
|
---|
| 72 | }
|
---|
[10677] | 73 | ++i;
|
---|
[10267] | 74 | }
|
---|
[10677] | 75 | return list;
|
---|
[10267] | 76 | }
|
---|
| 77 | }
|
---|
| 78 | /// <summary>
|
---|
| 79 | /// Performs a downward-breath-traversal of the genealogy graph using the nodes OutArcs
|
---|
| 80 | /// </summary>
|
---|
| 81 | /// <returns>All the descendants of the current node</returns>
|
---|
[10677] | 82 | public IEnumerable<IGenealogyGraphNode> Descendants {
|
---|
| 83 | get {
|
---|
| 84 | var nodes = new HashSet<IGenealogyGraphNode> { this };
|
---|
| 85 | var list = new List<IGenealogyGraphNode> { this };
|
---|
| 86 | int i = 0;
|
---|
| 87 | while (i != list.Count) {
|
---|
[10267] | 88 | foreach (var e in list[i].OutArcs) {
|
---|
| 89 | if (nodes.Contains(e.Target)) continue;
|
---|
| 90 | nodes.Add(e.Target);
|
---|
| 91 | list.Add(e.Target);
|
---|
| 92 | }
|
---|
[10677] | 93 | ++i;
|
---|
[10267] | 94 | }
|
---|
[10677] | 95 | return list;
|
---|
[10267] | 96 | }
|
---|
| 97 | }
|
---|
| 98 | [Storable]
|
---|
[11233] | 99 | public string Id { get; private set; }
|
---|
| 100 |
|
---|
| 101 | [Storable]
|
---|
[10267] | 102 | private double rank;
|
---|
| 103 | public double Rank {
|
---|
| 104 | get { return rank; }
|
---|
| 105 | set { rank = value; }
|
---|
| 106 | }
|
---|
| 107 | [Storable]
|
---|
| 108 | private double quality;
|
---|
| 109 | public double Quality {
|
---|
| 110 | get { return quality; }
|
---|
| 111 | set { quality = value; }
|
---|
| 112 | }
|
---|
| 113 | [Storable]
|
---|
| 114 | private bool isElite;
|
---|
| 115 | public bool IsElite {
|
---|
| 116 | get { return isElite; }
|
---|
| 117 | set { isElite = value; }
|
---|
| 118 | }
|
---|
| 119 | public int CompareTo(IGenealogyGraphNode other) {
|
---|
| 120 | return Quality.CompareTo(other.Quality);
|
---|
| 121 | }
|
---|
[10677] | 122 | public IEnumerable<IGenealogyGraphNode> Parents {
|
---|
[10685] | 123 | get { return InArcs.Select(a => a.Source); }
|
---|
[10677] | 124 | }
|
---|
| 125 | public IEnumerable<IGenealogyGraphNode> Children {
|
---|
[10685] | 126 | get { return OutArcs.Select(a => a.Target); }
|
---|
[10677] | 127 | }
|
---|
[10267] | 128 | }
|
---|
| 129 |
|
---|
| 130 | [StorableClass]
|
---|
| 131 | [Item("GenealogyGraphNode", "A genealogy graph node which also has a Content")]
|
---|
[10300] | 132 | public class GenealogyGraphNode<T> : GenealogyGraphNode, IGenealogyGraphNode<T> where T : class, IItem {
|
---|
[11253] | 133 | public new T Data {
|
---|
| 134 | get { return (T)base.Data; }
|
---|
| 135 | set { base.Data = value; }
|
---|
[10300] | 136 | }
|
---|
[10897] | 137 |
|
---|
[11262] | 138 | [StorableConstructor]
|
---|
| 139 | protected GenealogyGraphNode(bool deserializing) : base(deserializing) { }
|
---|
[10897] | 140 |
|
---|
[11253] | 141 | public GenealogyGraphNode(IDeepCloneable content) : base(content) { }
|
---|
| 142 |
|
---|
[10300] | 143 | protected GenealogyGraphNode(GenealogyGraphNode<T> original, Cloner cloner)
|
---|
| 144 | : base(original, cloner) {
|
---|
| 145 | }
|
---|
| 146 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 147 | return new GenealogyGraphNode<T>(this, cloner);
|
---|
| 148 | }
|
---|
[10677] | 149 | public new IEnumerable<IGenealogyGraphNode<T>> Parents {
|
---|
[11459] | 150 | get { return base.Parents.Select(x => (IGenealogyGraphNode<T>)x); }
|
---|
[10677] | 151 | }
|
---|
| 152 | public new IEnumerable<IGenealogyGraphNode<T>> Children {
|
---|
[11459] | 153 | get { return base.Children.Select(x => (IGenealogyGraphNode<T>)x); }
|
---|
[10677] | 154 | }
|
---|
| 155 | public new IEnumerable<IGenealogyGraphNode<T>> Ancestors {
|
---|
[11459] | 156 | get { return base.Ancestors.Select(x => (IGenealogyGraphNode<T>)x); }
|
---|
[10677] | 157 | }
|
---|
| 158 | public new IEnumerable<IGenealogyGraphNode<T>> Descendants {
|
---|
[11459] | 159 | get { return base.Descendants.Select(x => (IGenealogyGraphNode<T>)x); }
|
---|
[10677] | 160 | }
|
---|
[10267] | 161 | }
|
---|
| 162 | }
|
---|