Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/DirectedGraph/Vertex.cs @ 9082

Last change on this file since 9082 was 9082, checked in by bburlacu, 11 years ago

#1772: Renamed and refactored genealogy graph components. Added SymbolGraph and FPGraph (frequent pattern graph). Added evolvability and genetic operator average improvement analyzer.

File size: 4.8 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
25
26namespace 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
103    // this method can be used to add arcs towards targets that are not visible in the graph
104    // (they do not appear in the nodes Dictionary). It is the programmers responsibility to
105    // enforce the correct and desired behavior.
106    public void AddForwardArc(IVertex target, double weight = 0.0, object data = null) {
107      var e = new Arc { Source = this, Target = target, Data = data, Weight = weight };
108      if (OutEdges == null) OutEdges = new List<Arc> { e };
109      else OutEdges.Add(e);
110    }
111    public void AddForwardArc(Arc arc) {
112      if (arc.Source == null) { arc.Source = this; }
113      if (arc.Source != this) { throw new Exception("AddForwardArc: Source should be equal to this."); }
114      if (OutEdges == null) { OutEdges = new List<Arc> { arc }; } else { OutEdges.Add(arc); }
115    }
116
117    public void AddReverseArc(Arc arc) {
118      if (arc.Target == null) { arc.Target = this; }
119      if (arc.Target != this) { throw new Exception("AddReverseArc: Target should be equal to this."); };
120      if (InEdges == null) { InEdges = new List<Arc> { arc }; } else { InEdges.Add(arc); }
121    }
122
123
124    public void AddReverseArc(IVertex source, double weight = 0.0, object data = null) {
125      var e = new Arc { Source = source, Target = this, Data = data, Weight = weight };
126      if (InEdges == null) InEdges = new List<Arc> { e };
127      else InEdges.Add(e);
128    }
129  }
130}
Note: See TracBrowser for help on using the repository browser.