Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/GenealogyGraph.cs @ 11257

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

#1772:

  • GenealogyAnalyzer: compute relative success ratios per individual and save them in a DataTableHistory chart and as a heat map
  • GenealogyGraph: remove rank if it contains no vertices
  • BeforeManipulatorOperator: avoid "collection was modified" exception
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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.EvolutionTracking {
30  [StorableClass]
31  [Item("GenealogyGraph", "A class representing a genealogy graph")]
32  public class GenealogyGraph : DirectedGraph, IGenealogyGraph {
33    [Storable]
34    private readonly Dictionary<object, IGenealogyGraphNode> contentMap;
35
36    [Storable]
37    private readonly Dictionary<string, IGenealogyGraphNode> idMap;
38
39    [Storable]
40    private Dictionary<double, List<IGenealogyGraphNode>> ranks; // use a linked list for fast insertion/removal
41    public Dictionary<double, List<IGenealogyGraphNode>> Ranks {
42      get { return ranks; }
43      set { ranks = value; }
44    }
45
46    protected GenealogyGraph(GenealogyGraph original, Cloner cloner)
47      : base(original, cloner) {
48      Ranks = new Dictionary<double, List<IGenealogyGraphNode>>(original.Ranks);
49      contentMap = new Dictionary<object, IGenealogyGraphNode>(original.contentMap);
50      idMap = new Dictionary<string, IGenealogyGraphNode>(original.idMap);
51    }
52
53    public override IDeepCloneable Clone(Cloner cloner) {
54      return new GenealogyGraph(this, cloner);
55    }
56
57    [StorableConstructor]
58    protected GenealogyGraph(bool deserializing) : base(deserializing) { }
59    public GenealogyGraph() {
60      Ranks = new Dictionary<double, List<IGenealogyGraphNode>>();
61      contentMap = new Dictionary<object, IGenealogyGraphNode>();
62      idMap = new Dictionary<string, IGenealogyGraphNode>();
63    }
64
65    public new IEnumerable<IGenealogyGraphNode> Vertices {
66      get { return from n in base.Vertices select (IGenealogyGraphNode)n; }
67    }
68
69    public override IArc AddArc(IVertex source, IVertex target) {
70      var arc = new GenealogyGraphArc((IGenealogyGraphNode)source, (IGenealogyGraphNode)target);
71      base.AddArc(arc);
72      return arc;
73    }
74
75    public override void AddVertex(IVertex vertex) {
76      base.AddVertex(vertex);
77      var node = (IGenealogyGraphNode)vertex;
78      if (!Ranks.ContainsKey(node.Rank))
79        Ranks[node.Rank] = new List<IGenealogyGraphNode>();
80      Ranks[node.Rank].Add(node);
81
82      if (contentMap.ContainsKey(node.Data))
83        throw new InvalidOperationException("Duplicate content is not allowed in the genealogy graph.");
84      contentMap[node.Data] = node;
85
86      if (idMap.ContainsKey(node.Id))
87        throw new InvalidOperationException("Duplicate content is not allowed in the genealogy graph.");
88      idMap[node.Id] = node;
89    }
90
91    public override void RemoveVertex(IVertex vertex) {
92      var node = (IGenealogyGraphNode)vertex;
93      contentMap.Remove(node.Data);
94      idMap.Remove(node.Id);
95      if (Ranks.ContainsKey(node.Rank)) {
96        Ranks[node.Rank].Remove(node);
97        if (!Ranks[node.Rank].Any())
98          Ranks.Remove(node.Rank);
99      }
100      base.RemoveVertex(vertex);
101    }
102
103    public IGenealogyGraphNode GetByContent(object content) {
104      IGenealogyGraphNode result;
105      contentMap.TryGetValue(content, out result);
106      return result;
107    }
108
109    public IGenealogyGraphNode GetById(string id) {
110      IGenealogyGraphNode result;
111      idMap.TryGetValue(id, out result);
112      return result;
113    }
114
115    public bool Contains(object content) {
116      return contentMap.ContainsKey(content);
117    }
118
119    public event EventHandler GraphUpdated;
120    private void OnGraphUpdated(object sender, EventArgs args) {
121      var updated = GraphUpdated;
122      if (updated != null) updated(sender, args);
123    }
124
125    public override void Clear() {
126      base.Clear();
127      ranks.Clear();
128    }
129  }
130
131  [Item("GenealogyGraph", "A specialization of the genealogy graph with T as content for vertices")]
132  [StorableClass]
133  public class GenealogyGraph<T> : GenealogyGraph, IGenealogyGraph<T> where T : class, IItem {
134    public new IEnumerable<IGenealogyGraphNode<T>> Vertices {
135      get { return base.Vertices.Cast<IGenealogyGraphNode<T>>(); }
136    }
137  }
138}
Note: See TracBrowser for help on using the repository browser.