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 |
|
---|
| 9 | namespace HeuristicLab.Algorithms.Bandits.Models {
|
---|
| 10 | public class BernoulliModel : IModel {
|
---|
[11732] | 11 | private int success;
|
---|
| 12 | private int failure;
|
---|
[11730] | 13 |
|
---|
| 14 | // parameters of beta prior distribution
|
---|
| 15 | private readonly double alpha;
|
---|
| 16 | private readonly double beta;
|
---|
| 17 |
|
---|
[11732] | 18 | public BernoulliModel(double alpha = 1.0, double beta = 1.0) {
|
---|
[11730] | 19 | this.alpha = alpha;
|
---|
| 20 | this.beta = beta;
|
---|
| 21 | }
|
---|
| 22 |
|
---|
[11732] | 23 | public double SampleExpectedReward(Random random) {
|
---|
[11730] | 24 | // sample bernoulli mean from beta prior
|
---|
[11732] | 25 | return Rand.BetaRand(random, success + alpha, failure + beta);
|
---|
[11730] | 26 | }
|
---|
| 27 |
|
---|
[11732] | 28 | public void Update(double reward) {
|
---|
| 29 | Debug.Assert(reward.IsAlmost(1.0) || reward.IsAlmost(0.0));
|
---|
| 30 | if (reward.IsAlmost(1.0)) {
|
---|
| 31 | success++;
|
---|
[11730] | 32 | } else {
|
---|
[11732] | 33 | failure++;
|
---|
[11730] | 34 | }
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | public void Reset() {
|
---|
[11732] | 38 | success = 0;
|
---|
| 39 | failure = 0;
|
---|
[11730] | 40 | }
|
---|
| 41 |
|
---|
| 42 | public void PrintStats() {
|
---|
[11732] | 43 | Console.Write("{0:F2} ", success / (double)failure);
|
---|
[11730] | 44 | }
|
---|
[11732] | 45 |
|
---|
| 46 | public object Clone() {
|
---|
| 47 | return new BernoulliModel() { failure = this.failure, success = this.success };
|
---|
| 48 | }
|
---|
[11730] | 49 | }
|
---|
| 50 | }
|
---|
Note: See
TracBrowser
for help on using the repository browser.