-
Notifications
You must be signed in to change notification settings - Fork 8
/
EpsilonGreedy.jl
162 lines (136 loc) · 4.38 KB
/
EpsilonGreedy.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
"""
epsGreedy( noOfArms, ϵ )
Implements constant exploration ϵ-greedy agent. `noOfArms` is the number
of arms to pick from and `ϵ` is the exploration factor.
"""
mutable struct epsGreedy <: StationaryAgentBase
noOfArms::Int64
noOfSteps::Int64
lastPlayedArm::Int64
ϵ::Float64
cummReward::Vector{Float64}
count::Vector{Int64}
avgValue::Vector{Float64}
function epsGreedy( noOfArms::Int, ϵ::Float64 )
new( noOfArms,
0,
0,
ϵ,
zeros(Float64,noOfArms),
zeros(Int64,noOfArms),
zeros(Float64,noOfArms)
)
end
end
function get_arm_index( agent::epsGreedy )
if rand() > agent.ϵ
agent.lastPlayedArm = findmax(agent.avgValue)[2]
else
agent.lastPlayedArm = rand(1:agent.noOfArms)
end
return agent.lastPlayedArm
end
function update_reward!( agent::epsGreedy, r::Real )
agent.cummReward[agent.lastPlayedArm] += r
agent.count[agent.lastPlayedArm] += 1
agent.noOfSteps += 1
agent.avgValue[agent.lastPlayedArm] = agent.cummReward[agent.lastPlayedArm] ./
agent.count[agent.lastPlayedArm]
nothing
end
function reset!( agent::epsGreedy )
agent.noOfSteps = 0
agent.lastPlayedArm = 0
agent.cummReward = zeros( Float64, agent.noOfArms )
agent.count = zeros( Int64, agent.noOfArms )
agent.avgValue = zeros( Float64, agent.noOfArms )
nothing
end
function info_str( agent::epsGreedy, latex::Bool = false )
if latex
return @sprintf( "\$\\epsilon\$-Greedy (\$\\epsilon = %4.3f\$)", agent.ϵ )
else
return @sprintf( "ϵ-Greedy (ϵ = %4.3f)", agent.ϵ )
end
end
"""
epsNGreedy( noOfArms, c , d )
Implementats decaying exploration factor ϵ-greedy agent. `noOfArms` is the number of
of options, `c` and `d` are algorithm dependent parameters.
Reference: Auer, P., Bianchi, N. C., & Fischer, P. (2002). Finite time analysis of the multiarmed bandit problem. Machine Learning, 47, 235–256.
"""
mutable struct epsNGreedy <: StationaryAgentBase
noOfArms::Int64
noOfSteps::Int64
lastPlayedArm::Int64
param_c::Float64
param_d::Float64
ϵ::Float64
cummReward::Vector{Float64}
count::Vector{Int64}
avgValue::Vector{Float64}
function epsNGreedy( noOfArms::Int, param_c::Real, param_d::Float64 )
new( noOfArms,
0,
0,
param_c,
param_d,
1,
zeros(Float64,noOfArms),
zeros(Int64,noOfArms),
zeros(Float64,noOfArms)
)
end
function epsNGreedy( noOfArms::Int64 )
new( noOfArms,
0,
0,
1/noOfArms,
1,
1,
zeros(Float64,noOfArms),
zeros(Int64,noOfArms),
zeros(Float64,noOfArms)
)
end
end
function get_arm_index( agent::epsNGreedy )
# if any(agent.count.==0)
# agent.lastPlayedArm = rand( find(agent.count.==0) )
# else
if rand() > agent.ϵ
agent.lastPlayedArm = findmax(agent.avgValue)[2]
else
agent.lastPlayedArm = rand(1:agent.noOfArms)
end
# end
return agent.lastPlayedArm
end
function update_reward!( agent::epsNGreedy, r::Real )
# Book keeping
agent.cummReward[agent.lastPlayedArm] += r
agent.count[agent.lastPlayedArm] += 1
agent.noOfSteps += 1
# Update the observed reward to the corresponding arm
agent.avgValue[agent.lastPlayedArm] = agent.cummReward[agent.lastPlayedArm] ./
agent.count[agent.lastPlayedArm]
# Also change the exploration rate
agent.ϵ = min( 1, (agent.param_c*agent.noOfArms)/(agent.param_d*agent.noOfSteps) )
nothing
end
function reset!( agent::epsNGreedy )
agent.noOfSteps = 0
agent.lastPlayedArm = 0
agent.ϵ = 1
agent.cummReward = zeros( Float64, agent.noOfArms )
agent.count = zeros( Int64, agent.noOfArms )
agent.avgValue = zeros( Float64, agent.noOfArms )
nothing
end
function info_str( agent::epsNGreedy, latex::Bool )
if latex
return @sprintf( "\$\\epsilon_n\$-Greedy (\$c = %4.3f, d = %4.3f\$)", agent.param_c, agent.param_d )
else
return @sprintf( "ϵₙ-Greedy (c = %4.3f, d = %4.3f)", agent.param_c, agent.param_d )
end
end