Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/24/15 13:56:27 (9 years ago)
Author:
gkronber
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Problems.GrammaticalOptimization/Problems/SantaFeAntProblem.cs

    r12876 r12893  
    3030    // here we use a modified grammar which has only one syntax tree for each sentence
    3131
     32    public IEnumerable<string> OptimalSentences {
     33      get {
     34        return new string[]
     35        {
     36          "?(m)(ll?(m)(r))mr",
     37          "?(m)(rr?(m)(l))ml",
     38          "r?(m)(ll?(m)(r))m",
     39          "l?(m)(rr?(m)(l))m",
     40          "mr?(m)(ll?(m)(r))",
     41          "ml?(m)(rr?(m)(l))",
     42          "?(m)(ll?(m)(l))ml",
     43          "?(m)(rr?(m)(r))mr",
     44          "l?(m)(ll?(m)(l))m",
     45          "r?(m)(rr?(m)(r))m",
     46          "ml?(m)(ll?(m)(l))",
     47          "mr?(m)(rr?(m)(r))",
     48        };
     49      }
     50    }
    3251
    3352    private readonly IGrammar grammar;
     
    138157
    139158    public IEnumerable<Feature> GetFeatures(string phrase) {
    140       yield return new Feature(phrase, 1.0);
     159      for (int idx = 0; idx < 15; idx++) {
     160        foreach (var t in Grammar.TerminalSymbols)
     161          yield return new Feature(t.ToString(), phrase.Length > idx ? phrase[idx] == t ? 1 : 0 : 0);
     162      }
    141163      //var ant = new Ant(false);
    142164      //int p = 0;
     
    166188      }
    167189    }
     190    public bool IsOptimalPhrase(string phrase) {
     191      var firstNonTerminalIdx = new Sequence(phrase).FirstNonTerminalIndex;
     192      if (firstNonTerminalIdx == -1) firstNonTerminalIdx = phrase.Length;
     193      return OptimalSentences.Any(s => s.Substring(0, firstNonTerminalIdx) == phrase.Substring(0, firstNonTerminalIdx));
     194    }
     195
    168196  }
    169197
Note: See TracChangeset for help on using the changeset viewer.