Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.Bandits/GaussianBandit.cs @ 13693

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

#2283: solution reorganization

File size: 1.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using HeuristicLab.Common;
7
8namespace 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}
Note: See TracBrowser for help on using the repository browser.