Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Algorithms.GrammaticalOptimization/Solvers/RandomSearch.cs @ 13599

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

#2283: experiments on grammatical optimization algorithms (maxreward instead of avg reward, ...)

File size: 1003 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 System.Random random;
8    private readonly IProblem problem;
9
10    public RandomSearch(IProblem problem, System.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.