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.Persistence.Default.CompositeSerializers.Storable;
|
---|
25 |
|
---|
26 | namespace HeuristicLab.EvolutionaryTracking {
|
---|
27 | [StorableClass]
|
---|
28 | public class Vertex : IVertex {
|
---|
29 | public string Id { get; private set; }
|
---|
30 | public string Label { get; set; }
|
---|
31 | public double Weight { get; set; }
|
---|
32 | public List<Arc> InEdges { get; private set; }
|
---|
33 | public List<Arc> OutEdges { get; private set; }
|
---|
34 | private object content;
|
---|
35 | public object Content {
|
---|
36 | get { return content; }
|
---|
37 | set {
|
---|
38 | if (value == null)
|
---|
39 | throw new ArgumentException("Node content cannot be null.");
|
---|
40 | content = value;
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | public Vertex() {
|
---|
45 | Id = Guid.NewGuid().ToString();
|
---|
46 | }
|
---|
47 |
|
---|
48 | public Vertex(Vertex node) {
|
---|
49 | Id = Guid.NewGuid().ToString();
|
---|
50 | Label = node.Label;
|
---|
51 | Content = node.Content;
|
---|
52 | InEdges = new List<Arc>(node.InEdges);
|
---|
53 | OutEdges = new List<Arc>(node.OutEdges);
|
---|
54 | }
|
---|
55 |
|
---|
56 | public int InDegree { get { return InEdges == null ? 0 : InEdges.Count; } }
|
---|
57 | public int OutDegree { get { return OutEdges == null ? 0 : OutEdges.Count; } }
|
---|
58 | public int Degree { get { return InDegree + OutDegree; } }
|
---|
59 |
|
---|
60 | /// <summary>
|
---|
61 | /// Performs an upward-breath-traversal of the graph using the nodes InEdges
|
---|
62 | /// </summary>
|
---|
63 | /// <returns>All the ancestors of the current node</returns>
|
---|
64 | public IEnumerable<IVertex> Ancestors() {
|
---|
65 | // for performance, we use a hashset for lookup and a list for iteration
|
---|
66 | var nodes = new HashSet<IVertex> { this };
|
---|
67 | var list = new List<IVertex> { this };
|
---|
68 | int i = 0;
|
---|
69 | while (i != list.Count) {
|
---|
70 | if (list[i].InEdges != null) {
|
---|
71 | foreach (var e in list[i].InEdges) {
|
---|
72 | if (nodes.Contains(e.Source)) continue;
|
---|
73 | nodes.Add(e.Source);
|
---|
74 | list.Add(e.Source);
|
---|
75 | }
|
---|
76 | }
|
---|
77 | ++i;
|
---|
78 | }
|
---|
79 | return list;
|
---|
80 | }
|
---|
81 |
|
---|
82 | /// <summary>
|
---|
83 | /// Performs a downward-breath-traversal of the genealogy graph using the nodes OutEdges
|
---|
84 | /// </summary>
|
---|
85 | /// <returns>All the descendants of the current node</returns>
|
---|
86 | public IEnumerable<IVertex> Descendants() {
|
---|
87 | var nodes = new HashSet<IVertex> { this };
|
---|
88 | var list = new List<IVertex> { this };
|
---|
89 | int i = 0;
|
---|
90 | while (i != list.Count) {
|
---|
91 | if (list[i].OutEdges != null) {
|
---|
92 | foreach (var e in list[i].OutEdges) {
|
---|
93 | if (nodes.Contains(e.Target)) continue;
|
---|
94 | nodes.Add(e.Target);
|
---|
95 | list.Add(e.Target);
|
---|
96 | }
|
---|
97 | }
|
---|
98 | ++i;
|
---|
99 | }
|
---|
100 | return list;
|
---|
101 | }
|
---|
102 | // this method can be used to add arcs towards targets that are not visible in the graph
|
---|
103 | // (they do not appear in the nodes Dictionary). It is the programmers responsibility to
|
---|
104 | // enforce the correct and desired behavior.
|
---|
105 | public void AddForwardArc(IVertex target, double weight = 0.0, object data = null) {
|
---|
106 | var e = new Arc { Source = this, Target = target, Data = data, Weight = weight };
|
---|
107 | if (OutEdges == null) OutEdges = new List<Arc> { e };
|
---|
108 | else OutEdges.Add(e);
|
---|
109 | }
|
---|
110 | public void AddForwardArc(Arc arc) {
|
---|
111 | if (arc.Source == null) { arc.Source = this; }
|
---|
112 | if (arc.Source != this) { throw new Exception("AddForwardArc: Source should be equal to this."); }
|
---|
113 | if (OutEdges == null) { OutEdges = new List<Arc> { arc }; } else { OutEdges.Add(arc); }
|
---|
114 | }
|
---|
115 | public void AddReverseArc(Arc arc) {
|
---|
116 | if (arc.Target == null) { arc.Target = this; }
|
---|
117 | if (arc.Target != this) { throw new Exception("AddReverseArc: Target should be equal to this."); };
|
---|
118 | if (InEdges == null) { InEdges = new List<Arc> { arc }; } else { InEdges.Add(arc); }
|
---|
119 | }
|
---|
120 | public void AddReverseArc(IVertex source, double weight = 0.0, object data = null) {
|
---|
121 | var e = new Arc { Source = source, Target = this, Data = data, Weight = weight };
|
---|
122 | if (InEdges == null) InEdges = new List<Arc> { e };
|
---|
123 | else InEdges.Add(e);
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|