Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/28/14 16:23:40 (10 years ago)
Author:
bburlacu
Message:

#1772: Improved usage of GenealogyGraph and GenealogyGraphNode classes. Made some progress on building block tracing.

Location:
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Analyzers/GenealogyAnalyzer.cs

    r10675 r10677  
    230230          previousVertex.AddForwardArc(vertex);
    231231          vertex.AddReverseArc(previousVertex);
    232           vertex.InArcs.Last().Data = previousVertex.InArcs.Last().Data; // save the fragment in case there is one
    233           vertex.Id = previousVertex.Id;
     232          vertex.Id = previousVertex.Id; // another way would be to introduce the vertex.Id into the scope of the elite
     233          if (previousVertex.InArcs.Any()) {
     234            vertex.InArcs.Last().Data = previousVertex.InArcs.Last().Data; // save the fragment in case there is one
     235          }
    234236        }
    235237      }
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/DirectedGraph/DirectedGraph.cs

    r10300 r10677  
    7777    public virtual void Clear() {
    7878      nodes.Clear();
     79      contentMap.Clear();
    7980    }
    8081    public virtual void AddVertex(IVertex vertex) {
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/DirectedGraph/Interfaces/IVertex.cs

    r10650 r10677  
    2626  public interface IVertex : IItem {
    2727    string Id { get; }
    28     List<IArc> InArcs { get; }
    29     List<IArc> OutArcs { get; }
     28    IEnumerable<IArc> InArcs { get; set; }
     29    IEnumerable<IArc> OutArcs { get; set; }
    3030
    3131    int InDegree { get; }
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/DirectedGraph/Vertex.cs

    r10300 r10677  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Linq;
    2425using HeuristicLab.Common;
    2526using HeuristicLab.Core;
     
    4344    [Storable]
    4445    private List<IArc> inArcs;
    45     public List<IArc> InArcs { get { return inArcs; } protected set { inArcs = value; } }
     46    public IEnumerable<IArc> InArcs {
     47      get { return inArcs ?? Enumerable.Empty<IArc>(); }
     48      set { inArcs = value.ToList(); }
     49    }
    4650    [Storable]
    4751    private List<IArc> outArcs;
    48     public List<IArc> OutArcs { get { return outArcs; } protected set { outArcs = value; } }
     52    public IEnumerable<IArc> OutArcs {
     53      get { return outArcs ?? Enumerable.Empty<IArc>(); }
     54      set { outArcs = value.ToList(); }
     55    }
    4956
    5057    [StorableConstructor]
     
    6168      Id = Guid.NewGuid().ToString();
    6269      Label = original.Label;
    63       InArcs = new List<IArc>(original.InArcs);
    64       OutArcs = new List<IArc>(original.OutArcs);
     70      inArcs = new List<IArc>(original.InArcs);
     71      outArcs = new List<IArc>(original.OutArcs);
    6572    }
    6673
     
    8188    public void AddForwardArc(IVertex target, double w = 0.0, object data = null) {
    8289      var arc = new Arc { Source = this, Target = target, Data = data, Weight = w };
    83       if (OutArcs == null) OutArcs = new List<IArc> { arc };
    84       else OutArcs.Add(arc);
     90      if (outArcs == null) outArcs = new List<IArc> { arc };
     91      else outArcs.Add(arc);
    8592    }
    8693    public void AddForwardArc(IArc arc) {
    8794      if (arc.Source == null) { arc.Source = this; }
    8895      if (arc.Source != this) { throw new Exception("AddForwardArc: Source should be equal to this."); }
    89       if (OutArcs == null) { OutArcs = new List<IArc> { arc }; } else { OutArcs.Add(arc); }
     96      if (outArcs == null) { outArcs = new List<IArc> { arc }; } else { outArcs.Add(arc); }
     97    }
     98    public void AddReverseArc(IVertex source, double w = 0.0, object data = null) {
     99      var arc = new Arc { Source = source, Target = this, Data = data, Weight = w };
     100      if (inArcs == null) inArcs = new List<IArc> { arc };
     101      else inArcs.Add(arc);
    90102    }
    91103    public void AddReverseArc(IArc arc) {
    92104      if (arc.Target == null) { arc.Target = this; }
    93105      if (arc.Target != this) { throw new Exception("AddReverseArc: Target should be equal to this."); };
    94       if (InArcs == null) { InArcs = new List<IArc> { arc }; } else { InArcs.Add(arc); }
    95     }
    96     public void AddReverseArc(IVertex source, double w = 0.0, object data = null) {
    97       var arc = new Arc { Source = source, Target = this, Data = data, Weight = w };
    98       if (InArcs == null) InArcs = new List<IArc> { arc };
    99       else InArcs.Add(arc);
     106      if (inArcs == null) { inArcs = new List<IArc> { arc }; } else { inArcs.Add(arc); }
    100107    }
    101108
    102     public int InDegree { get { return InArcs == null ? 0 : InArcs.Count; } }
    103     public int OutDegree { get { return OutArcs == null ? 0 : OutArcs.Count; } }
     109    public int InDegree { get { return InArcs.Count(); } }
     110    public int OutDegree { get { return OutArcs.Count(); } }
    104111    public int Degree { get { return InDegree + OutDegree; } }
    105112  }
    106 
    107113
    108114  [StorableClass]
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/GenealogyGraph.cs

    r10650 r10677  
    3737      set { ranks = value; }
    3838    }
     39    public new IEnumerable<IGenealogyGraphNode> Nodes {
     40      get { return from n in base.Nodes select (IGenealogyGraphNode)n; }
     41    }
    3942    protected GenealogyGraph(GenealogyGraph original, Cloner cloner)
    4043      : base(original, cloner) {
     
    7477      }
    7578    }
     79
     80    public override void Clear() {
     81      base.Clear();
     82      ranks.Clear();
     83    }
    7684  }
    7785
     
    7987  [Item("GenealogyGraph", "")]
    8088  public class GenealogyGraph<T> : DirectedGraph, IGenealogyGraph<T> where T : class, IItem {
     89    // members and properties
    8190    [Storable]
    8291    private Dictionary<double, LinkedList<IGenealogyGraphNode>> ranks;
     
    8594      set { ranks = value; }
    8695    }
     96    public new IEnumerable<IGenealogyGraphNode<T>> Nodes {
     97      get { return from n in base.Nodes select (IGenealogyGraphNode<T>)n; }
     98    }
     99    // contructors
    87100    protected GenealogyGraph(GenealogyGraph<T> original, Cloner cloner)
    88101      : base(original, cloner) {
     
    96109      Ranks = new Dictionary<double, LinkedList<IGenealogyGraphNode>>();
    97110    }
     111    // methods
    98112    public override void AddVertex(IVertex vertex) {
    99113      var node = (IGenealogyGraphNode<T>)vertex;
     
    110124      base.RemoveVertex(vertex);
    111125    }
     126
     127    IEnumerable<IGenealogyGraphNode> IGenealogyGraph.Nodes {
     128      get { return Nodes; }
     129    }
     130
    112131    public event EventHandler GraphUpdated;
    113132    private void OnGraphUpdated(object sender, EventArgs args) {
     
    115134      if (updated != null) updated(sender, args);
    116135    }
    117 
    118136    public new List<IGenealogyGraphNode<T>> this[object content] {
    119137      get {
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/GenealogyGraphNode.cs

    r10300 r10677  
    4141    public GenealogyGraphNode() { }
    4242
    43     public new List<IGenealogyGraphArc> InArcs {
     43    public new IEnumerable<IGenealogyGraphArc> InArcs {
    4444      get {
    45         return base.InArcs == null ? null : base.InArcs.Cast<IGenealogyGraphArc>().ToList();
     45        return base.InArcs.Cast<IGenealogyGraphArc>().ToList();
    4646      }
     47      set { base.InArcs = value; }
    4748    }
    48     public new List<IGenealogyGraphArc> OutArcs {
     49    public new IEnumerable<IGenealogyGraphArc> OutArcs {
    4950      get {
    50         return base.OutArcs == null ? null : base.OutArcs.Cast<IGenealogyGraphArc>().ToList();
     51        return base.OutArcs.Cast<IGenealogyGraphArc>().ToList();
    5152      }
     53      set { base.InArcs = value; }
    5254    }
    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) {
     55    public IEnumerable<IGenealogyGraphNode> Ancestors {
     56      get {
     57        // for performance, we use a hashset for lookup and a list for iteration
     58        var nodes = new HashSet<IGenealogyGraphNode> { this };
     59        var list = new List<IGenealogyGraphNode> { this };
     60        int i = 0;
     61        while (i != list.Count) {
    6062          foreach (var e in list[i].InArcs) {
    6163            if (nodes.Contains(e.Source)) continue;
     
    6365            list.Add(e.Source);
    6466          }
     67          ++i;
    6568        }
    66         ++i;
     69        return list;
    6770      }
    68       return list;
    6971    }
    7072    /// <summary>
     
    7274    /// </summary>
    7375    /// <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) {
     76    public IEnumerable<IGenealogyGraphNode> Descendants {
     77      get {
     78        var nodes = new HashSet<IGenealogyGraphNode> { this };
     79        var list = new List<IGenealogyGraphNode> { this };
     80        int i = 0;
     81        while (i != list.Count) {
    8082          foreach (var e in list[i].OutArcs) {
    8183            if (nodes.Contains(e.Target)) continue;
     
    8385            list.Add(e.Target);
    8486          }
     87          ++i;
    8588        }
    86         ++i;
     89        return list;
    8790      }
    88       return list;
    8991    }
    9092    [Storable]
     
    118120      base.AddReverseArc(arc);
    119121    }
     122
     123    public IEnumerable<IGenealogyGraphNode> Parents {
     124      get {
     125        return InArcs == null ? Enumerable.Empty<IGenealogyGraphNode>() : InArcs.Select(a => a.Source);
     126      }
     127    }
     128
     129    public IEnumerable<IGenealogyGraphNode> Children {
     130      get { return OutArcs == null ? Enumerable.Empty<IGenealogyGraphNode>() : OutArcs.Select(a => a.Source); }
     131    }
    120132  }
    121133
     
    135147      return new GenealogyGraphNode<T>(this, cloner);
    136148    }
     149
     150    public new IEnumerable<IGenealogyGraphNode<T>> Parents {
     151      get {
     152        return base.Parents.Select(p => (IGenealogyGraphNode<T>)p);
     153      }
     154    }
     155
     156    public new IEnumerable<IGenealogyGraphNode<T>> Children {
     157      get {
     158        return base.Children.Select(c => (IGenealogyGraphNode<T>)c);
     159      }
     160    }
     161
     162    public new IEnumerable<IGenealogyGraphNode<T>> Ancestors {
     163      get { return base.Ancestors.Select(x => (IGenealogyGraphNode<T>)x); }
     164    }
     165
     166    public new IEnumerable<IGenealogyGraphNode<T>> Descendants {
     167      get { return base.Descendants.Select(x => (IGenealogyGraphNode<T>)x); }
     168    }
    137169  }
    138170}
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/Interfaces/IGenealogyGraph.cs

    r10650 r10677  
    2626  public interface IGenealogyGraph : IDirectedGraph {
    2727    Dictionary<double, LinkedList<IGenealogyGraphNode>> Ranks { get; }
     28    new IEnumerable<IGenealogyGraphNode> Nodes { get; }
    2829  }
    2930
    30   public interface IGenealogyGraph<T> : IGenealogyGraph where T : class,IItem { }
     31  public interface IGenealogyGraph<T> : IGenealogyGraph where T : class, IItem {
     32    new IEnumerable<IGenealogyGraphNode<T>> Nodes { get; }
     33  }
    3134}
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/Interfaces/IGenealogyGraphNode.cs

    r10650 r10677  
    2222using System;
    2323using System.Collections.Generic;
    24 using HeuristicLab.Core;
    2524
    2625namespace HeuristicLab.EvolutionTracking {
    2726  public interface IGenealogyGraphNode : IVertex, IComparable<IGenealogyGraphNode> {
    28     IEnumerable<IGenealogyGraphNode> Ancestors();
    29     IEnumerable<IGenealogyGraphNode> Descendants();
    30     new List<IGenealogyGraphArc> InArcs { get; }
    31     new List<IGenealogyGraphArc> OutArcs { get; }
    32 
    3327    double Rank { get; set; }
    3428    double Quality { get; set; }
    3529    bool IsElite { get; set; }
     30
     31    IEnumerable<IGenealogyGraphNode> Parents { get; }
     32    IEnumerable<IGenealogyGraphNode> Children { get; }
     33    IEnumerable<IGenealogyGraphNode> Ancestors { get; }
     34    IEnumerable<IGenealogyGraphNode> Descendants { get; }
     35    new IEnumerable<IGenealogyGraphArc> InArcs { get; set; }
     36    new IEnumerable<IGenealogyGraphArc> OutArcs { get; set; }
    3637  }
    3738
    38   public interface IGenealogyGraphNode<T> : IGenealogyGraphNode, IVertex<T> where T : class { }
     39  public interface IGenealogyGraphNode<T> : IGenealogyGraphNode, IVertex<T> where T : class {
     40    new IEnumerable<IGenealogyGraphNode<T>> Parents { get; }
     41    new IEnumerable<IGenealogyGraphNode<T>> Children { get; }
     42
     43    new IEnumerable<IGenealogyGraphNode<T>> Ancestors { get; }
     44    new IEnumerable<IGenealogyGraphNode<T>> Descendants { get; }
     45  }
    3946}
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Operators/BeforeManipulatorOperator.cs

    r10650 r10677  
    2020#endregion
    2121
     22using System.Collections.Generic;
    2223using System.Linq;
    2324using HeuristicLab.Common;
     
    5556        // adjust parent-child(clone) relationship in the graph
    5657        var parents = vChild.InArcs.Select(a => a.Source);
    57         vChild.InArcs.Clear();
     58        vChild.InArcs = new List<IGenealogyGraphArc>();
    5859        foreach (var p in parents) {
    5960          foreach (var a in p.OutArcs) {
Note: See TracChangeset for help on using the changeset viewer.