[10347] | 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 |
|
---|
| 22 | using System.Collections.Generic;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 26 | using HeuristicLab.EvolutionTracking;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 |
|
---|
| 29 | namespace 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 | void AddArc(SymbolNode source, SymbolNode target, double weight, object data = null) {
|
---|
| 53 | source.AddForwardArc(target, weight, data);
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | [StorableClass]
|
---|
| 58 | public class SymbolNode : Vertex {
|
---|
| 59 | public ISymbol Symbol { get; set; }
|
---|
| 60 | public Dictionary<int, int> Positions { get; set; }
|
---|
| 61 | public double AverageArity { get; set; }
|
---|
| 62 |
|
---|
| 63 | public new List<SymbolArc> InArcs {
|
---|
| 64 | get { return base.InArcs == null ? null : base.InArcs.Cast<SymbolArc>().ToList(); }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | public new List<SymbolArc> OutArcs {
|
---|
| 68 | get { return base.OutArcs == null ? null : base.OutArcs.Cast<SymbolArc>().ToList(); }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | public SymbolNode() {
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | public class SymbolArc : IArc {
|
---|
| 76 | public IVertex Source { get; set; }
|
---|
| 77 | public IVertex Target { get; set; }
|
---|
| 78 | public double Weight { get; set; }
|
---|
| 79 |
|
---|
| 80 | public object Data { get; set; }
|
---|
| 81 | }
|
---|
| 82 | }
|
---|