Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/Analysis/SearchGraphVisualizer.cs @ 15910

Last change on this file since 15910 was 15910, checked in by lkammere, 6 years ago

#2886: Fix length parameter when priorizing phrases and add weighting parameter to control exploration/exploitation during search, fix copy constructors in Analyzers

File size: 3.3 KB
Line 
1using System;
2using System.Diagnostics;
3using System.IO;
4using HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration.GrammarEnumeration;
5using HeuristicLab.Common;
6using HeuristicLab.Core;
7
8namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration {
9  class SearchGraphVisualizer : Item, IGrammarEnumerationAnalyzer {
10    private readonly string dotFileName = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\searchgraph.dot";
11
12    private TextWriterTraceListener dotFileTrace;
13
14    public SearchGraphVisualizer() { }
15
16    protected SearchGraphVisualizer(SearchGraphVisualizer original, Cloner cloner) : base(original, cloner) { }
17
18    public override IDeepCloneable Clone(Cloner cloner) {
19      return new SearchGraphVisualizer(this, cloner);
20    }
21
22    public void Register(GrammarEnumerationAlgorithm algorithm) {
23      algorithm.Started += GrammarEnumerationAlgorithmOnStarted;
24      algorithm.Stopped += GrammarEnumerationAlgorithmOnStopped;
25      algorithm.ExceptionOccurred += GrammarEnumerationAlgorithmOnStopped;
26
27      algorithm.PhraseFetched += PhraseFetched;
28      algorithm.PhraseDerived += PhraseDerived;
29      algorithm.SentenceGenerated += SentenceGenerated;
30    }
31
32    public void Deregister(GrammarEnumerationAlgorithm algorithm) {
33      algorithm.Started -= GrammarEnumerationAlgorithmOnStarted;
34      algorithm.Stopped -= GrammarEnumerationAlgorithmOnStopped;
35      algorithm.ExceptionOccurred -= GrammarEnumerationAlgorithmOnStopped;
36
37      algorithm.PhraseFetched -= PhraseFetched;
38      algorithm.PhraseDerived -= PhraseDerived;
39      algorithm.SentenceGenerated -= SentenceGenerated;
40    }
41
42    private void GrammarEnumerationAlgorithmOnStarted(object sender, EventArgs eventArgs) {
43      dotFileTrace = new TextWriterTraceListener(new FileStream(dotFileName, FileMode.Create));
44      ((StreamWriter)dotFileTrace.Writer).AutoFlush = true;
45      dotFileTrace.WriteLine("digraph searchgraph { pad=0.02; nodesep=0.3; ranksep=0.02;ratio=0.5625;");
46    }
47
48    private void GrammarEnumerationAlgorithmOnStopped(object sender, EventArgs eventArgs) {
49      try {
50        var alg = (GrammarEnumerationAlgorithm)sender;
51        var phrase0 = new SymbolString(new[] { alg.Grammar.StartSymbol });
52        var phrase0Hash = alg.Grammar.Hasher.CalcHashCode(phrase0);
53
54        dotFileTrace.WriteLine($"{phrase0Hash} [label=\"{phrase0}\", shape=doublecircle]; }}");
55        dotFileTrace.Flush();
56      } catch (Exception) {
57        dotFileTrace.Close();
58        throw;
59      }
60    }
61
62    private void PhraseFetched(object sender, PhraseEventArgs phraseEventArgs) {
63      dotFileTrace.WriteLine($"{phraseEventArgs.Hash} [label=\"{phraseEventArgs.Phrase}\"];");
64    }
65
66    private void PhraseDerived(object sender, PhraseAddedEventArgs args) {
67      dotFileTrace.WriteLine($"{args.ParentHash} -> {args.NewHash} [label=\"{args.ExpandedSymbol.StringRepresentation} + → {args.ExpandedProduction}\"];");
68    }
69
70    private void SentenceGenerated(object sender, PhraseAddedEventArgs args) {
71      var alg = (GrammarEnumerationAlgorithm)sender;
72      dotFileTrace.WriteLine($"{args.NewHash} [label=\"{alg.Grammar.ToInfixString(args.NewPhrase)}\", style=\"filled\", fillcolor=\"#f79423\", color=\"#f79423\"];");
73    }
74  }
75}
Note: See TracBrowser for help on using the repository browser.