Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12346 was 11849, checked in by gkronber, 10 years ago

#2283: solution reorganization

File size: 1.4 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 : 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.