source: branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.Bandits/Policies/GenericThompsonSamplingPolicy.cs @ 11732

Last change on this file since 11732 was 11732, checked in by gkronber, 5 years ago

#2283: refactoring and bug fixes

File size: 1.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Diagnostics;
4using System.Linq;
5using System.Text;
6using System.Threading.Tasks;
7using HeuristicLab.Common;
8
9namespace HeuristicLab.Algorithms.Bandits {
10  public class GenericThompsonSamplingPolicy : IPolicy {
11    private readonly IModel model;
12
13    public GenericThompsonSamplingPolicy(IModel model) {
14      this.model = model;
15    }
16
17    public int SelectAction(Random random, IEnumerable<IPolicyActionInfo> actionInfos) {
18      var myActionInfos = actionInfos.OfType<ModelPolicyActionInfo>();
19      int bestAction = -1;
20      double bestQ = double.NegativeInfinity;
21      var aIdx = -1;
22      foreach (var aInfo in myActionInfos) {
23        aIdx++;
24        if (aInfo.Disabled) continue;
25        //if (aInfo.Tries == 0) return aIdx;
26        var q = aInfo.SampleExpectedReward(random);
27        if (q > bestQ) {
28          bestQ = q;
29          bestAction = aIdx;
30        }
31      }
32      Debug.Assert(bestAction > -1);
33      return bestAction;
34    }
35
36    public IPolicyActionInfo CreateActionInfo() {
37      return new ModelPolicyActionInfo((IModel)model.Clone());
38    }
39
40    public override string ToString() {
41      return string.Format("GenericThompsonSamplingPolicy({0})", model);
42    }
43  }
44}
Note: See TracBrowser for help on using the repository browser.