#region License Information /* HeuristicLab * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.EvolutionTracking { [StorableClass] [Item("GenealogGraphNode", "A class representing a node in the GenealogyGraph")] public class GenealogyGraphNode : Vertex, IGenealogyGraphNode { [StorableConstructor] protected GenealogyGraphNode(bool deserializing) : base(deserializing) { } public override IDeepCloneable Clone(Cloner cloner) { return new GenealogyGraphNode(this, cloner); } protected GenealogyGraphNode(GenealogyGraphNode original, Cloner cloner) : base(original, cloner) { Quality = original.Quality; Rank = original.Rank; IsElite = original.IsElite; Id = Guid.NewGuid().ToString(); } public GenealogyGraphNode() { Id = Guid.NewGuid().ToString(); } public GenealogyGraphNode(IDeepCloneable data) : this() { Data = data; } public new IEnumerable InArcs { get { return base.InArcs.Select(x => (IGenealogyGraphArc)x); } } public new IEnumerable OutArcs { get { return base.OutArcs.Select(x => (IGenealogyGraphArc)x); } } public IEnumerable Parents { get { return base.InArcs.Select(x => (IGenealogyGraphNode)x.Source); } } public IEnumerable Children { get { return base.OutArcs.Select(x => (IGenealogyGraphNode)x.Target); } } public IEnumerable Ancestors { get { if (!Parents.Any()) return Enumerable.Empty(); // for performance, we use a hashset for lookup and a list for iteration var nodes = new HashSet(Parents); var list = new List(nodes); int i = 0; while (i != list.Count) { foreach (var arc in list[i].InArcs) { if (nodes.Add(arc.Source)) list.Add(arc.Source); } ++i; } return list; } } public IEnumerable Descendants { get { var nodes = new HashSet(Children); var list = new List(nodes); int i = 0; while (i != list.Count) { foreach (var e in list[i].OutArcs) { if (nodes.Add(e.Target)) list.Add(e.Target); } ++i; } return list; } } [Storable] public string Id { get; private set; } [Storable] private double rank; public double Rank { get { return rank; } set { rank = value; } } [Storable] private double quality; public double Quality { get { return quality; } set { quality = value; } } [Storable] private bool isElite; public bool IsElite { get { return isElite; } set { isElite = value; } } public int CompareTo(IGenealogyGraphNode other) { return Quality.CompareTo(other.Quality); } } [StorableClass] [Item("GenealogyGraphNode", "A genealogy graph node which also has a Content")] public class GenealogyGraphNode : GenealogyGraphNode, IGenealogyGraphNode where T : class, IItem { public new T Data { get { return (T)base.Data; } set { base.Data = value; } } [StorableConstructor] protected GenealogyGraphNode(bool deserializing) : base(deserializing) { } public GenealogyGraphNode(IDeepCloneable content) : base(content) { } protected GenealogyGraphNode(GenealogyGraphNode original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new GenealogyGraphNode(this, cloner); } public new IEnumerable> Parents { get { return base.Parents.Select(x => (IGenealogyGraphNode)x); } } public new IEnumerable> Children { get { return base.Children.Select(x => (IGenealogyGraphNode)x); } } public new IEnumerable> Ancestors { get { return base.Ancestors.Select(x => (IGenealogyGraphNode)x); } } public new IEnumerable> Descendants { get { return base.Descendants.Select(x => (IGenealogyGraphNode)x); } } } }