Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Algorithms.Bandits/Policies/UCB1TunedPolicy.cs @ 13728

Last change on this file since 13728 was 12893, checked in by gkronber, 9 years ago

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

File size: 2.0 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.BanditPolicies {
10  // policy for k-armed bandit (see Auer et al. 2002)
11  // specific to Bernoulli distributed rewards
12  public class UCB1TunedPolicy : IBanditPolicy {
13
14    public int SelectAction(Random random, IEnumerable<IBanditPolicyActionInfo> actionInfos) {
15      var myActionInfos = actionInfos.OfType<MeanAndVariancePolicyActionInfo>();
16
17      int totalTries = myActionInfos.Sum(a => a.Tries);
18
19      int aIdx = -1;
20      double bestQ = double.NegativeInfinity;
21      var bestActions = new List<int>();
22      foreach (var aInfo in myActionInfos) {
23        aIdx++;
24        double q;
25        if (aInfo.Tries == 0) {
26          q = double.PositiveInfinity;
27        } else {
28          var sumReward = aInfo.SumReward;
29          var tries = aInfo.Tries;
30
31          //var avgReward = aInfo.MaxReward;
32          var avgReward = sumReward / tries;
33          q = avgReward + Math.Sqrt((Math.Log(totalTries) / tries) * Math.Min(1.0 / 4, V(aInfo, totalTries)));
34          // 1/4 is upper bound of bernoulli distributed variable
35        }
36        if (q > bestQ) {
37          bestQ = q;
38          bestActions.Clear();
39          bestActions.Add(aIdx);
40        } else if (q.IsAlmost(bestQ)) {
41          bestActions.Add(aIdx);
42        }
43      }
44      Debug.Assert(bestActions.Any());
45
46      return bestActions.SelectRandom(random);
47    }
48
49    public IBanditPolicyActionInfo CreateActionInfo() {
50      return new MeanAndVariancePolicyActionInfo();
51    }
52
53    private double V(MeanAndVariancePolicyActionInfo actionInfo, int totalTries) {
54      var s = actionInfo.Tries;
55      return actionInfo.RewardVariance + Math.Sqrt(2 * Math.Log(totalTries) / s);
56    }
57
58    public override string ToString() {
59      return "UCB1TunedPolicy";
60    }
61  }
62}
Note: See TracBrowser for help on using the repository browser.