[16053] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2018 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;
|
---|
[15821] | 23 | using System.Diagnostics;
|
---|
| 24 | using System.IO;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
[15949] | 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[15821] | 28 |
|
---|
| 29 | namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration {
|
---|
[15949] | 30 | [Item("SearchGraphVisualizer", "")]
|
---|
| 31 | [StorableClass]
|
---|
| 32 | internal class SearchGraphVisualizer : Item, IGrammarEnumerationAnalyzer {
|
---|
[15821] | 33 | private readonly string dotFileName = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\searchgraph.dot";
|
---|
| 34 |
|
---|
| 35 | private TextWriterTraceListener dotFileTrace;
|
---|
| 36 |
|
---|
| 37 | public SearchGraphVisualizer() { }
|
---|
| 38 |
|
---|
[15910] | 39 | protected SearchGraphVisualizer(SearchGraphVisualizer original, Cloner cloner) : base(original, cloner) { }
|
---|
[15821] | 40 |
|
---|
[15949] | 41 | [StorableConstructor]
|
---|
| 42 | protected SearchGraphVisualizer(bool deserializing) : base(deserializing) { }
|
---|
| 43 |
|
---|
[15821] | 44 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 45 | return new SearchGraphVisualizer(this, cloner);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | public void Register(GrammarEnumerationAlgorithm algorithm) {
|
---|
| 49 | algorithm.Started += GrammarEnumerationAlgorithmOnStarted;
|
---|
| 50 | algorithm.Stopped += GrammarEnumerationAlgorithmOnStopped;
|
---|
| 51 | algorithm.ExceptionOccurred += GrammarEnumerationAlgorithmOnStopped;
|
---|
| 52 |
|
---|
| 53 | algorithm.PhraseFetched += PhraseFetched;
|
---|
| 54 | algorithm.PhraseDerived += PhraseDerived;
|
---|
| 55 | algorithm.SentenceGenerated += SentenceGenerated;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | public void Deregister(GrammarEnumerationAlgorithm algorithm) {
|
---|
| 59 | algorithm.Started -= GrammarEnumerationAlgorithmOnStarted;
|
---|
| 60 | algorithm.Stopped -= GrammarEnumerationAlgorithmOnStopped;
|
---|
| 61 | algorithm.ExceptionOccurred -= GrammarEnumerationAlgorithmOnStopped;
|
---|
| 62 |
|
---|
| 63 | algorithm.PhraseFetched -= PhraseFetched;
|
---|
| 64 | algorithm.PhraseDerived -= PhraseDerived;
|
---|
| 65 | algorithm.SentenceGenerated -= SentenceGenerated;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | private void GrammarEnumerationAlgorithmOnStarted(object sender, EventArgs eventArgs) {
|
---|
| 69 | dotFileTrace = new TextWriterTraceListener(new FileStream(dotFileName, FileMode.Create));
|
---|
| 70 | ((StreamWriter)dotFileTrace.Writer).AutoFlush = true;
|
---|
| 71 | dotFileTrace.WriteLine("digraph searchgraph { pad=0.02; nodesep=0.3; ranksep=0.02;ratio=0.5625;");
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | private void GrammarEnumerationAlgorithmOnStopped(object sender, EventArgs eventArgs) {
|
---|
| 75 | try {
|
---|
| 76 | var alg = (GrammarEnumerationAlgorithm)sender;
|
---|
[16026] | 77 | var phrase0 = new SymbolList(new[] { alg.Grammar.StartSymbol });
|
---|
[15832] | 78 | var phrase0Hash = alg.Grammar.Hasher.CalcHashCode(phrase0);
|
---|
[15821] | 79 |
|
---|
| 80 | dotFileTrace.WriteLine($"{phrase0Hash} [label=\"{phrase0}\", shape=doublecircle]; }}");
|
---|
| 81 | dotFileTrace.Flush();
|
---|
| 82 | } catch (Exception) {
|
---|
| 83 | dotFileTrace.Close();
|
---|
| 84 | throw;
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | private void PhraseFetched(object sender, PhraseEventArgs phraseEventArgs) {
|
---|
| 89 | dotFileTrace.WriteLine($"{phraseEventArgs.Hash} [label=\"{phraseEventArgs.Phrase}\"];");
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | private void PhraseDerived(object sender, PhraseAddedEventArgs args) {
|
---|
| 93 | dotFileTrace.WriteLine($"{args.ParentHash} -> {args.NewHash} [label=\"{args.ExpandedSymbol.StringRepresentation} + → {args.ExpandedProduction}\"];");
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | private void SentenceGenerated(object sender, PhraseAddedEventArgs args) {
|
---|
| 97 | var alg = (GrammarEnumerationAlgorithm)sender;
|
---|
| 98 | dotFileTrace.WriteLine($"{args.NewHash} [label=\"{alg.Grammar.ToInfixString(args.NewPhrase)}\", style=\"filled\", fillcolor=\"#f79423\", color=\"#f79423\"];");
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 | }
|
---|