Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/02/15 16:08:21 (8 years ago)
Author:
gkronber
Message:

#2283: several major extensions for grammatical optimization

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GrammaticalOptimization/RandomSearch.cs

    r11690 r11730  
    1313    private readonly int maxLen;
    1414    private readonly Random random;
     15    private readonly IProblem problem;
    1516
    16     public RandomSearch(int maxLen) {
     17    public RandomSearch(IProblem problem, Random random, int maxLen) {
    1718      this.maxLen = maxLen;
    18       this.random = new Random(31415);
     19      this.random = random;
     20      this.problem = problem;
    1921    }
    2022
    21     public void Run(IProblem problem, int maxIterations) {
     23    public void Run(int maxIterations) {
    2224      double bestQuality = double.MinValue;
    2325      for (int i = 0; i < maxIterations; i++) {
    24         var sentence = CreateSentence(problem.Grammar);
    25         var quality = problem.Evaluate(sentence);
     26        var sentence = CreateSentence(problem.Grammar).ToString();
     27        var quality = problem.Evaluate(sentence) / problem.GetBestKnownQuality(maxLen);
    2628        RaiseSolutionEvaluated(sentence, quality);
    2729
     
    3335    }
    3436
    35     private string CreateSentence(IGrammar grammar) {
    36       var sentence = grammar.SentenceSymbol.ToString();
     37    private Sequence CreateSentence(IGrammar grammar) {
     38      var sentence = new Sequence(grammar.SentenceSymbol);
    3739      return grammar.CompleteSentenceRandomly(random, sentence, maxLen);
    3840    }
Note: See TracChangeset for help on using the changeset viewer.