[11710] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using System.Threading.Tasks;
|
---|
[11730] | 6 | using HeuristicLab.Common;
|
---|
[11710] | 7 |
|
---|
| 8 | namespace HeuristicLab.Algorithms.Bandits {
|
---|
[11730] | 9 | public class TruncatedNormalBandit : IBandit {
|
---|
[11710] | 10 | public int NumArms { get; private set; }
|
---|
| 11 | public double OptimalExpectedReward { get; private set; } // reward of the best arm, for calculating regret
|
---|
[11730] | 12 | public int OptimalExpectedRewardArm { get; private set; }
|
---|
| 13 | // the arm with highest expected reward also has the highest probability of return a reward of 1.0
|
---|
| 14 | public int OptimalMaximalRewardArm { get { return OptimalExpectedRewardArm; } }
|
---|
| 15 |
|
---|
[11710] | 16 | private readonly Random random;
|
---|
| 17 | private readonly double[] expReward;
|
---|
| 18 | public TruncatedNormalBandit(Random random, int nArms) {
|
---|
| 19 | this.random = random;
|
---|
| 20 | this.NumArms = nArms;
|
---|
| 21 | // expected reward of arms is iid and uniformly distributed
|
---|
| 22 | expReward = new double[nArms];
|
---|
| 23 | OptimalExpectedReward = double.NegativeInfinity;
|
---|
| 24 | for (int i = 0; i < nArms; i++) {
|
---|
[11730] | 25 | expReward[i] = random.NextDouble() * 0.7;
|
---|
| 26 | if (expReward[i] > OptimalExpectedReward) {
|
---|
| 27 | OptimalExpectedReward = expReward[i];
|
---|
| 28 | OptimalExpectedRewardArm = i;
|
---|
| 29 | }
|
---|
[11710] | 30 | }
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | // pulling an arm results in a truncated normally distributed reward
|
---|
| 34 | // with mean expReward[i] and std.dev 0.1
|
---|
| 35 | public double Pull(int arm) {
|
---|
| 36 | double x = 0;
|
---|
| 37 | do {
|
---|
[11730] | 38 | var z = Rand.RandNormal(random);
|
---|
[11710] | 39 | x = z * 0.1 + expReward[arm];
|
---|
| 40 | }
|
---|
| 41 | while (x < 0 || x > 1);
|
---|
| 42 | return x;
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 | }
|
---|