[10264] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2012 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 HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 27 |
|
---|
| 28 | namespace HeuristicLab.EvolutionaryTracking {
|
---|
| 29 | [StorableClass]
|
---|
| 30 | public class Vertex : Item, IVertex {
|
---|
| 31 | [Storable]
|
---|
| 32 | private string id;
|
---|
| 33 | public string Id { get { return id; } }
|
---|
| 34 | [Storable]
|
---|
| 35 | private string label;
|
---|
| 36 | public string Label { get { return label; } set { label = value; } }
|
---|
| 37 | [Storable]
|
---|
| 38 | private double weight;
|
---|
| 39 | public double Weight { get { return weight; } set { weight = value; } }
|
---|
| 40 | [Storable]
|
---|
| 41 | private List<IEdge> inEdges;
|
---|
| 42 | public List<IEdge> InEdges { get { return inEdges; } private set { inEdges = value; } }
|
---|
| 43 | [Storable]
|
---|
| 44 | private List<IEdge> outEdges;
|
---|
| 45 | public List<IEdge> OutEdges { get { return outEdges; } private set { outEdges = value; } }
|
---|
| 46 | [Storable]
|
---|
| 47 | private object content;
|
---|
| 48 | public object Content {
|
---|
| 49 | get { return content; }
|
---|
| 50 | set {
|
---|
| 51 | if (value == null)
|
---|
| 52 | throw new ArgumentException("Node content cannot be null.");
|
---|
| 53 | content = value;
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | [StorableConstructor]
|
---|
| 58 | public Vertex(bool deserializing) : base(deserializing) { }
|
---|
| 59 |
|
---|
| 60 | public Vertex() {
|
---|
| 61 | id = Guid.NewGuid().ToString();
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 65 | private void AfterDeserialization() {
|
---|
| 66 | if (id == null) {
|
---|
| 67 | id = Guid.NewGuid().ToString();
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | protected Vertex(Vertex original, Cloner cloner)
|
---|
| 72 | : base(original, cloner) {
|
---|
| 73 | Content = original.Content;
|
---|
| 74 | id = Guid.NewGuid().ToString();
|
---|
| 75 | Label = original.Label;
|
---|
| 76 | Content = original.Content;
|
---|
| 77 | InEdges = new List<IEdge>(original.InEdges);
|
---|
| 78 | OutEdges = new List<IEdge>(original.OutEdges);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 82 | return new Vertex(this, cloner);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | public Vertex(Vertex node) {
|
---|
| 86 | id = Guid.NewGuid().ToString();
|
---|
| 87 | Label = node.Label;
|
---|
| 88 | Content = node.Content;
|
---|
| 89 | InEdges = new List<IEdge>(node.InEdges);
|
---|
| 90 | OutEdges = new List<IEdge>(node.OutEdges);
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | public int InDegree { get { return InEdges == null ? 0 : InEdges.Count; } }
|
---|
| 94 | public int OutDegree { get { return OutEdges == null ? 0 : OutEdges.Count; } }
|
---|
| 95 | public int Degree { get { return InDegree + OutDegree; } }
|
---|
| 96 |
|
---|
| 97 | /// <summary>
|
---|
| 98 | /// Performs an upward-breath-traversal of the graph using the nodes InEdges
|
---|
| 99 | /// </summary>
|
---|
| 100 | /// <returns>All the ancestors of the current node</returns>
|
---|
| 101 | public IEnumerable<IVertex> Ancestors() {
|
---|
| 102 | // for performance, we use a hashset for lookup and a list for iteration
|
---|
| 103 | var nodes = new HashSet<IVertex> { this };
|
---|
| 104 | var list = new List<IVertex> { this };
|
---|
| 105 | int i = 0;
|
---|
| 106 | while (i != list.Count) {
|
---|
| 107 | if (list[i].InEdges != null) {
|
---|
| 108 | foreach (var e in list[i].InEdges) {
|
---|
| 109 | if (nodes.Contains(e.Source)) continue;
|
---|
| 110 | nodes.Add(e.Source);
|
---|
| 111 | list.Add(e.Source);
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 | ++i;
|
---|
| 115 | }
|
---|
| 116 | return list;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | /// <summary>
|
---|
| 120 | /// Performs a downward-breath-traversal of the genealogy graph using the nodes OutEdges
|
---|
| 121 | /// </summary>
|
---|
| 122 | /// <returns>All the descendants of the current node</returns>
|
---|
| 123 | public IEnumerable<IVertex> Descendants() {
|
---|
| 124 | var nodes = new HashSet<IVertex> { this };
|
---|
| 125 | var list = new List<IVertex> { this };
|
---|
| 126 | int i = 0;
|
---|
| 127 | while (i != list.Count) {
|
---|
| 128 | if (list[i].OutEdges != null) {
|
---|
| 129 | foreach (var e in list[i].OutEdges) {
|
---|
| 130 | if (nodes.Contains(e.Target)) continue;
|
---|
| 131 | nodes.Add(e.Target);
|
---|
| 132 | list.Add(e.Target);
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 | ++i;
|
---|
| 136 | }
|
---|
| 137 | return list;
|
---|
| 138 | }
|
---|
| 139 | // this method can be used to add arcs towards targets that are not visible in the graph
|
---|
| 140 | // (they do not appear in the nodes Dictionary). It is the programmers responsibility to
|
---|
| 141 | // enforce the correct and desired behavior.
|
---|
| 142 | public void AddForwardArc(IVertex target, double weight = 0.0, object data = null) {
|
---|
| 143 | var e = new Arc { Source = this, Target = target, Data = data, Weight = weight };
|
---|
| 144 | if (OutEdges == null) OutEdges = new List<IEdge> { e };
|
---|
| 145 | else OutEdges.Add(e);
|
---|
| 146 | }
|
---|
| 147 | public void AddForwardArc(IEdge arc) {
|
---|
| 148 | if (arc.Source == null) { arc.Source = this; }
|
---|
| 149 | if (arc.Source != this) { throw new Exception("AddForwardArc: Source should be equal to this."); }
|
---|
| 150 | if (OutEdges == null) { OutEdges = new List<IEdge> { arc }; } else { OutEdges.Add(arc); }
|
---|
| 151 | }
|
---|
| 152 | public void AddReverseArc(IEdge arc) {
|
---|
| 153 | if (arc.Target == null) { arc.Target = this; }
|
---|
| 154 | if (arc.Target != this) { throw new Exception("AddReverseArc: Target should be equal to this."); };
|
---|
| 155 | if (InEdges == null) { InEdges = new List<IEdge> { arc }; } else { InEdges.Add(arc); }
|
---|
| 156 | }
|
---|
| 157 | public void AddReverseArc(IVertex source, double weight = 0.0, object data = null) {
|
---|
| 158 | var e = new Arc { Source = source, Target = this, Data = data, Weight = weight };
|
---|
| 159 | if (InEdges == null) InEdges = new List<IEdge> { e };
|
---|
| 160 | else InEdges.Add(e);
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 | }
|
---|