1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Diagnostics;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Text;
|
---|
6 | using System.Threading.Tasks;
|
---|
7 |
|
---|
8 | namespace HeuristicLab.Algorithms.Bandits {
|
---|
9 | public class Exp3Policy : BanditPolicy {
|
---|
10 | private readonly Random random;
|
---|
11 | private readonly double gamma;
|
---|
12 | private readonly double[] w;
|
---|
13 | // TODO: debug (very large weights over time)
|
---|
14 | public Exp3Policy(Random random, int numActions, double gamma)
|
---|
15 | : base(numActions) {
|
---|
16 | if (gamma < 0 || gamma > 1) throw new ArgumentException();
|
---|
17 | this.random = random;
|
---|
18 | this.gamma = gamma;
|
---|
19 | this.w = Enumerable.Repeat(1.0, numActions).ToArray();
|
---|
20 | }
|
---|
21 |
|
---|
22 | public override int SelectAction() {
|
---|
23 | Debug.Assert(Actions.Any());
|
---|
24 | var numActions = Actions.Count();
|
---|
25 | var sumW = w.Sum();
|
---|
26 | var r = random.NextDouble();
|
---|
27 | var sumP = (1 - gamma) * w[0] / sumW + gamma / numActions;
|
---|
28 | int i = 0;
|
---|
29 | while (r > sumP) {
|
---|
30 | i++;
|
---|
31 | sumP += (1 - gamma) * w[i] / sumW + gamma / numActions;
|
---|
32 | }
|
---|
33 | Debug.Assert(i >= 0 && i < numActions);
|
---|
34 | return i;
|
---|
35 | }
|
---|
36 |
|
---|
37 | public override void UpdateReward(int action, double reward) {
|
---|
38 | Debug.Assert(Actions.Contains(action));
|
---|
39 | var numActions = Actions.Count();
|
---|
40 | var p = (1 - gamma) * w[action] / w.Sum() + gamma / numActions;
|
---|
41 | var estReward = reward / p;
|
---|
42 | w[action] = w[action] * Math.Exp(gamma * estReward / numActions);
|
---|
43 | }
|
---|
44 |
|
---|
45 | public override void DisableAction(int action) {
|
---|
46 | base.DisableAction(action);
|
---|
47 | w[action] = 0;
|
---|
48 | }
|
---|
49 |
|
---|
50 | public override void Reset() {
|
---|
51 | base.Reset();
|
---|
52 | foreach (var a in Actions) w[a] = 1.0;
|
---|
53 | }
|
---|
54 | }
|
---|
55 | }
|
---|