[10278] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2014 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 |
|
---|
[10267] | 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[11459] | 24 | using System.Drawing;
|
---|
| 25 | using System.IO;
|
---|
[10300] | 26 | using System.Linq;
|
---|
[11459] | 27 | using System.Text;
|
---|
[10267] | 28 | using HeuristicLab.Common;
|
---|
| 29 | using HeuristicLab.Core;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 |
|
---|
[10271] | 32 | namespace HeuristicLab.EvolutionTracking {
|
---|
[10267] | 33 | [StorableClass]
|
---|
[10888] | 34 | [Item("GenealogyGraph", "A class representing a genealogy graph")]
|
---|
[10300] | 35 | public class GenealogyGraph : DirectedGraph, IGenealogyGraph {
|
---|
[11459] | 36 | private Dictionary<object, IGenealogyGraphNode> contentMap;
|
---|
| 37 | private Dictionary<string, IGenealogyGraphNode> idMap;
|
---|
[11318] | 38 |
|
---|
[11475] | 39 | private Dictionary<double, List<IGenealogyGraphNode>> ranks;
|
---|
[10830] | 40 | public Dictionary<double, List<IGenealogyGraphNode>> Ranks {
|
---|
[10267] | 41 | get { return ranks; }
|
---|
| 42 | set { ranks = value; }
|
---|
| 43 | }
|
---|
[10886] | 44 |
|
---|
[10300] | 45 | protected GenealogyGraph(GenealogyGraph original, Cloner cloner)
|
---|
| 46 | : base(original, cloner) {
|
---|
[11502] | 47 | RebuildDictionaries();
|
---|
[10300] | 48 | }
|
---|
[11233] | 49 |
|
---|
[10267] | 50 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[10300] | 51 | return new GenealogyGraph(this, cloner);
|
---|
[10267] | 52 | }
|
---|
[11233] | 53 |
|
---|
[10267] | 54 | [StorableConstructor]
|
---|
[11318] | 55 | protected GenealogyGraph(bool deserializing)
|
---|
| 56 | : base(deserializing) {
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[11459] | 59 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 60 | private void AfterDeserialization() {
|
---|
[11502] | 61 | RebuildDictionaries();
|
---|
[11459] | 62 | }
|
---|
| 63 |
|
---|
[10267] | 64 | public GenealogyGraph() {
|
---|
[10830] | 65 | Ranks = new Dictionary<double, List<IGenealogyGraphNode>>();
|
---|
[11233] | 66 | contentMap = new Dictionary<object, IGenealogyGraphNode>();
|
---|
| 67 | idMap = new Dictionary<string, IGenealogyGraphNode>();
|
---|
[10267] | 68 | }
|
---|
[10888] | 69 |
|
---|
[11390] | 70 | new public IEnumerable<IGenealogyGraphNode> Vertices {
|
---|
| 71 | get { return base.Vertices.Select(x => (IGenealogyGraphNode)x); }
|
---|
[11233] | 72 | }
|
---|
| 73 |
|
---|
[11475] | 74 | new public IEnumerable<IGenealogyGraphArc> Arcs {
|
---|
| 75 | get { return base.Arcs.Select(x => (IGenealogyGraphArc)x); }
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[10888] | 78 | public override IArc AddArc(IVertex source, IVertex target) {
|
---|
| 79 | var arc = new GenealogyGraphArc((IGenealogyGraphNode)source, (IGenealogyGraphNode)target);
|
---|
[11253] | 80 | base.AddArc(arc);
|
---|
[10888] | 81 | return arc;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[10300] | 84 | public override void AddVertex(IVertex vertex) {
|
---|
[10888] | 85 | base.AddVertex(vertex);
|
---|
[11390] | 86 | var genealogyGraphNode = (IGenealogyGraphNode)vertex;
|
---|
| 87 | if (contentMap.ContainsKey(genealogyGraphNode.Data))
|
---|
[11233] | 88 | throw new InvalidOperationException("Duplicate content is not allowed in the genealogy graph.");
|
---|
[11390] | 89 | contentMap[genealogyGraphNode.Data] = genealogyGraphNode;
|
---|
| 90 | if (idMap.ContainsKey(genealogyGraphNode.Id))
|
---|
[11318] | 91 | throw new InvalidOperationException("Duplicate ids are not allowed in the genealogy graph.");
|
---|
[11390] | 92 | idMap[genealogyGraphNode.Id] = genealogyGraphNode;
|
---|
| 93 | if (!Ranks.ContainsKey(genealogyGraphNode.Rank))
|
---|
| 94 | Ranks[genealogyGraphNode.Rank] = new List<IGenealogyGraphNode>();
|
---|
| 95 | Ranks[genealogyGraphNode.Rank].Add(genealogyGraphNode);
|
---|
[10278] | 96 | }
|
---|
[11233] | 97 |
|
---|
[10300] | 98 | public override void RemoveVertex(IVertex vertex) {
|
---|
[10302] | 99 | var node = (IGenealogyGraphNode)vertex;
|
---|
[11253] | 100 | contentMap.Remove(node.Data);
|
---|
[11233] | 101 | idMap.Remove(node.Id);
|
---|
[10302] | 102 | if (Ranks.ContainsKey(node.Rank)) {
|
---|
| 103 | Ranks[node.Rank].Remove(node);
|
---|
[11257] | 104 | if (!Ranks[node.Rank].Any())
|
---|
| 105 | Ranks.Remove(node.Rank);
|
---|
[10278] | 106 | }
|
---|
| 107 | base.RemoveVertex(vertex);
|
---|
| 108 | }
|
---|
[11233] | 109 |
|
---|
| 110 | public IGenealogyGraphNode GetByContent(object content) {
|
---|
| 111 | IGenealogyGraphNode result;
|
---|
| 112 | contentMap.TryGetValue(content, out result);
|
---|
| 113 | return result;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | public IGenealogyGraphNode GetById(string id) {
|
---|
| 117 | IGenealogyGraphNode result;
|
---|
| 118 | idMap.TryGetValue(id, out result);
|
---|
| 119 | return result;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | public bool Contains(object content) {
|
---|
| 123 | return contentMap.ContainsKey(content);
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[10278] | 126 | public event EventHandler GraphUpdated;
|
---|
| 127 | private void OnGraphUpdated(object sender, EventArgs args) {
|
---|
| 128 | var updated = GraphUpdated;
|
---|
| 129 | if (updated != null) updated(sender, args);
|
---|
| 130 | }
|
---|
[10267] | 131 |
|
---|
[10677] | 132 | public override void Clear() {
|
---|
| 133 | base.Clear();
|
---|
| 134 | ranks.Clear();
|
---|
[11475] | 135 | contentMap.Clear();
|
---|
| 136 | idMap.Clear();
|
---|
[10677] | 137 | }
|
---|
[11459] | 138 |
|
---|
[11502] | 139 | private void RebuildDictionaries() {
|
---|
| 140 | contentMap = new Dictionary<object, IGenealogyGraphNode>();
|
---|
| 141 | idMap = new Dictionary<string, IGenealogyGraphNode>();
|
---|
| 142 | ranks = new Dictionary<double, List<IGenealogyGraphNode>>();
|
---|
| 143 |
|
---|
| 144 | foreach (var v in Vertices) {
|
---|
| 145 | contentMap[v.Data] = v;
|
---|
| 146 | idMap[v.Id] = v;
|
---|
| 147 | if (ranks.ContainsKey(v.Rank)) {
|
---|
| 148 | ranks[v.Rank].Add(v);
|
---|
| 149 | } else {
|
---|
| 150 | ranks[v.Rank] = new List<IGenealogyGraphNode> { v };
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 |
|
---|
[11459] | 155 | public void ExportDot(string path) {
|
---|
| 156 | var sb = new StringBuilder();
|
---|
| 157 | sb.AppendLine("graph fragmentgraph {");
|
---|
| 158 | foreach (var v in Vertices) {
|
---|
| 159 | sb.AppendLine("\"" + v.Id + "\"[shape=circle, style = filled, width = " + v.Degree / 2.0 + ", label = " + v.Rank + ", fillcolor = \"" + ColorTranslator.ToHtml(GetColor(v)) + "\"]");
|
---|
| 160 | }
|
---|
[11502] | 161 | foreach (var a in Arcs) {
|
---|
[11459] | 162 | sb.AppendLine("\"" + a.Source.Id + "\" -- \"" + a.Target.Id + "\"");
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | sb.AppendLine("}");
|
---|
| 166 | File.WriteAllText(path, sb.ToString());
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | private Color GetColor(IGenealogyGraphNode node) {
|
---|
| 170 | var colorIndex = (int)Math.Round(node.Quality * ColorGradient.Colors.Count);
|
---|
| 171 | if (colorIndex >= ColorGradient.Colors.Count) return ColorGradient.Colors.Last();
|
---|
| 172 | return ColorGradient.Colors[colorIndex];
|
---|
| 173 | }
|
---|
[10267] | 174 | }
|
---|
[10300] | 175 |
|
---|
[11459] | 176 | [Item("GenealogyGraph", "A genealogy graph in which the vertex data is of type T")]
|
---|
[10347] | 177 | [StorableClass]
|
---|
[11233] | 178 | public class GenealogyGraph<T> : GenealogyGraph, IGenealogyGraph<T> where T : class, IItem {
|
---|
[11390] | 179 | new public IEnumerable<IGenealogyGraphNode<T>> Vertices {
|
---|
| 180 | get { return base.Vertices.Select(x => (IGenealogyGraphNode<T>)x); }
|
---|
[10677] | 181 | }
|
---|
[11390] | 182 | new public IGenealogyGraphNode<T> GetByContent(object content) {
|
---|
[11386] | 183 | return (IGenealogyGraphNode<T>)base.GetByContent(content);
|
---|
| 184 | }
|
---|
[11390] | 185 | new public IGenealogyGraphNode<T> GetById(string id) {
|
---|
[11386] | 186 | return (IGenealogyGraphNode<T>)base.GetById(id);
|
---|
| 187 | }
|
---|
[10347] | 188 | }
|
---|
[10267] | 189 | }
|
---|