Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/SymbolicExpressionTreeGenealogyGraph.cs @ 8555

Last change on this file since 8555 was 8555, checked in by bburlacu, 12 years ago

#1772: Introduced more specific graph class for symbolic expression problems.

File size: 4.1 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.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Problems.DataAnalysis.Symbolic;
29
30namespace HeuristicLab.EvolutionaryTracking {
31  [Item("SymbolicExpressionTreeGenealogyGraph", "A genealogy graph for symbolic expression trees")]
32  [StorableClass]
33  public class SymbolicExpressionTreeGenealogyGraph : GenealogyGraph<SymbolicExpressionGenealogyGraphNode>, ISymbolicExpressionTreeGenealogyGraph {
34    [Storable]
35    private readonly Dictionary<ISymbolicExpressionTree, List<SymbolicExpressionGenealogyGraphNode>> nodeMap =
36      new Dictionary<ISymbolicExpressionTree, List<SymbolicExpressionGenealogyGraphNode>>();
37
38    public SymbolicExpressionTreeGenealogyGraph() { }
39
40    [StorableConstructor]
41    public SymbolicExpressionTreeGenealogyGraph(bool serializing) : base(serializing) { }
42
43    public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicExpressionTreeGenealogyGraph(this, cloner); }
44
45    protected SymbolicExpressionTreeGenealogyGraph(SymbolicExpressionTreeGenealogyGraph original, Cloner cloner) : base(original, cloner) { }
46
47    public override void AddNode(SymbolicExpressionGenealogyGraphNode node) {
48      if (!nodeMap.ContainsKey(node.SymbolicExpressionTree))
49        nodeMap[node.SymbolicExpressionTree] = new List<SymbolicExpressionGenealogyGraphNode> { node };
50      else
51        nodeMap[node.SymbolicExpressionTree].Add(node);
52      base.AddNode(node);
53    }
54
55    public List<SymbolicExpressionGenealogyGraphNode> GetGraphNodes(ISymbolicExpressionTree tree) {
56      return nodeMap.ContainsKey(tree) ? nodeMap[tree] : null;
57    }
58
59    public bool HasNode(SymbolicExpressionTree tree) {
60      return Nodes.Any(x => x.SymbolicExpressionTree == tree);
61    }
62
63    public int Compare(GenealogyGraphNode a, GenealogyGraphNode b) {
64      return Compare(a as SymbolicExpressionGenealogyGraphNode, b as SymbolicExpressionGenealogyGraphNode);
65    }
66
67    // TODO: make sure a and b are valid and the info exists for both
68    public int Compare(SymbolicExpressionGenealogyGraphNode a, SymbolicExpressionGenealogyGraphNode b) {
69      if (a.Quality.Equals(b.Quality)) {
70        if (a.IsElite) return +1;
71        if (b.IsElite) return -1;
72      }
73      return a.Quality.CompareTo(b.Quality);
74    }
75
76    #region Fragment tracing
77    public IEnumerable<ISymbolicExpressionTree> TraceFragment(ISymbolicExpressionTreeNode fragment, SimilarityLevel similarity = SimilarityLevel.Exact) {
78      return Nodes.Where(x => x.SymbolicExpressionTree.ContainsFragment(fragment, similarity)).Select(x => x.SymbolicExpressionTree).ToList();
79    }
80    #endregion
81  }
82
83  public class SymbolicExpressionGenealogyGraphNode : GenealogyGraphNode {
84    public ISymbolicExpressionTree SymbolicExpressionTree { get; set; }
85    public double Quality { get; set; }
86    public bool IsElite { get; set; }
87    public List<double> Ranks { get; set; }
88
89    public SymbolicExpressionGenealogyGraphNode() {
90      Ranks = new List<double>();
91    }
92
93    public SymbolicExpressionGenealogyGraphNode(object content) {
94      Content = content;
95      Ranks = new List<double>();
96    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.