This commit is contained in:
narawat lamaiin
2024-08-22 10:59:26 +07:00
parent c33d69b00c
commit 856308555f

View File

@@ -196,72 +196,69 @@ end
# Return # Return
- None - None
# TODO
- [WORKING] implement multithreads
# Signature # Signature
""" """
function expand(node::MCTSNode, transition::Function, transitionargs::NamedTuple; # function expand(node::MCTSNode, transition::Function, transitionargs::NamedTuple;
totalsample::Integer=3)
# not use Any[] because I want to preserve result order
results = Vector{Any}(undef, totalsample)
@sync for i in 1:totalsample
@spawn begin
result = transition(deepcopy(node.state), deepcopy(transitionargs))
results[i] = result
end
end
for result in results
newNodeKey::AbstractString = result[:newNodeKey]
newstate::AbstractDict = result[:newstate]
progressvalue::Integer = result[:progressvalue]
"""
[] newNodeKey ∉ keys(node.children).
New state may have semantic vector close enought to
one of existing child state. Which can be assume that they are the same state
semantically-wise i.e. De javu. This could be used to recall lessons for this
similar situation to improve decisionMaker and evaluator.
"""
if newNodeKey keys(node.children)
node.children[newNodeKey] =
MCTSNode(newNodeKey, newstate, 0, progressvalue, 0, newstate[:reward],
newstate[:isterminal], node, Dict{String, MCTSNode}())
end
end
end
# function expand(node::MCTSNode,transition::Function, transitionargs::NamedTuple;
# totalsample::Integer=3) # totalsample::Integer=3)
# nthSample = 0 # # not use Any[] because I want to preserve result order
# while true # results = Vector{Any}(undef, totalsample)
# nthSample += 1
# if nthSample <= totalsample
# result = transition(node.state, transitionargs)
# newNodeKey::AbstractString = result[:newNodeKey]
# newstate::AbstractDict = result[:newstate]
# progressvalue::Integer = result[:progressvalue]
# """ # @sync for i in 1:totalsample
# [] newNodeKey ∉ keys(node.children). # @spawn begin
# New state may have semantic vector close enought to # result = transition(deepcopy(node.state), deepcopy(transitionargs))
# one of existing child state. Which can be assume that they are the same state # results[i] = result
# semantically-wise i.e. De javu. This could be used to recall lessons for this
# similar situation to improve decisionMaker and evaluator.
# """
# if newNodeKey ∉ keys(node.children)
# node.children[newNodeKey] =
# MCTSNode(newNodeKey, newstate, 0, progressvalue, 0, newstate[:reward],
# newstate[:isterminal], node, Dict{String, MCTSNode}())
# end # end
# else # end
# break
# for result in results
# newNodeKey::AbstractString = result[:newNodeKey]
# newstate::AbstractDict = result[:newstate]
# progressvalue::Integer = result[:progressvalue]
# """
# [] newNodeKey ∉ keys(node.children).
# New state may have semantic vector close enought to
# one of existing child state. Which can be assume that they are the same state
# semantically-wise i.e. De javu. This could be used to recall lessons for this
# similar situation to improve decisionMaker and evaluator.
# """
# if newNodeKey ∉ keys(node.children)
# node.children[newNodeKey] =
# MCTSNode(newNodeKey, newstate, 0, progressvalue, 0, newstate[:reward],
# newstate[:isterminal], node, Dict{String, MCTSNode}())
# end # end
# end # end
# end # end
function expand(node::MCTSNode,transition::Function, transitionargs::NamedTuple;
totalsample::Integer=3)
nthSample = 0
while true
nthSample += 1
if nthSample <= totalsample
result = transition(node.state, transitionargs)
newNodeKey::AbstractString = result[:newNodeKey]
newstate::AbstractDict = result[:newstate]
progressvalue::Integer = result[:progressvalue]
"""
[] newNodeKey ∉ keys(node.children).
New state may have semantic vector close enought to
one of existing child state. Which can be assume that they are the same state
semantically-wise i.e. De javu. This could be used to recall lessons for this
similar situation to improve decisionMaker and evaluator.
"""
if newNodeKey keys(node.children)
node.children[newNodeKey] =
MCTSNode(newNodeKey, newstate, 0, progressvalue, 0, newstate[:reward],
newstate[:isterminal], node, Dict{String, MCTSNode}())
end
else
break
end
end
end
""" Simulate interactions between agent and environment """ Simulate interactions between agent and environment