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 HEAL.Attic;
|
---|
29 | using HeuristicLab.Common;
|
---|
30 | using HeuristicLab.Core;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.EvolutionTracking
|
---|
34 | {
|
---|
35 | [Item("GenealogyGraph", "A class representing a genealogy graph")]
|
---|
36 | [StorableType("66DCA305-87F7-41D8-91CF-68415B1809A8")]
|
---|
37 | public class GenealogyGraph : DirectedGraph, IGenealogyGraph
|
---|
38 | {
|
---|
39 | private Dictionary<object, IGenealogyGraphNode> contentMap;
|
---|
40 | private Dictionary<string, IGenealogyGraphNode> idMap;
|
---|
41 |
|
---|
42 | private readonly Comparison<IArc<IDeepCloneable>> compareArcs = (a, b) => {
|
---|
43 | var da = a.Data;
|
---|
44 | var db = b.Data;
|
---|
45 |
|
---|
46 | if ((da == null && db == null) || (da != null && db != null))
|
---|
47 | return 0;
|
---|
48 |
|
---|
49 | if (da == null)
|
---|
50 | return -1;
|
---|
51 |
|
---|
52 | return 1;
|
---|
53 | };
|
---|
54 |
|
---|
55 | protected Dictionary<double, List<IGenealogyGraphNode>> ranks;
|
---|
56 | public IEnumerable<KeyValuePair<double, IEnumerable<IGenealogyGraphNode>>> Ranks {
|
---|
57 | get { return ranks.Select(x => new KeyValuePair<double, IEnumerable<IGenealogyGraphNode>>(x.Key, x.Value)); }
|
---|
58 | }
|
---|
59 | public IEnumerable<IGenealogyGraphNode> GetByRank(double rank) {
|
---|
60 | return ranks.ContainsKey(rank) ? ranks[rank] : Enumerable.Empty<IGenealogyGraphNode>();
|
---|
61 | }
|
---|
62 | protected GenealogyGraph(GenealogyGraph original, Cloner cloner)
|
---|
63 | : base(original, cloner) {
|
---|
64 | RebuildDictionaries();
|
---|
65 | // sort arcs so that in the case of crossover (child vertex with two parents)
|
---|
66 | // the arc which holds the fragment information is always last
|
---|
67 | foreach (var arcList in base.Vertices.Select(v => (List<IArc>)v.InArcs)) {
|
---|
68 | arcList.Sort((a, b) => compareArcs((IGenealogyGraphArc)a, (IGenealogyGraphArc)b));
|
---|
69 | }
|
---|
70 | }
|
---|
71 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
72 | return new GenealogyGraph(this, cloner);
|
---|
73 | }
|
---|
74 |
|
---|
75 | [StorableConstructor]
|
---|
76 | protected GenealogyGraph(StorableConstructorFlag _) : base(_) { }
|
---|
77 | [StorableHook(HookType.AfterDeserialization)]
|
---|
78 | private void AfterDeserialization() {
|
---|
79 | RebuildDictionaries();
|
---|
80 | foreach (var arcList in base.Vertices.Select(v => (List<IArc>)v.InArcs)) {
|
---|
81 | arcList.Sort((a, b) => compareArcs((IGenealogyGraphArc)a, (IGenealogyGraphArc)b));
|
---|
82 | }
|
---|
83 | }
|
---|
84 | public GenealogyGraph() {
|
---|
85 | ranks = new Dictionary<double, List<IGenealogyGraphNode>>();
|
---|
86 | contentMap = new Dictionary<object, IGenealogyGraphNode>();
|
---|
87 | idMap = new Dictionary<string, IGenealogyGraphNode>();
|
---|
88 | }
|
---|
89 | new public IEnumerable<IGenealogyGraphNode> Vertices {
|
---|
90 | get { return base.Vertices.Select(x => (IGenealogyGraphNode)x); }
|
---|
91 | }
|
---|
92 | new public IEnumerable<IGenealogyGraphArc> Arcs {
|
---|
93 | get { return base.Arcs.Select(x => (IGenealogyGraphArc)x); }
|
---|
94 | }
|
---|
95 | public override IArc AddArc(IVertex source, IVertex target) {
|
---|
96 | var arc = new GenealogyGraphArc((IGenealogyGraphNode)source, (IGenealogyGraphNode)target);
|
---|
97 | base.AddArc(arc);
|
---|
98 | return arc;
|
---|
99 | }
|
---|
100 | public override void AddVertex(IVertex vertex) {
|
---|
101 | base.AddVertex(vertex);
|
---|
102 | var genealogyGraphNode = (IGenealogyGraphNode)vertex;
|
---|
103 |
|
---|
104 | if (contentMap.ContainsKey(genealogyGraphNode.Data))
|
---|
105 | throw new InvalidOperationException("Duplicate content is not allowed in the genealogy graph.");
|
---|
106 | contentMap[genealogyGraphNode.Data] = genealogyGraphNode;
|
---|
107 |
|
---|
108 | if (idMap.ContainsKey(genealogyGraphNode.Id))
|
---|
109 | throw new InvalidOperationException("Duplicate ids are not allowed in the genealogy graph.");
|
---|
110 | idMap[genealogyGraphNode.Id] = genealogyGraphNode;
|
---|
111 |
|
---|
112 | if (!ranks.ContainsKey(genealogyGraphNode.Rank))
|
---|
113 | ranks[genealogyGraphNode.Rank] = new List<IGenealogyGraphNode>();
|
---|
114 | ranks[genealogyGraphNode.Rank].Add(genealogyGraphNode);
|
---|
115 | }
|
---|
116 | public override void RemoveVertex(IVertex vertex) {
|
---|
117 | var node = (IGenealogyGraphNode)vertex;
|
---|
118 | contentMap.Remove(node.Data);
|
---|
119 | idMap.Remove(node.Id);
|
---|
120 | if (ranks.ContainsKey(node.Rank)) {
|
---|
121 | ranks[node.Rank].Remove(node); // this call will be slow, use with caution
|
---|
122 | if (!ranks[node.Rank].Any())
|
---|
123 | ranks.Remove(node.Rank);
|
---|
124 | }
|
---|
125 | base.RemoveVertex(vertex);
|
---|
126 | }
|
---|
127 |
|
---|
128 | public override void RemoveVertices(IEnumerable<IVertex> vertices) {
|
---|
129 | foreach (var v in vertices)
|
---|
130 | this.RemoveVertex(v);
|
---|
131 | }
|
---|
132 |
|
---|
133 | public IGenealogyGraphNode GetByContent(object content) {
|
---|
134 | IGenealogyGraphNode result;
|
---|
135 | contentMap.TryGetValue(content, out result);
|
---|
136 | return result;
|
---|
137 | }
|
---|
138 | public IGenealogyGraphNode GetById(string id) {
|
---|
139 | IGenealogyGraphNode result;
|
---|
140 | idMap.TryGetValue(id, out result);
|
---|
141 | return result;
|
---|
142 | }
|
---|
143 | public bool Contains(object content) {
|
---|
144 | return contentMap.ContainsKey(content);
|
---|
145 | }
|
---|
146 | public event EventHandler GraphUpdated;
|
---|
147 | private void OnGraphUpdated(object sender, EventArgs args) {
|
---|
148 | var updated = GraphUpdated;
|
---|
149 | if (updated != null) updated(sender, args);
|
---|
150 | }
|
---|
151 | public override void Clear() {
|
---|
152 | base.Clear();
|
---|
153 | ranks.Clear();
|
---|
154 | contentMap.Clear();
|
---|
155 | idMap.Clear();
|
---|
156 | }
|
---|
157 | private void RebuildDictionaries() {
|
---|
158 | contentMap = new Dictionary<object, IGenealogyGraphNode>();
|
---|
159 | idMap = new Dictionary<string, IGenealogyGraphNode>();
|
---|
160 | ranks = new Dictionary<double, List<IGenealogyGraphNode>>();
|
---|
161 |
|
---|
162 | foreach (var v in Vertices) {
|
---|
163 | contentMap[v.Data] = v;
|
---|
164 | idMap[v.Id] = v;
|
---|
165 | if (ranks.ContainsKey(v.Rank)) {
|
---|
166 | ranks[v.Rank].Add(v);
|
---|
167 | } else {
|
---|
168 | ranks[v.Rank] = new List<IGenealogyGraphNode> { v };
|
---|
169 | }
|
---|
170 | }
|
---|
171 | }
|
---|
172 | public void ExportDot(string path) {
|
---|
173 | var sb = new StringBuilder();
|
---|
174 | sb.AppendLine("graph fragmentgraph {");
|
---|
175 | foreach (var v in Vertices) {
|
---|
176 | double width = Math.Max(0.5, v.Degree);
|
---|
177 | sb.AppendLine("\"" + v.Id + "\"[shape=circle, style = filled, width = " + width + ", label = " + v.Rank + "\n" + String.Format("{0:00}", v.Quality) + ", fillcolor = \"" + ColorTranslator.ToHtml(GetColor(v)) + "\"]");
|
---|
178 | }
|
---|
179 | foreach (var a in Arcs) {
|
---|
180 | sb.AppendLine("\"" + a.Source.Id + "\" -- \"" + a.Target.Id + "\"");
|
---|
181 | }
|
---|
182 |
|
---|
183 | sb.AppendLine("}");
|
---|
184 | File.WriteAllText(path, sb.ToString());
|
---|
185 | }
|
---|
186 | private Color GetColor(IGenealogyGraphNode node) {
|
---|
187 | var colorIndex = (int)Math.Round(node.Quality * ColorGradient.Colors.Count);
|
---|
188 | if (colorIndex >= ColorGradient.Colors.Count) return ColorGradient.Colors.Last();
|
---|
189 | return ColorGradient.Colors[colorIndex];
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | [Item("GenealogyGraph", "A genealogy graph in which the vertex data is of type T")]
|
---|
194 | [StorableType("3F5043F7-2538-4B76-BA12-062A4CEF5A1A")]
|
---|
195 | public class GenealogyGraph<T> : GenealogyGraph, IGenealogyGraph<T> where T : class, IItem
|
---|
196 | {
|
---|
197 | public GenealogyGraph() { }
|
---|
198 | private GenealogyGraph(GenealogyGraph original, Cloner cloner)
|
---|
199 | : base(original, cloner) {
|
---|
200 | }
|
---|
201 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
202 | return new GenealogyGraph<T>(this, cloner);
|
---|
203 | }
|
---|
204 | new public IEnumerable<IGenealogyGraphNode<T>> Vertices {
|
---|
205 | get { return base.Vertices.Select(x => (IGenealogyGraphNode<T>)x); }
|
---|
206 | }
|
---|
207 | new public IGenealogyGraphNode<T> GetByContent(object content) {
|
---|
208 | return (IGenealogyGraphNode<T>)base.GetByContent(content);
|
---|
209 | }
|
---|
210 | new public IGenealogyGraphNode<T> GetById(string id) {
|
---|
211 | return (IGenealogyGraphNode<T>)base.GetById(id);
|
---|
212 | }
|
---|
213 | new public IEnumerable<IGenealogyGraphNode<T>> GetByRank(double rank) {
|
---|
214 | return base.GetByRank(rank).Select(x => (IGenealogyGraphNode<T>)x);
|
---|
215 | }
|
---|
216 | public new IEnumerable<KeyValuePair<double, IEnumerable<IGenealogyGraphNode<T>>>> Ranks {
|
---|
217 | get { return base.Ranks.Select(x => new KeyValuePair<double, IEnumerable<IGenealogyGraphNode<T>>>(x.Key, x.Value.Select(n => (IGenealogyGraphNode<T>)n))); }
|
---|
218 | }
|
---|
219 | }
|
---|
220 | }
|
---|