Free cookie consent management tool by TermsFeed Policy Generator

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, 9 years ago

#2283: refactoring and bug fixes

File size: 1.3 KB
RevLine 
[11730]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 {
[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.