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