Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions input/init.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module init
k::Int8 = 15 # Dimension of the price grid
tstable::Int32 = 1e5 # Number of iterations needed for stability
tmax::Int32 = 1e7 # Maximum number of iterations
ξ::Float32 = 0.1 # Price padding factor

"""Derived Properties"""
A::Array{Float32,1} = zeros(1) # Action space
Expand Down Expand Up @@ -65,9 +66,10 @@ module init

function init_actions(game::model)::Array{Float32,1}
"""Get action space of the firms"""
a = range(min(game.p_minmax[:,1]...), max(game.p_minmax[:,2]...), length=game.k-2);
delta = a[2] - a[1];
A = collect(a[1]-delta:delta:a[end]+2*delta)
p_bert = minimum(game.p_minmax[:,1])
p_monop = maximum(game.p_minmax[:,2])
p_range_pad = game.ξ * (p_monop - p_bert)
A = collect(range(p_bert - p_range_pad, p_monop + p_range_pad, length=game.k))
return A
end

Expand Down
17 changes: 9 additions & 8 deletions input/qlearning.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module qlearning
return a
end

function update_Q(game::Main.init.model, profits::Array{Float32,1}, s::Array{Int8,1}, s1::Array{Int8,1}, a::Array{Int8,1}, stable::Int64)::Tuple{Array{Float32,4},Int64}
function update_Q(game::Main.init.model, profits::Array{Float32,1}, s::Array{Int8,1}, s1::Array{Int8,1}, a::Array{Int8,1}, stable::Vector{Int64})::Tuple{Array{Float32,4},Vector{Int64}}
"""Update Q function"""
for n=1:game.n
old_value = game.Q[n, s[1], s[2], a[n]];
Expand All @@ -30,21 +30,22 @@ module qlearning
# Check stability
new_argmax = argmax(game.Q[n, s[1], s[2], :])
same_argmax = Int8(old_argmax == new_argmax);
stable = (stable + same_argmax) * same_argmax
stable[n] = (stable[n] + same_argmax) * same_argmax
end
return game.Q, stable
end

function check_convergence(game::Main.init.model, t::Int64, stable::Int64)::Bool
function check_convergence(game::Main.init.model, t::Int64, stable::Vector{Int64})::Bool
"""Check if game converged"""
if rem(t, game.tstable)==0
print("\nstable = ", stable)
println("stable = ", stable)
end
if stable > game.tstable
print("\nConverged!")
if all(stable .> game.tstable)
println("Converged at $t !")
println("stable = ", stable)
return true;
elseif t==game.tmax
print("\nERROR! Not Converged!")
println("ERROR! Not Converged!")
return true;
else
return false;
Expand All @@ -61,7 +62,7 @@ module qlearning
# Simulate game
function simulate_game(game::Main.init.model)::Main.init.model
s = Int8.([1,1,1]);;
stable = 0
stable = [0,0]
# Iterate until convergence
for t=1:game.tmax
a = get_actions(game, s, t)
Expand Down