source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolGraph/SymbolGraph.cs @ 10886

Last change on this file since 10886 was 10886, checked in by bburlacu, 8 years ago

#1772: Simplified GenealogyGraph (and related components) API in an effort to improve speed and memory consumption (eg., by sharing the same arc when walking the graph in both directions: parent-child and child-parent).

File size: 2.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Core;
25using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
26using HeuristicLab.EvolutionTracking;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
30  [Item("Symbol graph", "A graph used to store the relationship between symbols in a population of individuals")]
31  [StorableClass]
32  public class SymbolGraph : DirectedGraph {
33    [Storable]
34    private readonly Dictionary<string, SymbolNode> nodeMap = new Dictionary<string, SymbolNode>();
35
36    public void AddVertex(SymbolNode node) {
37      if (nodeMap.ContainsKey(node.Label)) return; // node already present? do nothing
38      nodeMap[node.Label] = node;
39      base.AddVertex(node);
40    }
41
42    public bool ContainsKey(string key) {
43      return nodeMap.ContainsKey(key);
44    }
45
46    public SymbolNode GetNode(string key) {
47      SymbolNode node;
48      nodeMap.TryGetValue(key, out node);
49      return node;
50    }
51  }
52
53  [StorableClass]
54  public class SymbolNode : Vertex {
55    public ISymbol Symbol { get; set; }
56    public Dictionary<int, int> Positions { get; set; }
57    public double AverageArity { get; set; }
58
59    public new List<SymbolArc> InArcs {
60      get { return base.InArcs == null ? null : base.InArcs.Cast<SymbolArc>().ToList(); }
61    }
62
63    public new List<SymbolArc> OutArcs {
64      get { return base.OutArcs == null ? null : base.OutArcs.Cast<SymbolArc>().ToList(); }
65    }
66
67    public SymbolNode() {
68    }
69  }
70
71  public class SymbolArc : IArc {
72    public IVertex Source { get; set; }
73    public IVertex Target { get; set; }
74    public double Weight { get; set; }
75
76    public object Data { get; set; }
77  }
78}
Note: See TracBrowser for help on using the repository browser.