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