Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/FPGraph.cs @ 9419

Last change on this file since 9419 was 9419, checked in by bburlacu, 11 years ago

#1772: Refactoring of directed graph components, added code for correctly serializing vertices and edges. Added specific building blocks analyzers and new population diversity analyzer which correctly integrates with the parallel engine.

File size: 2.3 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.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.EvolutionaryTracking {
28  [Item("Symbol graph", "A graph used to store the relationship between symbols in a population of individuals")]
29  [StorableClass]
30  public class FPGraph : GenericGraph<SymbolNode> {
31    [Storable]
32    public Dictionary<int, List<SymbolNode>> Levels { get { return levels; } }
33    private Dictionary<int, List<SymbolNode>> levels = new Dictionary<int, List<SymbolNode>>();
34
35    public void AddNode(SymbolNode node, int level) {
36      if (levels.ContainsKey(level)) {
37        SymbolNode match = levels[level].Find(n => n.Label.Equals(node.Label));
38        if (match == null) {
39          levels[level].Add(node);
40        }
41      } else {
42        levels[level] = new List<SymbolNode> { node };
43      }
44      base.AddNode(node);
45    }
46
47    public bool ContainsKey(string key, int level) {
48      return levels.ContainsKey(level) && levels[level].Select(n => n.Label).Contains(key);
49    }
50
51    public SymbolNode GetNode(string key, int level) {
52      SymbolNode node = null;
53      if (levels.ContainsKey(level))
54        node = levels[level].Find(n => n.Label.Equals(key));
55      return node;
56    }
57
58    void AddArc(SymbolNode source, SymbolNode target, double weight, object data = null) {
59      source.AddForwardArc(target, weight, data);
60    }
61  }
62}
Note: See TracBrowser for help on using the repository browser.