Last change
on this file since 13231 was
11806,
checked in by gkronber, 10 years ago
|
#2283: separated value-states from done-states in GenericGrammarPolicy and removed disabling of actions from bandit policies
|
File size:
1.4 KB
|
Line | |
---|
1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Diagnostics;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Text;
|
---|
6 | using System.Threading.Tasks;
|
---|
7 | using HeuristicLab.Common;
|
---|
8 |
|
---|
9 | namespace HeuristicLab.Algorithms.Bandits.BanditPolicies {
|
---|
10 | // policy for k-armed bandit (see Auer et al. 2002)
|
---|
11 | public class UCB1Policy : IBanditPolicy {
|
---|
12 | public int SelectAction(Random random, IEnumerable<IBanditPolicyActionInfo> actionInfos) {
|
---|
13 | var myActionInfos = actionInfos.OfType<DefaultPolicyActionInfo>();
|
---|
14 | double bestQ = double.NegativeInfinity;
|
---|
15 | int totalTries = myActionInfos.Sum(a => a.Tries);
|
---|
16 |
|
---|
17 | var bestActions = new List<int>();
|
---|
18 | int aIdx = -1;
|
---|
19 | foreach (var aInfo in myActionInfos) {
|
---|
20 | aIdx++;
|
---|
21 | double q;
|
---|
22 | if (aInfo.Tries == 0) {
|
---|
23 | q = double.PositiveInfinity;
|
---|
24 | } else {
|
---|
25 |
|
---|
26 | q = aInfo.SumReward / aInfo.Tries + 0.5 * Math.Sqrt((2 * Math.Log(totalTries)) / aInfo.Tries);
|
---|
27 | }
|
---|
28 | if (q > bestQ) {
|
---|
29 | bestQ = q;
|
---|
30 | bestActions.Clear();
|
---|
31 | bestActions.Add(aIdx);
|
---|
32 | } else if (q.IsAlmost(bestQ)) {
|
---|
33 | bestActions.Add(aIdx);
|
---|
34 | }
|
---|
35 | }
|
---|
36 | Debug.Assert(bestActions.Any());
|
---|
37 | return bestActions.SelectRandom(random);
|
---|
38 | }
|
---|
39 |
|
---|
40 | public IBanditPolicyActionInfo CreateActionInfo() {
|
---|
41 | return new DefaultPolicyActionInfo();
|
---|
42 | }
|
---|
43 | public override string ToString() {
|
---|
44 | return "UCB1Policy";
|
---|
45 | }
|
---|
46 | }
|
---|
47 | }
|
---|
Note: See
TracBrowser
for help on using the repository browser.