Rev | Line | |
---|
[11727] | 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 |
|
---|
[11742] | 9 | namespace HeuristicLab.Algorithms.Bandits.BanditPolicies {
|
---|
| 10 | public class BernoulliThompsonSamplingPolicy : IBanditPolicy {
|
---|
[11727] | 11 | // parameters of beta prior distribution
|
---|
| 12 | private readonly double alpha = 1.0;
|
---|
| 13 | private readonly double beta = 1.0;
|
---|
| 14 |
|
---|
[11742] | 15 | public int SelectAction(Random random, IEnumerable<IBanditPolicyActionInfo> actionInfos) {
|
---|
| 16 | var myActionInfos = actionInfos.OfType<BernoulliPolicyActionInfo>();
|
---|
[11732] | 17 | int bestAction = -1;
|
---|
| 18 | double maxTheta = double.NegativeInfinity;
|
---|
| 19 | var aIdx = -1;
|
---|
[11727] | 20 |
|
---|
[11732] | 21 | foreach (var aInfo in myActionInfos) {
|
---|
| 22 | aIdx++;
|
---|
| 23 | if (aInfo.Disabled) continue;
|
---|
| 24 | var theta = Rand.BetaRand(random, aInfo.NumSuccess + alpha, aInfo.NumFailure + beta);
|
---|
[11727] | 25 | if (theta > maxTheta) {
|
---|
| 26 | maxTheta = theta;
|
---|
[11732] | 27 | bestAction = aIdx;
|
---|
[11727] | 28 | }
|
---|
| 29 | }
|
---|
[11732] | 30 | Debug.Assert(bestAction > -1);
|
---|
[11727] | 31 | return bestAction;
|
---|
| 32 | }
|
---|
| 33 |
|
---|
[11742] | 34 | public IBanditPolicyActionInfo CreateActionInfo() {
|
---|
[11732] | 35 | return new BernoulliPolicyActionInfo();
|
---|
[11727] | 36 | }
|
---|
| 37 |
|
---|
| 38 |
|
---|
[11730] | 39 | public override string ToString() {
|
---|
| 40 | return "BernoulliThompsonSamplingPolicy";
|
---|
| 41 | }
|
---|
[11727] | 42 | }
|
---|
| 43 | }
|
---|
Note: See
TracBrowser
for help on using the repository browser.