Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.Bandits/TruncatedNormalBandit.cs @ 12394

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

#2283: solution reorganization

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