Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/GenealogyGraph.cs @ 8555

Last change on this file since 8555 was 8555, checked in by bburlacu, 12 years ago

#1772: Introduced more specific graph class for symbolic expression problems.

File size: 3.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 System.Drawing;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.EvolutionaryTracking {
31  [Item("Genealogy graph", "Generic class for tracking the evolution of objects.")]
32  [StorableClass]
33  public class GenealogyGraph<T> : NamedItem, IGenealogyGraph<T> where T : GenealogyGraphNode {
34    private readonly List<T> nodes; // graph will consist of a set of nodes of type T
35    public List<T> Nodes {
36      get { return nodes; }
37    }
38
39    public GenealogyGraph() {
40      nodes = new List<T>();
41    }
42
43    public GenealogyGraph(GenealogyGraph<T> g) {
44      nodes = new List<T>(g.Nodes);
45    }
46
47    public override IDeepCloneable Clone(Cloner cloner) {
48      return new GenealogyGraph<T>(this, cloner);
49    }
50
51    public override Image ItemImage {
52      get { return Common.Resources.VSImageLibrary.Graph; }
53    }
54
55    [StorableConstructor]
56    protected GenealogyGraph(bool serializing)
57      : base(serializing) {
58    }
59
60    protected GenealogyGraph(GenealogyGraph<T> original, Cloner cloner)
61      : base(original, cloner) {
62      nodes = new List<T>();
63    }
64
65    public virtual bool HasNode(T t) {
66      return nodes.Contains(t);
67    }
68
69    public virtual GenealogyGraphNode GetNode(T t) {
70      var node = Nodes.First(x => x == t);
71      return node;
72    }
73
74    public virtual bool IsEmpty {
75      get { return !nodes.Any(); }
76    }
77
78    public virtual bool Any(Func<T, bool> predicate) {
79      return nodes.Any(predicate);
80    }
81
82    public virtual void Clear() {
83      nodes.Clear();
84    }
85
86    /// <summary>
87    /// Adds a node representing an individual
88    /// </summary>
89    /// <param name="node">The node to add</param>
90    public virtual void AddNode(T node) {
91      Nodes.Add(node);
92    }
93
94    /// <summary>
95    /// Remove a node from the graph along with edges associated with it
96    /// </summary>
97    /// <param name="t">The graph node</param>
98    public virtual void RemoveNode(T t) {
99      Nodes.Remove(t);
100    }
101
102    public double AverageDegree {
103      get { return Nodes.Average(x => x.Degree); }
104    }
105
106    /// <summary>
107    /// Adds a graph arc from a to b
108    /// </summary>
109    /// <param name="obj1"></param>
110    /// <param name="obj2"></param>
111    public virtual void AddArc(T srcNode, T destNode, object data1 = null, object data2 = null) {
112      if (srcNode == null || destNode == null) return;
113      srcNode.AddForwardArc(destNode, data1);
114      destNode.AddReverseArc(srcNode, data2);
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.