Skip to content
Closed

Dev #57

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/CI_full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ jobs:
arch:
- x64
steps:
- uses: actions/checkout@v4
- uses: julia-actions/setup-julia@v2
- uses: actions/checkout@v6
- uses: julia-actions/setup-julia@v3
with:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
- uses: julia-actions/cache@v2
- uses: julia-actions/cache@v3
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-runtest@v1
- uses: julia-actions/julia-processcoverage@v1
- uses: codecov/codecov-action@v5
- uses: codecov/codecov-action@v6
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: lcov.info
8 changes: 4 additions & 4 deletions .github/workflows/CI_small.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ jobs:
arch:
- x64
steps:
- uses: actions/checkout@v4
- uses: julia-actions/setup-julia@v2
- uses: actions/checkout@v6
- uses: julia-actions/setup-julia@v3
with:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
- uses: julia-actions/cache@v2
- uses: julia-actions/cache@v3
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-runtest@v1
- uses: julia-actions/julia-processcoverage@v1
- uses: codecov/codecov-action@v5
- uses: codecov/codecov-action@v6
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: lcov.info
4 changes: 2 additions & 2 deletions .github/workflows/Documenter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ jobs:
name: Documentation
runs-on: macOS-latest
steps:
- uses: actions/checkout@v4
- uses: julia-actions/setup-julia@v2
- uses: actions/checkout@v6
- uses: julia-actions/setup-julia@v3
with:
version: '1'
- uses: julia-actions/julia-buildpkg@v1
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,9 @@ infer_policies!(aif)
sample_action!(aif)

````

