[8555] | 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 |
|
---|
[9082] | 22 | using System;
|
---|
[8555] | 23 | using System.Collections.Generic;
|
---|
[7779] | 24 | using System.Linq;
|
---|
[8213] | 25 | using HeuristicLab.Common;
|
---|
[8236] | 26 | using HeuristicLab.Core;
|
---|
[7779] | 27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[7785] | 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[7779] | 29 |
|
---|
| 30 | namespace HeuristicLab.EvolutionaryTracking {
|
---|
[9082] | 31 | [Item("SymbolicExpressionTreeGenealogyGraph", "A genealogy graph for symbolic expression tree individuals")]
|
---|
[8236] | 32 | [StorableClass]
|
---|
[9419] | 33 | public sealed class SymbolicExpressionTreeGenealogyGraph : GenericGraph<SymbolicExpressionGenealogyGraphNode>,
|
---|
| 34 | ISymbolicExpressionTreeGenealogyGraph, IDisposable {
|
---|
[8248] | 35 | [Storable]
|
---|
[9419] | 36 | private readonly Dictionary<ISymbolicExpressionTree, List<SymbolicExpressionGenealogyGraphNode>> nodeMap
|
---|
| 37 | = new Dictionary<ISymbolicExpressionTree, List<SymbolicExpressionGenealogyGraphNode>>();
|
---|
[8248] | 38 |
|
---|
[9419] | 39 | public SymbolicExpressionTreeGenealogyGraph() {
|
---|
| 40 | }
|
---|
[7779] | 41 |
|
---|
[9419] | 42 | public void Updated() {
|
---|
| 43 | var updated = GraphUpdated;
|
---|
| 44 | if (updated != null) updated(this, null);
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[7785] | 47 | [StorableConstructor]
|
---|
[9419] | 48 | public SymbolicExpressionTreeGenealogyGraph(bool serializing)
|
---|
| 49 | : base(serializing) {
|
---|
| 50 | }
|
---|
[7785] | 51 |
|
---|
[9419] | 52 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 53 | return new SymbolicExpressionTreeGenealogyGraph(this, cloner);
|
---|
| 54 | }
|
---|
[8555] | 55 |
|
---|
[9419] | 56 | private SymbolicExpressionTreeGenealogyGraph(SymbolicExpressionTreeGenealogyGraph original, Cloner cloner)
|
---|
| 57 | : base(original, cloner) {
|
---|
| 58 | nodeMap = new Dictionary<ISymbolicExpressionTree, List<SymbolicExpressionGenealogyGraphNode>>(original.nodeMap);
|
---|
| 59 | }
|
---|
[8555] | 60 |
|
---|
| 61 | public override void AddNode(SymbolicExpressionGenealogyGraphNode node) {
|
---|
[9082] | 62 | var tree = node.SymbolicExpressionTree;
|
---|
| 63 | if (nodeMap.ContainsKey(tree))
|
---|
| 64 | nodeMap[tree].Add(node);
|
---|
[8555] | 65 | else
|
---|
[9082] | 66 | nodeMap[tree] = new List<SymbolicExpressionGenealogyGraphNode> { node };
|
---|
[9419] | 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
|
---|
[8213] | 70 | }
|
---|
| 71 |
|
---|
[9082] | 72 | public bool ContainsIndividual(ISymbolicExpressionTree tree) {
|
---|
| 73 | return nodeMap.ContainsKey(tree);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[8555] | 76 | public List<SymbolicExpressionGenealogyGraphNode> GetGraphNodes(ISymbolicExpressionTree tree) {
|
---|
| 77 | return nodeMap.ContainsKey(tree) ? nodeMap[tree] : null;
|
---|
[8213] | 78 | }
|
---|
| 79 |
|
---|
[8555] | 80 | // TODO: make sure a and b are valid and the info exists for both
|
---|
| 81 | public int Compare(SymbolicExpressionGenealogyGraphNode a, SymbolicExpressionGenealogyGraphNode b) {
|
---|
| 82 | if (a.Quality.Equals(b.Quality)) {
|
---|
| 83 | if (a.IsElite) return +1;
|
---|
| 84 | if (b.IsElite) return -1;
|
---|
[8248] | 85 | }
|
---|
[8555] | 86 | return a.Quality.CompareTo(b.Quality);
|
---|
[8248] | 87 | }
|
---|
| 88 |
|
---|
[9419] | 89 | public SymbolicExpressionGenealogyGraphNode MostRecentCommonAncestor() {
|
---|
| 90 | double currentRank = Nodes.Last().Rank;
|
---|
| 91 | var currentGen = Nodes.Where(n => n.Rank.IsAlmost(currentRank)).ToList();
|
---|
| 92 | var lineages = currentGen.Select(n => n.RootLineage().ToArray()).ToArray();
|
---|
| 93 | for (int i = 0; i < (int)currentRank; ++i) {
|
---|
| 94 | if (lineages.All(lineage => lineages[0][i].SymbolicExpressionTree == lineage[i].SymbolicExpressionTree))
|
---|
| 95 | return lineages[0][i];
|
---|
| 96 | }
|
---|
| 97 | return null;
|
---|
| 98 | }
|
---|
[9082] | 99 |
|
---|
| 100 | public void Dispose() {
|
---|
| 101 | Nodes.Clear();
|
---|
| 102 | nodeMap.Clear();
|
---|
| 103 | // call GC.SupressFinalize?
|
---|
[7779] | 104 | }
|
---|
[9419] | 105 |
|
---|
| 106 | public event EventHandler GraphUpdated;
|
---|
| 107 | private void OnGraphUpdated(object sender, EventArgs args) {
|
---|
| 108 | var updated = GraphUpdated;
|
---|
| 109 | if (updated != null) updated(sender, args);
|
---|
| 110 | }
|
---|
[7779] | 111 | }
|
---|
[8248] | 112 |
|
---|
[9247] | 113 | [StorableClass]
|
---|
[9082] | 114 | public class SymbolicExpressionGenealogyGraphNode : Vertex {
|
---|
| 115 | public ISymbolicExpressionTree SymbolicExpressionTree {
|
---|
| 116 | get { return (ISymbolicExpressionTree)Content; }
|
---|
| 117 | set { Content = value; }
|
---|
| 118 | }
|
---|
[8248] | 119 |
|
---|
[9419] | 120 | [Storable]
|
---|
| 121 | private double quality;
|
---|
| 122 | public double Quality { get { return quality; } set { quality = value; } }
|
---|
| 123 | [Storable]
|
---|
| 124 | private bool isElite;
|
---|
| 125 | public bool IsElite { get { return isElite; } set { isElite = value; } }
|
---|
| 126 | [Storable]
|
---|
| 127 | private double rank;
|
---|
| 128 | public double Rank { get { return rank; } set { rank = value; } }
|
---|
| 129 |
|
---|
| 130 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 131 | private void AfterDeserialization() {
|
---|
| 132 | if (Id == null) throw new Exception();
|
---|
| 133 | }
|
---|
| 134 |
|
---|
[9247] | 135 | [StorableConstructor]
|
---|
[9419] | 136 | public SymbolicExpressionGenealogyGraphNode(bool deserializing)
|
---|
| 137 | : base(deserializing) {
|
---|
| 138 | }
|
---|
[9247] | 139 |
|
---|
[9419] | 140 | public SymbolicExpressionGenealogyGraphNode() {
|
---|
| 141 | }
|
---|
[8555] | 142 |
|
---|
[9419] | 143 | private SymbolicExpressionGenealogyGraphNode(SymbolicExpressionGenealogyGraphNode original, Cloner cloner)
|
---|
| 144 | : base(original, cloner) {
|
---|
| 145 | Quality = original.Quality;
|
---|
| 146 | IsElite = original.IsElite;
|
---|
| 147 | Rank = original.Rank;
|
---|
[8555] | 148 | }
|
---|
[9082] | 149 |
|
---|
[9419] | 150 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 151 | return new SymbolicExpressionGenealogyGraphNode(this, cloner);
|
---|
| 152 | }
|
---|
| 153 |
|
---|
[9082] | 154 | public new IEnumerable<SymbolicExpressionGenealogyGraphNode> Ancestors() {
|
---|
| 155 | return base.Ancestors().Cast<SymbolicExpressionGenealogyGraphNode>();
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | public new IEnumerable<SymbolicExpressionGenealogyGraphNode> Descendants() {
|
---|
| 159 | return base.Descendants().Cast<SymbolicExpressionGenealogyGraphNode>();
|
---|
| 160 | }
|
---|
[9419] | 161 |
|
---|
| 162 | public IEnumerable<SymbolicExpressionGenealogyGraphNode> RootLineage() {
|
---|
| 163 | var lineage = new List<SymbolicExpressionGenealogyGraphNode> { this };
|
---|
| 164 | int i = 0;
|
---|
| 165 | while (i != lineage.Count) {
|
---|
| 166 | if (lineage[i].InEdges != null) {
|
---|
| 167 | lineage.Add((SymbolicExpressionGenealogyGraphNode)lineage[i].InEdges[0].Source);
|
---|
| 168 | }
|
---|
| 169 | ++i;
|
---|
| 170 | }
|
---|
| 171 | return lineage;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | public new List<Arc> InEdges {
|
---|
| 175 | get {
|
---|
| 176 | return base.InEdges == null ? null : base.InEdges.Cast<Arc>().ToList();
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | public new List<Arc> OutEdges {
|
---|
| 181 | get {
|
---|
| 182 | return base.OutEdges == null ? null : base.InEdges.Cast<Arc>().ToList();
|
---|
| 183 | }
|
---|
| 184 | }
|
---|
[8248] | 185 | }
|
---|
[7779] | 186 | }
|
---|