This commit is contained in:
narawat lamaiin
2024-08-18 19:15:55 +07:00
parent 32dec4b47c
commit 997cc904dd

View File

@@ -2,7 +2,7 @@ module mcts
export selectBestNextNode, selectBestTrajectoryNode, backpropagate, isleaf, isroot, selectChildNode, export selectBestNextNode, selectBestTrajectoryNode, backpropagate, isleaf, isroot, selectChildNode,
expand, simulate, makeNewState expand, simulate, makeNewState
using Base.Threads
using GeneralUtils using GeneralUtils
using ..type using ..type
@@ -196,37 +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) totalsample::Integer=3)
results = Any[]
nthSample = 0 @sync for i in 1:totalsample
while true @spawn begin
nthSample += 1 results[i] = transition(deepcopy(node.state), deepcopy(transitionargs))
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 end
else println("--> sampling $i")
break 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 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