From 4e06ef88bff7173b2ec1e8c9d536b2886f8c9190 Mon Sep 17 00:00:00 2001 From: Jeremiah <4462211+jeremiahpslewis@users.noreply.github.com> Date: Tue, 20 Jun 2023 20:44:18 +0200 Subject: [PATCH 1/4] Fix actions --- input/init.jl | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/input/init.jl b/input/init.jl index bd0e1c5..b733ff3 100644 --- a/input/init.jl +++ b/input/init.jl @@ -65,9 +65,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 From eca94160e7f63f4702237cb13dc7e2dc39c1f43a Mon Sep 17 00:00:00 2001 From: Jeremiah <4462211+jeremiahpslewis@users.noreply.github.com> Date: Tue, 20 Jun 2023 21:31:09 +0200 Subject: [PATCH 2/4] Update init.jl --- input/init.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/input/init.jl b/input/init.jl index b733ff3..1b8d9e9 100644 --- a/input/init.jl +++ b/input/init.jl @@ -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 """Derived Properties""" A::Array{Float32,1} = zeros(1) # Action space From 5c7d42841d683211474331df9f3056e61b0b3f76 Mon Sep 17 00:00:00 2001 From: Jeremiah <4462211+jeremiahpslewis@users.noreply.github.com> Date: Tue, 20 Jun 2023 22:06:45 +0200 Subject: [PATCH 3/4] Add comment --- input/init.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/input/init.jl b/input/init.jl index 1b8d9e9..7f3467c 100644 --- a/input/init.jl +++ b/input/init.jl @@ -20,7 +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 + ξ::Float32 = 0.1 # Price padding factor """Derived Properties""" A::Array{Float32,1} = zeros(1) # Action space From 117af6222b8592b0b555afcad1d54b486fbbcee7 Mon Sep 17 00:00:00 2001 From: Jeremiah <4462211+jeremiahpslewis@users.noreply.github.com> Date: Tue, 20 Jun 2023 22:44:03 +0200 Subject: [PATCH 4/4] Fix conversion metric --- input/qlearning.jl | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/input/qlearning.jl b/input/qlearning.jl index f1b1378..13e9b71 100644 --- a/input/qlearning.jl +++ b/input/qlearning.jl @@ -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]]; @@ -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; @@ -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)