[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;
|
---|
[17434] | 25 | using HEAL.Attic;
|
---|
[10267] | 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
[10271] | 30 | namespace HeuristicLab.EvolutionTracking {
|
---|
[10267] | 31 | [Item("GenealogGraphNode", "A class representing a node in the GenealogyGraph")]
|
---|
[17434] | 32 | [StorableType("3112AC58-F11A-435A-A669-8C9B6EF88F40")]
|
---|
[11925] | 33 | public class GenealogyGraphNode : Vertex<IDeepCloneable>, IGenealogyGraphNode {
|
---|
[10267] | 34 | [StorableConstructor]
|
---|
[17434] | 35 | protected GenealogyGraphNode(StorableConstructorFlag _) : base(_) { }
|
---|
[11253] | 36 |
|
---|
[10267] | 37 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 38 | return new GenealogyGraphNode(this, cloner);
|
---|
| 39 | }
|
---|
[11253] | 40 |
|
---|
[10267] | 41 | protected GenealogyGraphNode(GenealogyGraphNode original, Cloner cloner)
|
---|
| 42 | : base(original, cloner) {
|
---|
[11253] | 43 | Quality = original.Quality;
|
---|
| 44 | Rank = original.Rank;
|
---|
| 45 | IsElite = original.IsElite;
|
---|
| 46 | Id = Guid.NewGuid().ToString();
|
---|
[10267] | 47 | }
|
---|
| 48 |
|
---|
[11925] | 49 | public GenealogyGraphNode() {
|
---|
[11233] | 50 | Id = Guid.NewGuid().ToString();
|
---|
| 51 | }
|
---|
[10267] | 52 |
|
---|
[11925] | 53 | public GenealogyGraphNode(IDeepCloneable data)
|
---|
| 54 | : this() {
|
---|
| 55 | Data = data;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[10677] | 58 | public new IEnumerable<IGenealogyGraphArc> InArcs {
|
---|
[11459] | 59 | get { return base.InArcs.Select(x => (IGenealogyGraphArc)x); }
|
---|
[10267] | 60 | }
|
---|
[11253] | 61 |
|
---|
[10677] | 62 | public new IEnumerable<IGenealogyGraphArc> OutArcs {
|
---|
[11459] | 63 | get { return base.OutArcs.Select(x => (IGenealogyGraphArc)x); }
|
---|
[10267] | 64 | }
|
---|
[11253] | 65 |
|
---|
[11750] | 66 | public IEnumerable<IGenealogyGraphNode> Parents {
|
---|
[11858] | 67 | get { return base.InArcs.Select(x => (IGenealogyGraphNode)x.Source); }
|
---|
[11750] | 68 | }
|
---|
[11852] | 69 |
|
---|
[11750] | 70 | public IEnumerable<IGenealogyGraphNode> Children {
|
---|
[11858] | 71 | get { return base.OutArcs.Select(x => (IGenealogyGraphNode)x.Target); }
|
---|
[11750] | 72 | }
|
---|
| 73 |
|
---|
[10677] | 74 | public IEnumerable<IGenealogyGraphNode> Ancestors {
|
---|
| 75 | get {
|
---|
[11750] | 76 | if (!Parents.Any())
|
---|
| 77 | return Enumerable.Empty<IGenealogyGraphNode>();
|
---|
[10677] | 78 | // for performance, we use a hashset for lookup and a list for iteration
|
---|
[11750] | 79 | var nodes = new HashSet<IGenealogyGraphNode>(Parents);
|
---|
[15757] | 80 | var list = new List<IGenealogyGraphNode>(nodes);
|
---|
[10677] | 81 | int i = 0;
|
---|
| 82 | while (i != list.Count) {
|
---|
[15757] | 83 | foreach (var arc in list[i].InArcs) {
|
---|
| 84 | if (nodes.Add(arc.Source))
|
---|
| 85 | list.Add(arc.Source);
|
---|
[10267] | 86 | }
|
---|
[10677] | 87 | ++i;
|
---|
[10267] | 88 | }
|
---|
[10677] | 89 | return list;
|
---|
[10267] | 90 | }
|
---|
| 91 | }
|
---|
[11750] | 92 |
|
---|
[10677] | 93 | public IEnumerable<IGenealogyGraphNode> Descendants {
|
---|
| 94 | get {
|
---|
[11750] | 95 | var nodes = new HashSet<IGenealogyGraphNode>(Children);
|
---|
[15757] | 96 | var list = new List<IGenealogyGraphNode>(nodes);
|
---|
[10677] | 97 | int i = 0;
|
---|
| 98 | while (i != list.Count) {
|
---|
[10267] | 99 | foreach (var e in list[i].OutArcs) {
|
---|
[15757] | 100 | if (nodes.Add(e.Target))
|
---|
| 101 | list.Add(e.Target);
|
---|
[10267] | 102 | }
|
---|
[10677] | 103 | ++i;
|
---|
[10267] | 104 | }
|
---|
[10677] | 105 | return list;
|
---|
[10267] | 106 | }
|
---|
| 107 | }
|
---|
[11750] | 108 |
|
---|
[10267] | 109 | [Storable]
|
---|
[11233] | 110 | public string Id { get; private set; }
|
---|
| 111 |
|
---|
| 112 | [Storable]
|
---|
[10267] | 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")]
|
---|
[17434] | 136 | [StorableType("68BE4E3D-2578-4C06-BF03-8583BA03022F")]
|
---|
[10300] | 137 | public class GenealogyGraphNode<T> : GenealogyGraphNode, IGenealogyGraphNode<T> where T : class, IItem {
|
---|
[11253] | 138 | public new T Data {
|
---|
| 139 | get { return (T)base.Data; }
|
---|
| 140 | set { base.Data = value; }
|
---|
[10300] | 141 | }
|
---|
[10897] | 142 |
|
---|
[11262] | 143 | [StorableConstructor]
|
---|
[17434] | 144 | protected GenealogyGraphNode(StorableConstructorFlag _) : base(_) { }
|
---|
[10897] | 145 |
|
---|
[11253] | 146 | public GenealogyGraphNode(IDeepCloneable content) : base(content) { }
|
---|
| 147 |
|
---|
[10300] | 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 | }
|
---|
[10677] | 154 | public new IEnumerable<IGenealogyGraphNode<T>> Parents {
|
---|
[11459] | 155 | get { return base.Parents.Select(x => (IGenealogyGraphNode<T>)x); }
|
---|
[10677] | 156 | }
|
---|
| 157 | public new IEnumerable<IGenealogyGraphNode<T>> Children {
|
---|
[11459] | 158 | get { return base.Children.Select(x => (IGenealogyGraphNode<T>)x); }
|
---|
[10677] | 159 | }
|
---|
| 160 | public new IEnumerable<IGenealogyGraphNode<T>> Ancestors {
|
---|
[11459] | 161 | get { return base.Ancestors.Select(x => (IGenealogyGraphNode<T>)x); }
|
---|
[10677] | 162 | }
|
---|
| 163 | public new IEnumerable<IGenealogyGraphNode<T>> Descendants {
|
---|
[11459] | 164 | get { return base.Descendants.Select(x => (IGenealogyGraphNode<T>)x); }
|
---|
[10677] | 165 | }
|
---|
[10267] | 166 | }
|
---|
| 167 | }
|
---|