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