From a73d25af40b25efc631f220e48e06613062bb0ee Mon Sep 17 00:00:00 2001 From: narawat lamaiin Date: Sun, 28 Apr 2024 12:39:16 +0700 Subject: [PATCH] update --- src/interface.jl | 8 +- src/mcts.jl | 332 ++++++++++++++++++++--------------------------- src/util.jl | 4 +- test/runtest.jl | 4 - 4 files changed, 148 insertions(+), 200 deletions(-) diff --git a/src/interface.jl b/src/interface.jl index f236193..f873e4f 100644 --- a/src/interface.jl +++ b/src/interface.jl @@ -51,7 +51,7 @@ julia> # TODO [] update docstring - [x] implement the function + [PENDING] implement the function [] implement RAG to pull similar experience [] use iterative prompting to ensure JSON format @@ -143,8 +143,8 @@ function decisionMaker(a::T1, state::T2) where {T1<:agent, T2<:AbstractDict} ) ) - thought = GeneralUtils.sendReceiveMqttMsg(outgoingMsg) - + result = GeneralUtils.sendReceiveMqttMsg(outgoingMsg) + thought = result[:response][:text] return thought end @@ -288,7 +288,7 @@ end function conversation(a::T, userinput::Dict) where {T<:agent} """ [] update document - [x] MCTS() for planning + [PENDING] MCTS() for planning """ # "newtopic" command to delete chat history if userinput[:text] == "newtopic" diff --git a/src/mcts.jl b/src/mcts.jl index a9b6705..9bcb67c 100644 --- a/src/mcts.jl +++ b/src/mcts.jl @@ -15,43 +15,38 @@ using ..type """ a node for MCTS search tree - Arguments\n - ----- - state::T - a state of a game. Can be a Dict or something else. - For example: - state = Dict( - :info=> Dict(), # keyword info - :thoughtHistory=> Dict( - :question=> _, - :thought_1=> _, - :action_1=> _, - :observation_1=> _, - :thought_2=> _, - ... - ) - ) - visits::Integer - number of time the game visits this state - stateValue::Float64 - state value +# Arguments + - `state::T` + a state of a game. Can be a Dict or something else. + For example: + state = Dict( + :info=> Dict(), # keyword info + :thoughtHistory=> Dict( + :question=> _, + :thought_1=> _, + :action_1=> _, + :observation_1=> _, + :thought_2=> _, + ... + ) + ) + - `visits::Integer ` + number of time the game visits this state + - `stateValue::Float64` + state value - Return\n - ----- +# Return - Example\n - ----- - ```jldoctest - julia> - ``` +# Example +```jldoctest +julia> +``` - TODO\n - ----- - [] update docstring - [DONE] implement the function +# TODO + [] update docstring + [x] implement the function - Signature\n - ----- +# Signature """ struct MCTSNode{T<:AbstractDict} state::T @@ -62,28 +57,23 @@ end """ Select a node based on UCT score - Arguments\n - ----- - node::MCTSNode - mcts node - w::Float64 - exploration weight - Return\n - ----- +# Arguments + - `node::MCTSNode` + mcts node + - `w::Float64` + exploration weight +# Return - Example\n - ----- - ```jldoctest - julia> - ``` +# Example +```jldoctest +julia> +``` - TODO\n - ----- - [] update docstring - [DONE] check childNode.total_reward w/ LATS paper. Which value total_reward representing +# TODO + [] update docstring + [DONE] check childNode.total_reward w/ LATS paper. Which value total_reward representing - Signature\n - ----- +# Signature """ function select(node::MCTSNode, w::Float64) max_uct = -Inf @@ -103,31 +93,26 @@ end """ Expand selected node - Arguments\n - ----- - node::MCTSNode - MCTS node - state::T - a state of a game. Can be a Dict or something else. - decisionMaker::Function - +# Arguments + - `node::MCTSNode` + MCTS node + - `state::T` + a state of a game. Can be a Dict or something else. + - `decisionMaker::Function` - Return\n - ----- + +# Return - Example\n - ----- - ```jldoctest - julia> - ``` +# Example +```jldoctest +julia> +``` - TODO\n - ----- - [] update docstring - [x] implement the function +# TODO + - [] update docstring + - [WORKING] implement the function - Signature\n - ----- +# Signature """ function expand(a::T1, node::MCTSNode, state::T2, decisionMaker::Function, stateValueEstimator::Function; n::Integer=3) where {T1<:agent, T2<:AbstractDict} @@ -149,26 +134,21 @@ end """ - Arguments\n - ----- - - Return\n - ----- +# Arguments + +# Return - Example\n - ----- - ```jldoctest - julia> - ``` +# Example +```jldoctest +julia> +``` - TODO\n - ----- - [] update docstring - [] implement the function - [] reward only comes at terminal state +# TODO + - [] update docstring + - [] implement the function + - [] reward only comes at terminal state - Signature\n - ----- +# Signature """ function simulate(state::T, max_depth::Int) where {T<:AbstractDict} total_reward = 0.0 @@ -186,25 +166,20 @@ end """ - Arguments\n - ----- - - Return\n - ----- +# Arguments + +# Return - Example\n - ----- - ```jldoctest - julia> - ``` +# Example +```jldoctest +julia> +``` - TODO\n - ----- - [] update docstring - [] implement the function +# TODO + - [] update docstring + - [] implement the function - Signature\n - ----- +# Signature """ function backpropagate(node::MCTSNode, reward::Float64) node.visits += 1 @@ -219,79 +194,61 @@ end """ - Arguments\n - ----- - - Return\n - ----- +# Arguments + +# Return - Example\n - ----- - ```jldoctest - julia> - ``` +# Example +```jldoctest +julia> +``` - TODO\n - ----- - [] update docstring - [] implement the function +# TODO + - [] update docstring + - [] implement the function - Signature\n - ----- +# Signature """ function transition(state, action) end -""" Check whether a node is a leaf node of a tree +""" - Arguments\n - ----- - node::MCTSNode - node of a tree - - Return\n - ----- - result::Bool - true if the node is a leaf node of a tree otherwise false +# Arguments + +# Return - Example\n - ----- - ```jldoctest - julia> using - ``` +# Example +```jldoctest +julia> +``` - TODO\n - ----- - [] update docstring +# TODO + - [] update docstring + - [x] implement the function - Signature\n - ----- +# Signature """ isleaf(node::MCTSNode)::Bool = isempty(node.children) """ - Arguments\n - ----- - - Return\n - ----- +# Arguments + +# Return - Example\n - ----- - ```jldoctest - julia> - ``` +# Example +```jldoctest +julia> +``` - TODO\n - ----- - [] update docstring - [] implement the function +# TODO + - [] update docstring + - [] implement the function - Signature\n - ----- +# Signature """ function executeLLMFunction() @@ -303,42 +260,37 @@ end # ------------------------------------------------------------------------------------------------ # """ Search the best action to take for a given state and task - Arguments\n - ----- - a::agent - one of Yiem's agents +# Arguments + - `a::agent` + one of Yiem's agents + - `initial state` initial state - initial state - decisionMaker::Function - decide what action to take - stateValueEstimator::Function - assess the value of the state - reflector::Function - generate lesson from trajectory and reward - isterminal::Function - determine whether a given state is a terminal state - n::Integer - how many times action will be sampled from decisionMaker - w::Float64 - exploration weight - - Return\n - ----- - plan::Vector{Dict} - best plan + - `decisionMaker::Function` + decide what action to take + - `stateValueEstimator::Function` + assess the value of the state + - `reflector::Function` + generate lesson from trajectory and reward + - `isterminal::Function` + determine whether a given state is a terminal state + - `n::Integer` + how many times action will be sampled from decisionMaker + - `w::Float64` + exploration weight + +# Return + - `plan::Vector{Dict}` + best plan - Example\n - ----- - ```jldoctest - julia> - ``` +# Example +```jldoctest +julia> +``` - TODO\n - ----- - [] update docstring +# TODO + [] update docstring - Signature\n - ----- +# Signature """ function runMCTS( a::T1, diff --git a/src/util.jl b/src/util.jl index 8f736ae..a707efa 100644 --- a/src/util.jl +++ b/src/util.jl @@ -293,7 +293,7 @@ end TODO\n ----- [] update docstring - [WORKING] implement the function + [PENDING] implement the function Signature\n ----- @@ -332,7 +332,7 @@ end TODO\n ----- [] update docstring - [WORKING] implement the function + [TESTING] implement the function Signature\n ----- diff --git a/test/runtest.jl b/test/runtest.jl index f33ae22..56759dd 100644 --- a/test/runtest.jl +++ b/test/runtest.jl @@ -100,10 +100,6 @@ response = YiemAgent.conversation(a, Dict(:text=> "Hello, I would like a get a b - - - -