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