Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16123 was 16053, checked in by bburlacu, 6 years ago

#2886: Refactor RSquaredEvaluator as a standalone ParameterizedNamedItem which is a parameter of the algorithm. Implement BestSolutionAnalyzer analyzer for quality statistics. Add license headers where missing.

File size: 4.3 KB
Line 
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
22using System;
23using System.Diagnostics;
24using System.IO;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration {
30  [Item("SearchGraphVisualizer", "")]
31  [StorableClass]
32  internal class SearchGraphVisualizer : Item, IGrammarEnumerationAnalyzer {
33    private readonly string dotFileName = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\searchgraph.dot";
34
35    private TextWriterTraceListener dotFileTrace;
36
37    public SearchGraphVisualizer() { }
38
39    protected SearchGraphVisualizer(SearchGraphVisualizer original, Cloner cloner) : base(original, cloner) { }
40
41    [StorableConstructor]
42    protected SearchGraphVisualizer(bool deserializing) : base(deserializing) { }
43
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;
77        var phrase0 = new SymbolList(new[] { alg.Grammar.StartSymbol });
78        var phrase0Hash = alg.Grammar.Hasher.CalcHashCode(phrase0);
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} + &rarr; {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}
Note: See TracBrowser for help on using the repository browser.