Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.Bandits/Bandits/BernoulliBandit.cs @ 11727

Last change on this file since 11727 was 11711, checked in by gkronber, 9 years ago

#2283: folders for bandits and policies

File size: 1.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6
7namespace HeuristicLab.Algorithms.Bandits {
8  public class BernoulliBandit {
9    public int NumArms { get; private set; }
10    public double OptimalExpectedReward { get; private set; } // reward of the best arm, for calculating regret
11    private readonly Random random;
12    private readonly double[] expReward;
13    public BernoulliBandit(Random random, int nArms) {
14      this.random = random;
15      this.NumArms = nArms;
16      // expected reward of arms is iid and uniformly distributed
17      expReward = new double[nArms];
18      OptimalExpectedReward = double.NegativeInfinity;
19      for (int i = 0; i < nArms; i++) {
20        expReward[i] = random.NextDouble();
21        if (expReward[i] > OptimalExpectedReward) OptimalExpectedReward = expReward[i];
22      }
23    }
24
25    // pulling an arm results in a bernoulli distributed reward
26    // with mean expReward[i]
27    public double Pull(int arm) {
28      return random.NextDouble() <= expReward[arm] ? 1 : 0;
29    }
30  }
31}
Note: See TracBrowser for help on using the repository browser.