Hi (again),
I have a weird issue where I need to run a much simpler version of my code
(test.jl) for the more "complicated" one to work (test2.jl).
Suppose you have just started Julia then:
If I run test.jl (this works without error), then test2.jl and then
test2.jl again my code works, note: the first test2.jl throws: error in
method definition: function Base.bytestring must be explicilty imported to
be extended...
If just run test2.jl it throws error in method definition: function
Base.bytestring must be explicilty imported to be extended and then if I
run test2.jl again it throws no method getindex... This way around nothing
works...
I think the error creeps in because I use a custom reducing function in my
parallel for loops but I have no idea why it would then work in the simpler
code? Also as far as I know I'm not extending any methods?
Any help would be appreciated...
Thanks!
****************test.jl********************************
@everywhere using Distributions
require("bandit.jl")
a = bandit(0.0, 100)
b = bandit(0.0, 100)
c = bandit_red(a,b) # shows that my reducing function works
r = @parallel (bandit_red) for k=1:100
bandit(0.0, 100)
end
****************test2.jl********************************
using PyPlot
@everywhere using Distributions
require("bandit.jl")
plays = 2500
replicates = 2000
run1 = @parallel (bandit_red) for k=1:replicates
bandit(0.0, plays)
end
run2 = @parallel (bandit_red) for k=1:replicates
bandit(0.025, plays)
end
run3 = @parallel (bandit_red) for k=1:replicates
bandit(0.05, plays)
end
# Plot the results
figure()
plot([1:plays], run1[1]/replicates,"b")
hold(true)
plot([1:plays], run2[1]/replicates,"r")
plot([1:plays], run3[1]/replicates,"g")
hold(false)
legend(("Greedy = 0.0 ","Greedy = 0.025","Greedy = 0.05"),"lower right")
figure()
plot([1:plays], run1[2]/replicates,"b")
hold(true)
plot([1:plays], run2[2]/replicates,"r")
plot([1:plays], run3[2]/replicates,"g")
hold(false)
legend(("Greedy = 0.0 ","Greedy = 0.025","Greedy = 0.05"),"lower right")
****************bandit.jl********************************
function bandit_red(a,b)
c = a[1] + b[1]
d = a[2] + b[2]
return c,d
end
function bandit(epsilon::Float64, plays::Int64)
Nbandit = 10::Int64 #arms of the bandit
Qtmeans = rand(Normal(), Nbandit) #true means of each arm
Qemeans = rand(MvNormal(Qtmeans, eye(Nbandit))) #estimated means of each arm
Qdraw(mean) = rand(Normal(mean)) #normal draw with mean
meanUpdate = ones(Nbandit,2) # array creation
meanUpdate[:,1] = Qemeans #update matrix of means of the bandit
# Play the game
reward = zeros(plays,1)
optim = zeros(plays,1)
total = 0.0
for p=1:plays
# Epsilon-Greedy Player
if rand() < epsilon
gp_index = rand(DiscreteUniform(1, Nbandit)) #return random index
else
gp_index = indmax(Qemeans) #return the max estimated index
end
# Update Step
value = Qdraw(Qtmeans[gp_index]) #draw from true means
reward[p] = value
meanUpdate[gp_index, 2] = meanUpdate[gp_index, 2] + 1.0
meanUpdate[gp_index, 1] = meanUpdate[gp_index, 1] + value
Qemeans[gp_index] = meanUpdate[gp_index, 1]/meanUpdate[gp_index, 2]
if gp_index == indmax(Qtmeans)
total += 1.0
optim[p] = total/float(p)
else
optim[p] = total/float(p)
end
end
return reward, optim
end