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

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

#2283 refactoring

File size: 1.7 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  // also called softmax policy
11  public class BoltzmannExplorationPolicy : IBanditPolicy {
12    private readonly double beta;
13    private readonly Func<DefaultPolicyActionInfo, double> valueFunction;
14
15    public BoltzmannExplorationPolicy(double eps) : this(eps, DefaultPolicyActionInfo.AverageReward) { }
16
17    public BoltzmannExplorationPolicy(double beta, Func<DefaultPolicyActionInfo, double> valueFunction) {
18      if (beta < 0) throw new ArgumentException();
19      this.beta = beta;
20      this.valueFunction = valueFunction;
21    }
22    public int SelectAction(Random random, IEnumerable<IBanditPolicyActionInfo> actionInfos) {
23      Debug.Assert(actionInfos.Any());
24
25      // select best
26      var myActionInfos = actionInfos.OfType<DefaultPolicyActionInfo>();
27      Debug.Assert(myActionInfos.Any(a => !a.Disabled));
28
29      var w = from aInfo in myActionInfos
30              select aInfo.Disabled
31                ? 0.0
32                : Math.Exp(beta * valueFunction(aInfo));
33
34      var bestAction = myActionInfos
35        .Select((aInfo, idx) => new { aInfo, idx })
36        .SampleProportional(random, w)
37        .Select(p => p.idx)
38        .First();
39      Debug.Assert(bestAction >= 0);
40      return bestAction;
41    }
42
43    public IBanditPolicyActionInfo CreateActionInfo() {
44      return new DefaultPolicyActionInfo();
45    }
46
47    public override string ToString() {
48      return string.Format("BoltzmannExplorationPolicy({0:F2})", beta);
49    }
50  }
51}
Note: See TracBrowser for help on using the repository browser.