[11690] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using HeuristicLab.Problems.GrammaticalOptimization;
|
---|
| 6 |
|
---|
| 7 | namespace HeuristicLab.Algorithms.GrammaticalOptimization {
|
---|
| 8 | public class ExhaustiveDepthFirstSearch {
|
---|
| 9 | public event Action<string, double> FoundNewBestSolution;
|
---|
| 10 | public event Action<string, double> SolutionEvaluated;
|
---|
| 11 |
|
---|
| 12 | private readonly int maxLen;
|
---|
[11730] | 13 | private readonly Stack<Sequence> stack = new Stack<Sequence>();
|
---|
| 14 | private readonly IProblem problem;
|
---|
[11690] | 15 |
|
---|
[11730] | 16 | public ExhaustiveDepthFirstSearch(IProblem problem, int maxLen) {
|
---|
[11708] | 17 | this.maxLen = maxLen;
|
---|
[11730] | 18 | this.problem = problem;
|
---|
[11690] | 19 | }
|
---|
| 20 |
|
---|
[11730] | 21 | public void Run(int maxIterations) {
|
---|
[11690] | 22 | double bestQuality = double.MinValue;
|
---|
[11730] | 23 | stack.Push(new Sequence(problem.Grammar.SentenceSymbol));
|
---|
[11690] | 24 | var sentences = GenerateLanguage(problem.Grammar);
|
---|
| 25 | var sentenceEnumerator = sentences.GetEnumerator();
|
---|
| 26 | for (int i = 0; sentenceEnumerator.MoveNext() && i < maxIterations; i++) {
|
---|
[11730] | 27 | var sentence = sentenceEnumerator.Current.ToString();
|
---|
[11732] | 28 | var quality = problem.Evaluate(sentence) / problem.BestKnownQuality(maxLen);
|
---|
[11690] | 29 | RaiseSolutionEvaluated(sentence, quality);
|
---|
| 30 |
|
---|
| 31 | if (quality > bestQuality) {
|
---|
| 32 | bestQuality = quality;
|
---|
| 33 | RaiseFoundNewBestSolution(sentence, quality);
|
---|
| 34 | }
|
---|
| 35 | }
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | // create sentences lazily
|
---|
[11730] | 39 | private IEnumerable<Sequence> GenerateLanguage(IGrammar grammar) {
|
---|
[11690] | 40 | while (stack.Any()) {
|
---|
| 41 | var phrase = stack.Pop();
|
---|
| 42 |
|
---|
[11730] | 43 | char nt = phrase.FirstNonTerminal;
|
---|
[11690] | 44 | var alts = grammar.GetAlternatives(nt);
|
---|
| 45 | foreach (var alt in alts) {
|
---|
[11730] | 46 | var newPhrase = new Sequence(phrase);
|
---|
| 47 | newPhrase.ReplaceAt(newPhrase.FirstNonTerminalIndex, 1, alt);
|
---|
| 48 |
|
---|
| 49 | if (newPhrase.IsTerminal && newPhrase.Length <= maxLen) {
|
---|
[11690] | 50 | yield return newPhrase;
|
---|
| 51 | } else if (grammar.MinPhraseLength(newPhrase) <= maxLen) {
|
---|
| 52 | stack.Push(newPhrase);
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 |
|
---|
| 59 | private void RaiseSolutionEvaluated(string sentence, double quality) {
|
---|
| 60 | var handler = SolutionEvaluated;
|
---|
| 61 | if (handler != null) handler(sentence, quality);
|
---|
| 62 | }
|
---|
| 63 | private void RaiseFoundNewBestSolution(string sentence, double quality) {
|
---|
| 64 | var handler = FoundNewBestSolution;
|
---|
| 65 | if (handler != null) handler(sentence, quality);
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 | }
|
---|