## Contributors
- [@jonathan7773](https://github.com/Jonathan7773) – Developer
- [@samuelnehrer02](https://github.com/samuelnehrer02) – Developer
- [@PTWaade](https://github.com/PTWaade) – Developer
- [@John-Boik](https://github.com/John-Boik) – Developer
109 changes: 21 additions & 88 deletions src/pomdp/POMDP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ function action_pomdp!(agent::Agent, obs::Vector{Int64})
action_distribution = Vector{Distributions.Categorical}(undef, n_factors)

#If there was a previous action
if !ismissing(agent.states["action"])
if !isempty(agent.substruct.states.action)

#Extract it
previous_action = agent.states["action"]
previous_action = agent.states.action

# If it is not a vector, make it one
if !(previous_action isa Vector)
Expand All @@ -45,19 +45,18 @@ function action_pomdp!(agent::Agent, obs::Vector{Int64})
infer_states!(agent.substruct, obs)

# If action is empty, update D vectors
if ismissing(agent.states["action"]) && !isnothing(agent.substruct.parameters.pD)
update_D!(agent.substruct)
if isempty(agent.substruct.states.action) && !isnothing(agent.substruct.parameters.pD)
update_D(agent.substruct)
end

# If learning of the B matrix is enabled and agent has a previous action
if !ismissing(agent.states["action"]) && !isnothing(agent.substruct.parameters.pB)
# Update Transition Matrix
update_B!(agent.substruct)
if !isempty(agent.substruct.states.action) && !isnothing(agent.substruct.parameters.pB)
update_B(agent.substruct)
end

# If learning of the A matrix is enabled
if !isnothing(agent.substruct.parameters.pA)
update_A!(agent.substruct)
update_A(agent.substruct)
end

# Run policy inference
Expand All @@ -83,18 +82,18 @@ function action_pomdp!(agent::Agent, obs::Tuple{Vararg{Int}})
obs = collect(obs)

### Get parameters
alpha = agent.substruct.parameters["alpha"]
n_factors = length(agent.substruct.settings["num_controls"])
alpha = agent.substruct.parameters.alpha
n_factors = length(agent.substruct.settings._n_controls)

# Initialize empty arrays for action distribution per factor
action_p = Vector{Any}(undef, n_factors)
action_distribution = Vector{Distributions.Categorical}(undef, n_factors)

#If there was a previous action
if !ismissing(agent.states["action"])
#If there was a previous action
if !isempty(agent.substruct.states.action)

#Extract it
previous_action = agent.states["action"]
previous_action = agent.states.action

# If it is not a vector, make it one
if !(previous_action isa Vector)
Expand All @@ -111,24 +110,19 @@ function action_pomdp!(agent::Agent, obs::Tuple{Vararg{Int}})
infer_states!(agent.substruct, obs)

# If action is empty and pD is not nothing, update D vectors
if ismissing(agent.states["action"]) && agent.substruct.pD !== nothing
qs_t1 = get_history(agent.substruct)["posterior_states"][1]
update_D!(agent.substruct, qs_t1)
if isempty(agent.substruct.states.action) && !isnothing(agent.substruct.parameters.pD)

update_D(agent.substruct)
end

# If learning of the B matrix is enabled and agent has a previous action
if !ismissing(agent.states["action"]) && agent.substruct.pB !== nothing

# Get the posterior over states from the previous time step
states_posterior = get_history(agent.substruct)["posterior_states"][end-1]

# Update Transition Matrix
update_B!(agent.substruct, states_posterior)
if !isempty(agent.substruct.states.action) && !isnothing(agent.substruct.parameters.pB)
update_B(agent.substruct)
end

# If learning of the A matrix is enabled
if agent.substruct.pA !== nothing
update_A!(agent.substruct, obs)
if !isnothing(agent.substruct.parameters.pA)
update_A(agent.substruct)
end

# Run policy inference
Expand Down Expand Up @@ -163,26 +157,17 @@ function action_pomdp!(aif::POMDPActiveInference, obs::Vector{Int64})
infer_states!(aif, obs)

# If action is empty, update D vectors
if isempty(get_states(aif, "action")) && !isnothing(aif.parameters.pD)
# qs_t1 = get_history(aif, "qs_current")[end]
# update_D(aif, qs_t1)
if isempty(aif.states.action) && !isnothing(aif.parameters.pD)
update_D(aif)
end

# If learning of the B matrix is enabled and agent has a previous action
if !isempty(get_states(aif, "action")) && !isnothing(aif.parameters.pB)

# Get the posterior over states from the previous time step
# states_posterior = get_history(aif, "qs_current")[end-1]

# Update Transition Matrix
# update_B(aif, states_posterior)
if !isempty(aif.states.action) && !isnothing(aif.parameters.pB)
update_B(aif)
end

# If learning of the A matrix is enabled
if !isnothing(aif.parameters.pA)
# update_A(aif, obs)
update_A(aif)
end

Expand All @@ -202,58 +187,6 @@ function action_pomdp!(aif::POMDPActiveInference, obs::Vector{Int64})
return n_factors == 1 ? action_distribution[1] : action_distribution
end

# function action_pomdp!(aif::AIF, obs::Vector{Int64})

# ### Get parameters
# alpha = aif.parameters["alpha"]
# n_factors = length(aif.settings["num_controls"])

# # Initialize empty arrays for action distribution per factor
# action_p = Vector{Any}(undef, n_factors)
# action_distribution = Vector{Distributions.Categorical}(undef, n_factors)

# ### Infer states & policies

# # Run state inference
# infer_states!(aif, obs)

# # If action is empty, update D vectors
# if ismissing(get_states(aif)["action"]) && aif.pD !== nothing
# qs_t1 = get_history(aif)["posterior_states"][1]
# update_D!(aif, qs_t1)
# end

# # If learning of the B matrix is enabled and agent has a previous action
# if !ismissing(get_states(aif)["action"]) && aif.pB !== nothing

# # Get the posterior over states from the previous time step
# states_posterior = get_history(aif)["posterior_states"][end-1]

# # Update Transition Matrix
# update_B!(aif, states_posterior)
# end

# # If learning of the A matrix is enabled
# if aif.pA !== nothing
# update_A!(aif, obs)
# end

# # Run policy inference
# infer_policies!(aif)


# ### Retrieve log marginal probabilities of actions
# log_action_marginals = get_log_action_marginals(aif)

# ### Pass action marginals through softmax function to get action probabilities
# for factor in 1:n_factors
# action_p[factor] = softmax(log_action_marginals[factor] * alpha, dims=1)
# action_distribution[factor] = Distributions.Categorical(action_p[factor])
# end

# return n_factors == 1 ? action_distribution[1] : action_distribution
# end

function action_pomdp!(agent::Agent, obs::Int64)
action_pomdp!(agent::Agent, [obs])
end
Loading