Rev | Line | |
---|
[11730] | 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 GenericThompsonSamplingPolicy : IBanditPolicy {
|
---|
[11730] | 11 | private readonly IModel model;
|
---|
| 12 |
|
---|
[11732] | 13 | public GenericThompsonSamplingPolicy(IModel model) {
|
---|
[11730] | 14 | this.model = model;
|
---|
| 15 | }
|
---|
| 16 |
|
---|
[11742] | 17 | public int SelectAction(Random random, IEnumerable<IBanditPolicyActionInfo> actionInfos) {
|
---|
[11732] | 18 | var myActionInfos = actionInfos.OfType<ModelPolicyActionInfo>();
|
---|
[11730] | 19 | int bestAction = -1;
|
---|
[11732] | 20 | double bestQ = double.NegativeInfinity;
|
---|
| 21 | var aIdx = -1;
|
---|
| 22 | foreach (var aInfo in myActionInfos) {
|
---|
| 23 | aIdx++;
|
---|
| 24 | var q = aInfo.SampleExpectedReward(random);
|
---|
[11974] | 25 | // very unlikely to be equal and we don't care
|
---|
[11732] | 26 | if (q > bestQ) {
|
---|
| 27 | bestQ = q;
|
---|
| 28 | bestAction = aIdx;
|
---|
[11730] | 29 | }
|
---|
| 30 | }
|
---|
[11732] | 31 | Debug.Assert(bestAction > -1);
|
---|
[11730] | 32 | return bestAction;
|
---|
| 33 | }
|
---|
| 34 |
|
---|
[11742] | 35 | public IBanditPolicyActionInfo CreateActionInfo() {
|
---|
[11732] | 36 | return new ModelPolicyActionInfo((IModel)model.Clone());
|
---|
[11730] | 37 | }
|
---|
| 38 |
|
---|
| 39 | public override string ToString() {
|
---|
[11799] | 40 | return string.Format("GenericThompsonSamplingPolicy({0})", model);
|
---|
[11730] | 41 | }
|
---|
| 42 | }
|
---|
| 43 | }
|
---|
Note: See
TracBrowser
for help on using the repository browser.