Free cookie consent management tool by TermsFeed Policy Generator

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

#2283: several major extensions for grammatical optimization

Location:
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GrammaticalOptimization
Files:
6 edited

Legend:

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

    r11727 r11730  
    1717    private readonly Random random;
    1818    private readonly int contextLen;
     19    private readonly Func<Random, int, IPolicy> policyFactory;
    1920
    20     public AlternativesContextSampler(IProblem problem, int maxLen) {
     21    public AlternativesContextSampler(IProblem problem, Random random, int maxLen, int contextLen, Func<Random, int, IPolicy> policyFactory) {
    2122      this.maxLen = maxLen;
    2223      this.problem = problem;
    23       this.random = new Random(31415);
    24       this.contextLen = 25;
     24      this.random = random;
     25      this.contextLen = contextLen;
     26      this.policyFactory = policyFactory;
    2527    }
    2628
     
    2931      InitPolicies(problem.Grammar);
    3032      for (int i = 0; i < maxIterations; i++) {
    31         var sentence = SampleSentence(problem.Grammar);
    32         var quality = problem.Evaluate(sentence) / problem.GetBestKnownQuality(maxLen); 
     33        var sentence = SampleSentence(problem.Grammar).ToString();
     34        var quality = problem.Evaluate(sentence) / problem.GetBestKnownQuality(maxLen);
    3335        DistributeReward(quality);
    3436
     
    4547    private Dictionary<string, IPolicy> ntPolicy;
    4648    private List<Tuple<string, int>> updateChain;
     49
    4750    private void InitPolicies(IGrammar grammar) {
    4851      this.ntPolicy = new Dictionary<string, IPolicy>();
     
    5053    }
    5154
    52     private string SampleSentence(IGrammar grammar) {
     55    private Sequence SampleSentence(IGrammar grammar) {
    5356      updateChain.Clear();
    54       return CompleteSentence(grammar, grammar.SentenceSymbol.ToString());
     57      return CompleteSentence(grammar, new Sequence(grammar.SentenceSymbol));
    5558    }
    5659
    57     public string CompleteSentence(IGrammar g, string phrase) {
     60    public Sequence CompleteSentence(IGrammar g, Sequence phrase) {
    5861      if (phrase.Length > maxLen) throw new ArgumentException();
    5962      if (g.MinPhraseLength(phrase) > maxLen) throw new ArgumentException();
    60       bool done = phrase.All(g.IsTerminal); // terminal phrase means we are done
     63      bool done = phrase.IsTerminal; // terminal phrase means we are done
    6164      while (!done) {
    62         int ntIdx; char nt;
    63         Grammar.FindFirstNonTerminal(g, phrase, out nt, out ntIdx);
     65        char nt = phrase.FirstNonTerminal;
    6466
    6567        int maxLenOfReplacement = maxLen - (phrase.Length - 1); // replacing aAb with maxLen 4 means we can only use alternatives with a minPhraseLen <= 2
     
    6769
    6870        var alts = g.GetAlternatives(nt);
    69         string selectedAlt;
     71        Sequence selectedAlt;
    7072        // if the choice is restricted then one of the allowed alternatives is selected randomly
    7173        if (alts.Any(alt => g.MinPhraseLength(alt) > maxLenOfReplacement)) {
     
    7678        } else {
    7779          // all alts are allowed => select using bandit policy
     80          var ntIdx = phrase.FirstNonTerminalIndex;
    7881          var startIdx = Math.Max(0, ntIdx - contextLen);
    7982          var endIdx = Math.Min(startIdx + contextLen, ntIdx);
    80           var lft = phrase.Substring(startIdx, endIdx - startIdx + 1);
     83          var lft = phrase.Subsequence(startIdx, endIdx - startIdx + 1).ToString();
    8184          lft = problem.Hash(lft);
    8285          if (!ntPolicy.ContainsKey(lft)) {
    83             ntPolicy.Add(lft, new UCB1TunedPolicy(g.GetAlternatives(nt).Count()));
     86            ntPolicy.Add(lft, policyFactory(random, g.GetAlternatives(nt).Count()));
    8487          }
    8588          var selectedAltIdx = ntPolicy[lft].SelectAction();
     
    8992
    9093        // replace nt with alt
    91         phrase = phrase.Remove(ntIdx, 1);
    92         phrase = phrase.Insert(ntIdx, selectedAlt);
     94        phrase.ReplaceAt(phrase.FirstNonTerminalIndex, 1, selectedAlt);
    9395
    94         done = phrase.All(g.IsTerminal); // terminal phrase means we are done
     96        done = phrase.IsTerminal; // terminal phrase means we are done
    9597      }
    9698      return phrase;
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GrammaticalOptimization/AlternativesSampler.cs

    r11727 r11730  
    2727      InitPolicies(problem.Grammar);
    2828      for (int i = 0; i < maxIterations; i++) {
    29         var sentence = SampleSentence(problem.Grammar);
    30         var quality = problem.Evaluate(sentence) / problem.GetBestKnownQuality(maxLen); 
     29        var sentence = SampleSentence(problem.Grammar).ToString();
     30        var quality = problem.Evaluate(sentence) / problem.GetBestKnownQuality(maxLen);
    3131        DistributeReward(quality);
    3232
     
    5252    }
    5353
    54     private string SampleSentence(IGrammar grammar) {
     54    private Sequence SampleSentence(IGrammar grammar) {
    5555      updateChain.Clear();
    56       return CompleteSentence(grammar, grammar.SentenceSymbol.ToString());
     56      return CompleteSentence(grammar, new Sequence(grammar.SentenceSymbol));
    5757    }
    5858
    59     public string CompleteSentence(IGrammar g, string phrase) {
     59    public Sequence CompleteSentence(IGrammar g, Sequence phrase) {
    6060      if (phrase.Length > maxLen) throw new ArgumentException();
    6161      if (g.MinPhraseLength(phrase) > maxLen) throw new ArgumentException();
    62       bool done = phrase.All(g.IsTerminal); // terminal phrase means we are done
     62      bool done = phrase.IsTerminal; // terminal phrase means we are done
    6363      while (!done) {
    64         int ntIdx; char nt;
    65         Grammar.FindFirstNonTerminal(g, phrase, out nt, out ntIdx);
     64        char nt = phrase.FirstNonTerminal;
    6665
    6766        int maxLenOfReplacement = maxLen - (phrase.Length - 1); // replacing aAb with maxLen 4 means we can only use alternatives with a minPhraseLen <= 2
     
    6968
    7069        var alts = g.GetAlternatives(nt);
    71         string selectedAlt;
     70        Sequence selectedAlt;
    7271        // if the choice is restricted then one of the allowed alternatives is selected randomly
    7372        if (alts.Any(alt => g.MinPhraseLength(alt) > maxLenOfReplacement)) {
     
    8483
    8584        // replace nt with alt
    86         phrase = phrase.Remove(ntIdx, 1);
    87         phrase = phrase.Insert(ntIdx, selectedAlt);
     85        phrase.ReplaceAt(phrase.FirstNonTerminalIndex, 1, selectedAlt);
    8886
    89         done = phrase.All(g.IsTerminal); // terminal phrase means we are done
     87        done = phrase.IsTerminal; // terminal phrase means we are done
    9088      }
    9189      return phrase;
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GrammaticalOptimization/ExhaustiveBreadthFirstSearch.cs

    r11727 r11730  
    1111
    1212    private readonly int maxLen;
    13     private readonly Queue<string> bfsQueue = new Queue<string>();
     13    private readonly Queue<Sequence> bfsQueue = new Queue<Sequence>();
    1414    private readonly IProblem problem;
    1515
     
    2121    public void Run(int maxIterations) {
    2222      double bestQuality = double.MinValue;
    23       bfsQueue.Enqueue(problem.Grammar.SentenceSymbol.ToString());
     23      bfsQueue.Enqueue(new Sequence(problem.Grammar.SentenceSymbol));
    2424      var sentences = GenerateLanguage(problem.Grammar);
    2525      var sentenceEnumerator = sentences.GetEnumerator();
    2626      for (int i = 0; sentenceEnumerator.MoveNext() && i < maxIterations; i++) {
    27         var sentence = sentenceEnumerator.Current;
    28         var quality = problem.Evaluate(sentence) / problem.GetBestKnownQuality(maxLen); 
     27        var sentence = sentenceEnumerator.Current.ToString();
     28        var quality = problem.Evaluate(sentence) / problem.GetBestKnownQuality(maxLen);
    2929        RaiseSolutionEvaluated(sentence, quality);
    3030
     
    3737
    3838    // create sentences lazily
    39     private IEnumerable<string> GenerateLanguage(IGrammar grammar) {
     39    private IEnumerable<Sequence> GenerateLanguage(IGrammar grammar) {
    4040      while (bfsQueue.Any()) {
    4141        var phrase = bfsQueue.Dequeue();
    4242
    43         char nt;
     43        char nt = phrase.FirstNonTerminal;
    4444        int ntIdx;
    45         Grammar.FindFirstNonTerminal(grammar, phrase, out nt, out ntIdx);
     45
    4646        var alts = grammar.GetAlternatives(nt);
    4747        foreach (var alt in alts) {
    48           var newPhrase = phrase.Remove(ntIdx, 1).Insert(ntIdx, alt);
    49           if (newPhrase.All(grammar.IsTerminal) && newPhrase.Length <= maxLen) {
     48          var newPhrase = new Sequence(phrase);
     49          newPhrase.ReplaceAt(newPhrase.FirstNonTerminalIndex, 1, alt);
     50          if (newPhrase.IsTerminal && newPhrase.Length <= maxLen) {
    5051            yield return newPhrase;
    5152          } else if (grammar.MinPhraseLength(newPhrase) <= maxLen) {
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GrammaticalOptimization/ExhaustiveDepthFirstSearch.cs

    r11727 r11730  
    1111
    1212    private readonly int maxLen;
    13     private readonly Stack<string> stack = new Stack<string>();
     13    private readonly Stack<Sequence> stack = new Stack<Sequence>();
     14    private readonly IProblem problem;
    1415
    15     public ExhaustiveDepthFirstSearch(int maxLen) {
     16    public ExhaustiveDepthFirstSearch(IProblem problem, int maxLen) {
    1617      this.maxLen = maxLen;
     18      this.problem = problem;
    1719    }
    1820
    19     public void Run(IProblem problem, int maxIterations) {
     21    public void Run(int maxIterations) {
    2022      double bestQuality = double.MinValue;
    21       stack.Push(problem.Grammar.SentenceSymbol.ToString());
     23      stack.Push(new Sequence(problem.Grammar.SentenceSymbol));
    2224      var sentences = GenerateLanguage(problem.Grammar);
    2325      var sentenceEnumerator = sentences.GetEnumerator();
    2426      for (int i = 0; sentenceEnumerator.MoveNext() && i < maxIterations; i++) {
    25         var sentence = sentenceEnumerator.Current;
     27        var sentence = sentenceEnumerator.Current.ToString();
    2628        var quality = problem.Evaluate(sentence) / problem.GetBestKnownQuality(maxLen);
    2729        RaiseSolutionEvaluated(sentence, quality);
     
    3537
    3638    // create sentences lazily
    37     private IEnumerable<string> GenerateLanguage(IGrammar grammar) {
     39    private IEnumerable<Sequence> GenerateLanguage(IGrammar grammar) {
    3840      while (stack.Any()) {
    3941        var phrase = stack.Pop();
    4042
    41         char nt;
    42         int ntIdx;
    43         Grammar.FindFirstNonTerminal(grammar, phrase, out nt, out ntIdx);
     43        char nt = phrase.FirstNonTerminal;
    4444        var alts = grammar.GetAlternatives(nt);
    4545        foreach (var alt in alts) {
    46           var newPhrase = phrase.Remove(ntIdx, 1).Insert(ntIdx, alt);
    47           if (newPhrase.All(grammar.IsTerminal) && newPhrase.Length <= maxLen) {
     46          var newPhrase = new Sequence(phrase);
     47          newPhrase.ReplaceAt(newPhrase.FirstNonTerminalIndex, 1, alt);
     48
     49          if (newPhrase.IsTerminal && newPhrase.Length <= maxLen) {
    4850            yield return newPhrase;
    4951          } else if (grammar.MinPhraseLength(newPhrase) <= maxLen) {
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GrammaticalOptimization/MctsSampler.cs

    r11727 r11730  
    1010  public class MctsSampler {
    1111    private class TreeNode {
     12      public string ident;
    1213      public int randomTries;
     14      public int policyTries;
    1315      public IPolicy policy;
    1416      public TreeNode[] children;
    1517      public bool done = false;
    1618
     19      public TreeNode(string id) {
     20        this.ident = id;
     21      }
     22
    1723      public override string ToString() {
    18         return string.Format("Node(random-tries: {0}, done: {1}, policy: {2})", randomTries, done, policy);
     24        return string.Format("Node({0} tries: {1}, done: {2}, policy: {3})", ident, randomTries + policyTries, done, policy);
    1925      }
    2026    }
     27
    2128
    2229    public event Action<string, double> FoundNewBestSolution;
     
    2734    private readonly Random random;
    2835    private readonly int randomTries;
    29     private readonly Func<int, IPolicy> policyFactory;
     36    private readonly Func<Random, int, IPolicy> policyFactory;
    3037
    3138    private List<Tuple<TreeNode, int>> updateChain;
    3239    private TreeNode rootNode;
    3340
     41    public int treeDepth;
     42    public int treeSize;
     43
    3444    public MctsSampler(IProblem problem, int maxLen, Random random) :
    35       this(problem, maxLen, random, 10, (numActions) => new EpsGreedyPolicy(random, numActions, 0.1)) {
     45      this(problem, maxLen, random, 10, (rand, numActions) => new EpsGreedyPolicy(rand, numActions, 0.1)) {
    3646
    3747    }
    3848
    39     public MctsSampler(IProblem problem, int maxLen, Random random, int randomTries, Func<int, IPolicy> policyFactory) {
     49    public MctsSampler(IProblem problem, int maxLen, Random random, int randomTries, Func<Random, int, IPolicy> policyFactory) {
    4050      this.maxLen = maxLen;
    4151      this.problem = problem;
     
    4757    public void Run(int maxIterations) {
    4858      double bestQuality = double.MinValue;
    49       InitPolicies();
     59      InitPolicies(problem.Grammar);
    5060      for (int i = 0; !rootNode.done && i < maxIterations; i++) {
    51         var sentence = SampleSentence(problem.Grammar);
     61        var sentence = SampleSentence(problem.Grammar).ToString();
    5262        var quality = problem.Evaluate(sentence) / problem.GetBestKnownQuality(maxLen);
    5363        Debug.Assert(quality >= 0 && quality <= 1.0);
     
    6171        }
    6272      }
     73
     74      // clean up
     75      InitPolicies(problem.Grammar); GC.Collect();
    6376    }
    6477
    65     private void InitPolicies() {
    66       this.updateChain = new List<Tuple<TreeNode, int>>();
    67       rootNode = new TreeNode();
     78    public void PrintStats() {
     79      var n = rootNode;
     80      Console.WriteLine("depth: {0,5} size: {1,10} root tries {2,10}", treeDepth, treeSize, rootNode.policyTries + rootNode.randomTries);
     81      while (n.policy != null) {
     82        Console.WriteLine();
     83        Console.WriteLine("{0,5}->{1,-50}", n.ident, string.Join(" ", n.children.Select(ch => string.Format("{0,4}", ch.ident))));
     84        Console.WriteLine("{0,5}  {1,-50}", string.Empty, string.Join(" ", n.children.Select(ch => string.Format("{0,4}", ch.randomTries + ch.policyTries))));
     85        //n.policy.PrintStats();
     86        n = n.children.OrderByDescending(c => c.policyTries).First();
     87      }
     88      Console.ReadLine();
    6889    }
    6990
    70     private string SampleSentence(IGrammar grammar) {
    71       updateChain.Clear();
    72       return CompleteSentence(grammar, grammar.SentenceSymbol.ToString());
     91    private void InitPolicies(IGrammar grammar) {
     92      this.updateChain = new List<Tuple<TreeNode, int>>();
     93
     94      rootNode = new TreeNode(grammar.SentenceSymbol.ToString());
     95      treeDepth = 0;
     96      treeSize = 0;
    7397    }
    7498
    75     public string CompleteSentence(IGrammar g, string phrase) {
     99    private Sequence SampleSentence(IGrammar grammar) {
     100      updateChain.Clear();
     101      var startPhrase = new Sequence(grammar.SentenceSymbol);
     102      return CompleteSentence(grammar, startPhrase);
     103    }
     104
     105    private Sequence CompleteSentence(IGrammar g, Sequence phrase) {
    76106      if (phrase.Length > maxLen) throw new ArgumentException();
    77107      if (g.MinPhraseLength(phrase) > maxLen) throw new ArgumentException();
    78108      TreeNode n = rootNode;
    79       bool done = phrase.All(g.IsTerminal); // terminal phrase means we are done
     109      bool done = phrase.IsTerminal;
    80110      int selectedAltIdx = -1;
     111      var curDepth = 0;
    81112      while (!done) {
    82         int ntIdx; char nt;
    83         Grammar.FindFirstNonTerminal(g, phrase, out nt, out ntIdx);
     113        char nt = phrase.FirstNonTerminal;
    84114
    85115        int maxLenOfReplacement = maxLen - (phrase.Length - 1); // replacing aAb with maxLen 4 means we can only use alternatives with a minPhraseLen <= 2
     
    90120        if (n.randomTries < randomTries) {
    91121          n.randomTries++;
     122
     123          treeDepth = Math.Max(treeDepth, curDepth);
     124
    92125          return g.CompleteSentenceRandomly(random, phrase, maxLen);
    93126        } else if (n.randomTries == randomTries && n.policy == null) {
    94           n.policy = policyFactory(alts.Count());
    95           n.children = alts.Select(_ => new TreeNode()).ToArray(); // create a new node for each alternative
     127          n.policy = policyFactory(random, alts.Count());
     128          //n.children = alts.Select(alt => new TreeNode(alt.ToString())).ToArray(); // create a new node for each alternative
     129          n.children = alts.Select(alt => new TreeNode(string.Empty)).ToArray(); // create a new node for each alternative
     130
     131          treeSize += n.children.Length;
    96132        }
    97 
     133        n.policyTries++;
    98134        // => select using bandit policy
    99135        selectedAltIdx = n.policy.SelectAction();
    100         string selectedAlt = alts.ElementAt(selectedAltIdx);
     136        Sequence selectedAlt = alts.ElementAt(selectedAltIdx);
     137
    101138        // replace nt with alt
    102         phrase = phrase.Remove(ntIdx, 1);
    103         phrase = phrase.Insert(ntIdx, selectedAlt);
     139        phrase.ReplaceAt(phrase.FirstNonTerminalIndex, 1, selectedAlt);
    104140
    105141        updateChain.Add(Tuple.Create(n, selectedAltIdx));
    106142
    107         done = phrase.All(g.IsTerminal); // terminal phrase means we are done
     143        curDepth++;
     144
     145        done = phrase.IsTerminal;
    108146        if (!done) {
    109147          // prepare for next iteration
     
    116154      n.children[selectedAltIdx].done = true;
    117155
     156      treeDepth = Math.Max(treeDepth, curDepth);
    118157      return phrase;
    119158    }
     
    127166        var policy = node.policy;
    128167        var action = e.Item2;
     168        //policy.UpdateReward(action, reward / updateChain.Count);
    129169        policy.UpdateReward(action, reward);
    130170
  • 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.