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