Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.Bandits/BanditPolicies/UCB1Policy.cs @ 11742

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

#2283 refactoring

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;
7
8namespace HeuristicLab.Algorithms.Bandits.BanditPolicies {
9  // policy for k-armed bandit (see Auer et al. 2002)
10  public class UCB1Policy : IBanditPolicy {
11    public int SelectAction(Random random, IEnumerable<IBanditPolicyActionInfo> actionInfos) {
12      var myActionInfos = actionInfos.OfType<DefaultPolicyActionInfo>().ToArray(); // TODO: performance
13      int bestAction = -1;
14      double bestQ = double.NegativeInfinity;
15      int totalTries = myActionInfos.Where(a => !a.Disabled).Sum(a => a.Tries);
16
17      for (int a = 0; a < myActionInfos.Length; a++) {
18        if (myActionInfos[a].Disabled) continue;
19        if (myActionInfos[a].Tries == 0) return a;
20        var q = myActionInfos[a].SumReward / myActionInfos[a].Tries + Math.Sqrt((2 * Math.Log(totalTries)) / myActionInfos[a].Tries);
21        if (q > bestQ) {
22          bestQ = q;
23          bestAction = a;
24        }
25      }
26      Debug.Assert(bestAction > -1);
27      return bestAction;
28    }
29
30    public IBanditPolicyActionInfo CreateActionInfo() {
31      return new DefaultPolicyActionInfo();
32    }
33    public override string ToString() {
34      return "UCB1Policy";
35    }
36  }
37}
Note: See TracBrowser for help on using the repository browser.