Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/07/14 21:44:36 (10 years ago)
Author:
bburlacu
Message:

#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:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/DirectedGraph/DirectedGraph.cs

    r10278 r10300  
    3131  [Item("DirectedGraph", "Generic class representing a directed graph with custom vertices and content")]
    3232  [StorableClass]
    33   public class DirectedGraph<TVertex> : Item, IDirectedGraph<TVertex> where TVertex : class,IVertex {
     33  public class DirectedGraph : Item, IDirectedGraph {
    3434    [Storable]
    35     protected readonly List<TVertex> nodes; // for performance reasons, maybe this should be a linked list (fast remove)
    36     public List<TVertex> Nodes {
     35    protected readonly List<IVertex> nodes; // for performance reasons, maybe this should be a linked list (fast remove)
     36    public List<IVertex> Nodes {
    3737      get { return nodes; }
    3838    }
     39    [Storable]
     40    private readonly Dictionary<object, List<IVertex>> contentMap;
    3941    public DirectedGraph() {
    40       nodes = new List<TVertex>();
     42      nodes = new List<IVertex>();
     43      contentMap = new Dictionary<object, List<IVertex>>();
    4144    }
    4245    [StorableConstructor]
     
    4447      : base(serializing) {
    4548    }
    46     protected DirectedGraph(DirectedGraph<TVertex> original, Cloner cloner)
     49    protected DirectedGraph(DirectedGraph original, Cloner cloner)
    4750      : base(original, cloner) {
    48       nodes = new List<TVertex>(original.Nodes);
     51      nodes = new List<IVertex>(original.Nodes);
     52      contentMap = new Dictionary<object, List<IVertex>>(original.contentMap);
    4953    }
    5054    public override IDeepCloneable Clone(Cloner cloner) {
    51       return new DirectedGraph<TVertex>(this, cloner);
     55      return new DirectedGraph(this, cloner);
    5256    }
    53     public bool Contains(TVertex t) {
     57    public bool Contains(IVertex t) {
    5458      return nodes.Contains(t);
    5559    }
    5660
    57     public virtual bool Any(Func<TVertex, bool> predicate) {
     61    public bool Contains(object content) {
     62      return contentMap.ContainsKey(content);
     63    }
     64    public List<IVertex> this[object key] {
     65      get {
     66        List<IVertex> result;
     67        contentMap.TryGetValue(key, out result);
     68        return result;
     69      }
     70    }
     71    public virtual bool Any(Func<IVertex, bool> predicate) {
    5872      return nodes.Any(predicate);
    5973    }
     
    6175      get { return !nodes.Any(); }
    6276    }
    63 
    6477    public virtual void Clear() {
    6578      nodes.Clear();
    6679    }
    67 
    68     public virtual void AddVertex(TVertex vertex) {
     80    public virtual void AddVertex(IVertex vertex) {
    6981      Nodes.Add(vertex);
     82      if (!contentMap.ContainsKey(vertex.Content))
     83        contentMap[vertex.Content] = new List<IVertex>();
     84      contentMap[vertex.Content].Add(vertex);
    7085    }
    7186
    72     public virtual void RemoveVertex(TVertex vertex) {
     87    public virtual void RemoveVertex(IVertex vertex) {
    7388      nodes.Remove(vertex);
    7489    }
     
    7792    }
    7893  }
    79 
    80   [Item("Directed graph", "Generic directed graph base class.")]
    81   [StorableClass]
    82   public class DirectedGraph<TVertex, TContent> : DirectedGraph<TVertex>, IDirectedGraph<TVertex, TContent>
    83     where TVertex : class,IVertex<TContent>
    84     where TContent : class,IItem {
    85 
    86     [StorableConstructor]
    87     protected DirectedGraph(bool deserializing) : base(deserializing) { }
    88 
    89     [Storable]
    90     private readonly Dictionary<TContent, List<TVertex>> contentMap; // reverse mapping of content to the actual vertices
    91     public DirectedGraph() {
    92       contentMap = new Dictionary<TContent, List<TVertex>>();
    93     }
    94     public override IDeepCloneable Clone(Cloner cloner) {
    95       return new DirectedGraph<TVertex, TContent>(this, cloner);
    96     }
    97     protected DirectedGraph(DirectedGraph<TVertex, TContent> original, Cloner cloner)
    98       : base(original, cloner) {
    99       contentMap = new Dictionary<TContent, List<TVertex>>(original.contentMap);
    100     }
    101     public bool Contains(TContent content) {
    102       return contentMap.ContainsKey(content);
    103     }
    104     public List<TVertex> this[TContent key] {
    105       get {
    106         List<TVertex> result = null;
    107         contentMap.TryGetValue(key, out result);
    108         return result;
    109       }
    110       //      set {
    111       //        contentMap[key] = value;
    112       //      }
    113     }
    114     public override void Clear() {
    115       contentMap.Clear();
    116       base.Clear();
    117     }
    118     public override void AddVertex(TVertex vertex) {
    119       if (contentMap.ContainsKey(vertex.Content)) {
    120         contentMap[vertex.Content].Add(vertex);
    121       } else {
    122         contentMap[vertex.Content] = new List<TVertex> { vertex };
    123       }
    124       base.AddVertex(vertex);
    125     }
    126 
    127     public override void RemoveVertex(TVertex vertex) {
    128       if (contentMap.ContainsKey(vertex.Content)) {
    129         contentMap[vertex.Content].Remove(vertex);
    130       }
    131       base.RemoveVertex(vertex);
    132     }
    133   }
    13494}
Note: See TracChangeset for help on using the changeset viewer.