Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1772: Fixed another minor display bug concerning elite individuals. Fixed bug when saving fragments from mutation. Displayed quality as well in the SymbolicExpressionTreeTile.

File size: 5.0 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", "")]
32  public class GenealogyGraph : DirectedGraph, IGenealogyGraph {
33    [Storable]
34    private Dictionary<double, List<IGenealogyGraphNode>> ranks; // use a linked list for fast insertion/removal
35    public Dictionary<double, List<IGenealogyGraphNode>> Ranks {
36      get { return ranks; }
37      set { ranks = value; }
38    }
39    public new IEnumerable<IGenealogyGraphNode> Nodes {
40      get { return from n in base.Nodes select (IGenealogyGraphNode)n; }
41    }
42    protected GenealogyGraph(GenealogyGraph original, Cloner cloner)
43      : base(original, cloner) {
44    }
45    public override IDeepCloneable Clone(Cloner cloner) {
46      return new GenealogyGraph(this, cloner);
47    }
48    [StorableConstructor]
49    protected GenealogyGraph(bool deserializing) : base(deserializing) { }
50    public GenealogyGraph() {
51      Ranks = new Dictionary<double, List<IGenealogyGraphNode>>();
52    }
53    public override void AddVertex(IVertex vertex) {
54      var node = (IGenealogyGraphNode)vertex;
55      if (!Ranks.ContainsKey(node.Rank))
56        Ranks[node.Rank] = new List<IGenealogyGraphNode>();
57      Ranks[node.Rank].Add(node);
58      base.AddVertex(vertex);
59    }
60    public override void RemoveVertex(IVertex vertex) {
61      var node = (IGenealogyGraphNode)vertex;
62      if (Ranks.ContainsKey(node.Rank)) {
63        Ranks[node.Rank].Remove(node);
64      }
65      base.RemoveVertex(vertex);
66    }
67    public event EventHandler GraphUpdated;
68    private void OnGraphUpdated(object sender, EventArgs args) {
69      var updated = GraphUpdated;
70      if (updated != null) updated(sender, args);
71    }
72
73    public new IGenealogyGraphNode this[object content] {
74      get {
75        var result = base[content];
76        return result == null ? null : (IGenealogyGraphNode)result;
77      }
78      set { base[content] = value; }
79    }
80
81    public override void Clear() {
82      base.Clear();
83      ranks.Clear();
84    }
85  }
86
87  [StorableClass]
88  [Item("GenealogyGraph", "")]
89  public class GenealogyGraph<T> : DirectedGraph, IGenealogyGraph<T> where T : class, IItem {
90    // members and properties
91    [Storable]
92    private Dictionary<double, List<IGenealogyGraphNode>> ranks;
93    public Dictionary<double, List<IGenealogyGraphNode>> Ranks {
94      get { return ranks; }
95      set { ranks = value; }
96    }
97    public new IEnumerable<IGenealogyGraphNode<T>> Nodes {
98      get { return from n in base.Nodes select (IGenealogyGraphNode<T>)n; }
99    }
100    // contructors
101    protected GenealogyGraph(GenealogyGraph<T> original, Cloner cloner)
102      : base(original, cloner) {
103    }
104    public override IDeepCloneable Clone(Cloner cloner) {
105      return new GenealogyGraph<T>(this, cloner);
106    }
107    [StorableConstructor]
108    protected GenealogyGraph(bool deserializing) : base(deserializing) { }
109    public GenealogyGraph() {
110      Ranks = new Dictionary<double, List<IGenealogyGraphNode>>();
111    }
112    // methods
113    public override void AddVertex(IVertex vertex) {
114      var node = (IGenealogyGraphNode<T>)vertex;
115      if (!Ranks.ContainsKey(node.Rank)) {
116        Ranks[node.Rank] = new List<IGenealogyGraphNode>();
117      }
118      Ranks[node.Rank].Add(node);
119      base.AddVertex(vertex);
120    }
121    public override void RemoveVertex(IVertex vertex) {
122      var node = (IGenealogyGraphNode<T>)vertex;
123      if (Ranks.ContainsKey(node.Rank)) {
124        Ranks[node.Rank].Remove(node);
125      }
126      base.RemoveVertex(vertex);
127    }
128
129    IEnumerable<IGenealogyGraphNode> IGenealogyGraph.Nodes {
130      get { return Nodes; }
131    }
132
133    public event EventHandler GraphUpdated;
134    private void OnGraphUpdated(object sender, EventArgs args) {
135      var updated = GraphUpdated;
136      if (updated != null) updated(sender, args);
137    }
138    public new IGenealogyGraphNode<T> this[object content] {
139      get {
140        var result = base[content];
141        return result == null ? null : (IGenealogyGraphNode<T>)result;
142      }
143    }
144  }
145}
Note: See TracBrowser for help on using the repository browser.