source: branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.Bandits/Models/BernoulliModel.cs @ 11732

Last change on this file since 11732 was 11732, checked in by gkronber, 5 years ago

#2283: refactoring and bug fixes

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