Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GrammaticalOptimization/RandomSearch.cs @ 11846

Last change on this file since 11846 was 11846, checked in by gkronber, 9 years ago

#2283 implemented bridge to HL (solve grammatical optimization problem instances with StandardGP and OffspringSelectionGP)

File size: 989 bytes
Line 
1using System;
2using HeuristicLab.Problems.GrammaticalOptimization;
3
4namespace HeuristicLab.Algorithms.GrammaticalOptimization {
5  public class RandomSearch : SolverBase {
6    private readonly int maxLen;
7    private readonly Random random;
8    private readonly IProblem problem;
9
10    public RandomSearch(IProblem problem, Random random, int maxLen) {
11      this.maxLen = maxLen;
12      this.random = random;
13      this.problem = problem;
14    }
15
16    public override void Run(int maxIterations) {
17      for (int i = 0; i < maxIterations; i++) {
18        var sentence = CreateSentence(problem.Grammar).ToString();
19        var quality = problem.Evaluate(sentence) / problem.BestKnownQuality(maxLen);
20        OnSolutionEvaluated(sentence, quality);
21      }
22    }
23
24    private Sequence CreateSentence(IGrammar grammar) {
25      var sentence = new Sequence(grammar.SentenceSymbol);
26      return grammar.CompleteSentenceRandomly(random, sentence, maxLen);
27    }
28  }
29}
Note: See TracBrowser for help on using the repository browser.