This commit is contained in:
2025-03-11 00:13:25 +07:00
parent b1d655acff
commit 097484675c
2 changed files with 62 additions and 54 deletions

View File

@@ -85,10 +85,11 @@ function runMCTS(
# do nothing then go directly to backpropagation. It means the end of this iteration
backpropagate(node, node.reward)
else
println(111)
_ = expand(node, transition, transitionargs;
horizontalSample=horizontalSampleExpansionPhase)
println(555)
println(666)
@sync for (leafNodeKey, leafNode) in node.children
@spawn simulateThenBackpropagate(leafNode, transition, transitionargs;
@@ -96,6 +97,14 @@ function runMCTS(
horizontalSampleSimulationPhase=horizontalSampleSimulationPhase,
saveSimulatedNode=saveSimulatedNode)
end
#CHANGE for testing
# for (leafNodeKey, leafNode) in node.children
# simulateThenBackpropagate(leafNode, transition, transitionargs;
# maxSimulationDepth=maxSimulationDepth,
# horizontalSampleSimulationPhase=horizontalSampleSimulationPhase,
# saveSimulatedNode=saveSimulatedNode)
# end
end
# stop if the early stop condition is met

View File

@@ -198,20 +198,26 @@ end
# Signature
"""
function expand(node::MCTSNode,transition::Function, transitionargs::NamedTuple;
horizontalSample::Integer=3)
@sync for i in 1:horizontalSample
@spawn _expand(node, transition, transitionargs)
end
#CHANGE for testing
# for i in 1:horizontalSample
# _expand(node, transition, transitionargs)
# end
end
# function expand(node::MCTSNode,transition::Function, transitionargs::NamedTuple;
# totalsample::Integer=3)
# horizontalSample::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
# nthSample = 0
# listOfNewNodeId = []
# while true
# nthSample += 1
# if nthSample <= horizontalSample
# result = transition(node.state, transitionargs)
# newNodeKey::AbstractString = result[:newNodeKey]
# newstate::AbstractDict = result[:newstate]
# progressvalue::Integer = result[:progressvalue]
@@ -224,20 +230,18 @@ end
# 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}())
# push!(listOfNewNodeId, newNodeKey)
# newNode = MCTSNode(newNodeKey, newstate, 0, progressvalue, 0, newstate[:reward],
# newstate[:isterminal], node, Dict{String, MCTSNode}(), Dict{Symbol, Any}())
# node.children[newNodeKey] = newNode
# end
# else
# return listOfNewNodeId
# end
# end
# end
function expand(node::MCTSNode,transition::Function, transitionargs::NamedTuple;
horizontalSample::Integer=3)
nthSample = 0
listOfNewNodeId = []
while true
nthSample += 1
if nthSample <= horizontalSample
function _expand(node::MCTSNode,transition::Function, transitionargs::NamedTuple)
result = transition(node.state, transitionargs)
newNodeKey::AbstractString = result[:newNodeKey]
newstate::AbstractDict = result[:newstate]
@@ -251,15 +255,10 @@ function expand(node::MCTSNode,transition::Function, transitionargs::NamedTuple;
similar situation to improve decisionMaker and evaluator.
"""
if newNodeKey keys(node.children)
push!(listOfNewNodeId, newNodeKey)
newNode = MCTSNode(newNodeKey, newstate, 0, progressvalue, 0, newstate[:reward],
newstate[:isterminal], node, Dict{String, MCTSNode}(), Dict{Symbol, Any}())
node.children[newNodeKey] = newNode
end
else
return listOfNewNodeId
end
end
end