Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionaryTracking/3.4/SymbolicExpressionTreeGenealogyGraph/SymbolicExpressionTreeGenealogyGraph.cs @ 10264

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

#1772: Created new branch for the redesigned version of the tracking plugin

File size: 4.5 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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.EvolutionaryTracking {
31  [Item("SymbolicExpressionTreeGenealogyGraph", "A genealogy graph for symbolic expression tree individuals")]
32  [StorableClass]
33  public sealed class SymbolicExpressionTreeGenealogyGraph : GenericGraph<SymbolicExpressionTreeGenealogyGraphNode>,
34                                                             ISymbolicExpressionTreeGenealogyGraph, IDisposable {
35    [Storable]
36    private readonly Dictionary<ISymbolicExpressionTree, List<SymbolicExpressionTreeGenealogyGraphNode>> nodeMap
37      = new Dictionary<ISymbolicExpressionTree, List<SymbolicExpressionTreeGenealogyGraphNode>>();
38
39    public SymbolicExpressionTreeGenealogyGraph() {
40    }
41
42    public void Updated() {
43      var updated = GraphUpdated;
44      if (updated != null) updated(this, null);
45    }
46
47    [StorableConstructor]
48    public SymbolicExpressionTreeGenealogyGraph(bool serializing)
49      : base(serializing) {
50    }
51
52    public override IDeepCloneable Clone(Cloner cloner) {
53      return new SymbolicExpressionTreeGenealogyGraph(this, cloner);
54    }
55
56    private SymbolicExpressionTreeGenealogyGraph(SymbolicExpressionTreeGenealogyGraph original, Cloner cloner)
57      : base(original, cloner) {
58      nodeMap = new Dictionary<ISymbolicExpressionTree, List<SymbolicExpressionTreeGenealogyGraphNode>>(original.nodeMap);
59    }
60
61    public override void AddNode(SymbolicExpressionTreeGenealogyGraphNode node) {
62      var tree = node.SymbolicExpressionTree;
63      if (nodeMap.ContainsKey(tree))
64        nodeMap[tree].Add(node);
65      else
66        nodeMap[tree] = new List<SymbolicExpressionTreeGenealogyGraphNode> { node };
67      base.AddNode(node);
68      // the genealogy graph should have as many nodes as individuals in the population multiplied by the number of generations
69      // plus a number of intermediate individuals ~= mutation probability * population size
70    }
71
72    public bool ContainsIndividual(ISymbolicExpressionTree tree) {
73      return nodeMap.ContainsKey(tree);
74    }
75
76    public List<SymbolicExpressionTreeGenealogyGraphNode> GetGraphNodes(ISymbolicExpressionTree tree) {
77      return nodeMap.ContainsKey(tree) ? nodeMap[tree] : null;
78    }
79    // TODO: make sure a and b are valid and the info exists for both
80    public int Compare(SymbolicExpressionTreeGenealogyGraphNode a, SymbolicExpressionTreeGenealogyGraphNode b) {
81      if (a.Quality.Equals(b.Quality)) {
82        if (a.IsElite) return +1;
83        if (b.IsElite) return -1;
84      }
85      return a.Quality.CompareTo(b.Quality);
86    }
87
88    public SymbolicExpressionTreeGenealogyGraphNode MostRecentCommonAncestor() {
89      double currentRank = Nodes.Last().Rank;
90      var currentGen = Nodes.Where(n => n.Rank.IsAlmost(currentRank)).ToList();
91      var lineages = currentGen.Select(n => n.RootLineage().ToArray()).ToArray();
92      for (int i = 0; i < (int)currentRank; ++i) {
93        if (lineages.All(lineage => lineages[0][i].SymbolicExpressionTree == lineage[i].SymbolicExpressionTree))
94          return lineages[0][i];
95      }
96      return null;
97    }
98
99    public void Dispose() {
100      Nodes.Clear();
101      nodeMap.Clear();
102      // call GC.SupressFinalize?
103    }
104
105    public event EventHandler GraphUpdated;
106    private void OnGraphUpdated(object sender, EventArgs args) {
107      var updated = GraphUpdated;
108      if (updated != null) updated(sender, args);
109    }
110  }
111}
Note: See TracBrowser for help on using the repository browser.