Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Algorithms.Bandits/Policies/UCBNormalPolicy.cs @ 13585

Last change on this file since 13585 was 11832, checked in by gkronber, 10 years ago

linear value function approximation and good results for poly-10 benchmark

File size: 1.6 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  public class UCBNormalPolicy : IBanditPolicy {
11
12    public int SelectAction(Random random, IEnumerable<IBanditPolicyActionInfo> actionInfos) {
13      var myActionInfos = actionInfos.OfType<MeanAndVariancePolicyActionInfo>();
14      int totalTries = myActionInfos.Sum(a => a.Tries);
15      double bestQ = double.NegativeInfinity;
16      int aIdx = -1;
17      var bestActions = new List<int>();
18      foreach (var aInfo in myActionInfos) {
19        aIdx++;
20        double q;
21        if (totalTries <= 1 || aInfo.Tries <= 1 || aInfo.Tries <= Math.Ceiling(8 * Math.Log(totalTries))) {
22          q = double.PositiveInfinity;
23        } else {
24          var tries = aInfo.Tries;
25          var avgReward = aInfo.AvgReward;
26          var rewardVariance = aInfo.RewardVariance;
27          var estVariance = 16.0 * rewardVariance * (Math.Log(totalTries - 1) / tries);
28          q = avgReward + Math.Sqrt(estVariance);
29        }
30        if (q > bestQ) {
31          bestQ = q;
32          bestActions.Clear();
33          bestActions.Add(aIdx);
34        } else if (q.IsAlmost(bestQ)) {
35          bestActions.Add(aIdx);
36        }
37      }
38      Debug.Assert(bestActions.Any());
39      return bestActions.SelectRandom(random);
40    }
41
42    public IBanditPolicyActionInfo CreateActionInfo() {
43      return new MeanAndVariancePolicyActionInfo();
44    }
45
46    public override string ToString() {
47      return "UCBNormalPolicy";
48    }
49  }
50}
Note: See TracBrowser for help on using the repository browser.