Ignore:
Timestamp:
01/07/15 09:21:46 (5 years ago)
Author:
gkronber
Message:

#2283: refactoring and bug fixes

File:
1 edited

Legend:

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

    r11730 r11732  
    1717    private readonly Random random;
    1818    private readonly int contextLen;
    19     private readonly Func<Random, int, IPolicy> policyFactory;
     19    private readonly IPolicy policy;
    2020
    21     public AlternativesContextSampler(IProblem problem, Random random, int maxLen, int contextLen, Func<Random, int, IPolicy> policyFactory) {
     21    public AlternativesContextSampler(IProblem problem, Random random, int maxLen, int contextLen, IPolicy policy) {
    2222      this.maxLen = maxLen;
    2323      this.problem = problem;
    2424      this.random = random;
    2525      this.contextLen = contextLen;
    26       this.policyFactory = policyFactory;
     26      this.policy = policy;
    2727    }
    2828
     
    3232      for (int i = 0; i < maxIterations; i++) {
    3333        var sentence = SampleSentence(problem.Grammar).ToString();
    34         var quality = problem.Evaluate(sentence) / problem.GetBestKnownQuality(maxLen);
     34        var quality = problem.Evaluate(sentence) / problem.BestKnownQuality(maxLen);
    3535        DistributeReward(quality);
    3636
     
    4545
    4646
    47     private Dictionary<string, IPolicy> ntPolicy;
     47    private Dictionary<string, IPolicyActionInfo[]> contextActionInfos;
    4848    private List<Tuple<string, int>> updateChain;
    4949
    5050    private void InitPolicies(IGrammar grammar) {
    51       this.ntPolicy = new Dictionary<string, IPolicy>();
     51      this.contextActionInfos = new Dictionary<string, IPolicyActionInfo[]>();
    5252      this.updateChain = new List<Tuple<string, int>>();
    5353    }
     
    8383          var lft = phrase.Subsequence(startIdx, endIdx - startIdx + 1).ToString();
    8484          lft = problem.Hash(lft);
    85           if (!ntPolicy.ContainsKey(lft)) {
    86             ntPolicy.Add(lft, policyFactory(random, g.GetAlternatives(nt).Count()));
     85          if (!contextActionInfos.ContainsKey(lft)) {
     86            contextActionInfos.Add(lft, g.GetAlternatives(nt).Select(_ => policy.CreateActionInfo()).ToArray());
    8787          }
    88           var selectedAltIdx = ntPolicy[lft].SelectAction();
     88          var selectedAltIdx = policy.SelectAction(random, contextActionInfos[lft]);
    8989          selectedAlt = alts.ElementAt(selectedAltIdx);
    9090          updateChain.Add(Tuple.Create(lft, selectedAltIdx));
     
    103103        var lft = e.Item1;
    104104        var action = e.Item2;
    105         ntPolicy[lft].UpdateReward(action, reward);
     105        contextActionInfos[lft][action].UpdateReward(reward);
    106106      }
    107107    }
Note: See TracChangeset for help on using the changeset viewer.