Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/GenealogyGraphNode.cs @ 10458

Last change on this file since 10458 was 10300, checked in by bburlacu, 10 years ago

#1772: Cleaned up the design of the generic genealogy analyzer and related classes, created generic genealogy graph view. Added instrumentation code to TravelingSalesmapProblem.cs allowing genealogy tracking. Merged trunk changes (instrumentation for multi operators).

File size: 4.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.EvolutionTracking {
29  [StorableClass]
30  [Item("GenealogGraphNode", "A class representing a node in the GenealogyGraph")]
31  public class GenealogyGraphNode : Vertex, IGenealogyGraphNode {
32    [StorableConstructor]
33    protected GenealogyGraphNode(bool deserializing) : base(deserializing) { }
34    public override IDeepCloneable Clone(Cloner cloner) {
35      return new GenealogyGraphNode(this, cloner);
36    }
37    protected GenealogyGraphNode(GenealogyGraphNode original, Cloner cloner)
38      : base(original, cloner) {
39    }
40
41    public GenealogyGraphNode() { }
42
43    public new List<IGenealogyGraphArc> InArcs {
44      get {
45        return base.InArcs == null ? null : base.InArcs.Cast<IGenealogyGraphArc>().ToList();
46      }
47    }
48    public new List<IGenealogyGraphArc> OutArcs {
49      get {
50        return base.OutArcs == null ? null : base.OutArcs.Cast<IGenealogyGraphArc>().ToList();
51      }
52    }
53    public IEnumerable<IGenealogyGraphNode> Ancestors() {
54      // for performance, we use a hashset for lookup and a list for iteration
55      var nodes = new HashSet<IGenealogyGraphNode> { this };
56      var list = new List<IGenealogyGraphNode> { this };
57      int i = 0;
58      while (i != list.Count) {
59        if (list[i].InArcs != null) {
60          foreach (var e in list[i].InArcs) {
61            if (nodes.Contains(e.Source)) continue;
62            nodes.Add(e.Source);
63            list.Add(e.Source);
64          }
65        }
66        ++i;
67      }
68      return list;
69    }
70    /// <summary>
71    /// Performs a downward-breath-traversal of the genealogy graph using the nodes OutArcs
72    /// </summary>
73    /// <returns>All the descendants of the current node</returns>
74    public IEnumerable<IGenealogyGraphNode> Descendants() {
75      var nodes = new HashSet<IGenealogyGraphNode> { this };
76      var list = new List<IGenealogyGraphNode> { this };
77      int i = 0;
78      while (i != list.Count) {
79        if (list[i].OutArcs != null) {
80          foreach (var e in list[i].OutArcs) {
81            if (nodes.Contains(e.Target)) continue;
82            nodes.Add(e.Target);
83            list.Add(e.Target);
84          }
85        }
86        ++i;
87      }
88      return list;
89    }
90    [Storable]
91    private double rank;
92    public double Rank {
93      get { return rank; }
94      set { rank = value; }
95    }
96    [Storable]
97    private double quality;
98    public double Quality {
99      get { return quality; }
100      set { quality = value; }
101    }
102    [Storable]
103    private bool isElite;
104    public bool IsElite {
105      get { return isElite; }
106      set { isElite = value; }
107    }
108    public int CompareTo(IGenealogyGraphNode other) {
109      return Quality.CompareTo(other.Quality);
110    }
111
112    public new void AddForwardArc(IVertex target, double w = 0.0, object data = null) {
113      var arc = new GenealogyGraphArc { Source = this, Target = (IGenealogyGraphNode)target, Data = data, Weight = w };
114      base.AddForwardArc(arc);
115    }
116    public new void AddReverseArc(IVertex source, double w = 0.0, object data = null) {
117      var arc = new GenealogyGraphArc { Source = (IGenealogyGraphNode)source, Target = this, Data = data, Weight = w };
118      base.AddReverseArc(arc);
119    }
120  }
121
122  [StorableClass]
123  [Item("GenealogyGraphNode", "A genealogy graph node which also has a Content")]
124  public class GenealogyGraphNode<T> : GenealogyGraphNode, IGenealogyGraphNode<T> where T : class, IItem {
125    public new T Content {
126      get { return (T)base.Content; }
127      set { base.Content = value; }
128    }
129    public GenealogyGraphNode() { }
130    protected GenealogyGraphNode(GenealogyGraphNode<T> original, Cloner cloner)
131      : base(original, cloner) {
132      Content = original.Content;
133    }
134    public override IDeepCloneable Clone(Cloner cloner) {
135      return new GenealogyGraphNode<T>(this, cloner);
136    }
137  }
138}
Note: See TracBrowser for help on using the repository browser.