Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
569cdac
Create .gitkeep
bergio13 Jul 10, 2025
438d861
Add files via upload
bergio13 Jul 10, 2025
d024f37
Add files via upload
bergio13 Jul 10, 2025
906a6c7
Delete examples/rl/.gitkeep
bergio13 Jul 10, 2025
6f5ab24
Implement ReinforcementLearningABM and integrate with POMDPs/Crux in…
bergio13 Jul 22, 2025
6463646
ignore log
bergio13 Jul 22, 2025
ece4017
fix wolfsheep
bergio13 Jul 24, 2025
fd5d309
fix indexing, stepping with policies and training config
bergio13 Aug 7, 2025
c1b69d9
fix wolfsheep
bergio13 Aug 12, 2025
92d8bc6
add scheduler
bergio13 Aug 12, 2025
af6bdce
add scheduler
bergio13 Aug 12, 2025
0687f2c
add discount rates to rl config
bergio13 Aug 14, 2025
9d9c9ee
add plot + start refactoring as extension
bergio13 Aug 16, 2025
b839b77
refactor in extension and fix plotting
bergio13 Aug 16, 2025
93678f3
Use less restrictive versions of libs
Tortar Aug 16, 2025
c318539
Merge branch 'main' into gsoc
Tortar Aug 16, 2025
2da0515
remove piracy
Tortar Aug 17, 2025
5f05a06
add documentation for rl functions + refactor rl code
bergio13 Aug 17, 2025
5327055
fix
bergio13 Aug 17, 2025
5fc5755
fix
bergio13 Aug 17, 2025
864302e
fix docstring
bergio13 Aug 17, 2025
d43fd04
fix exports
bergio13 Aug 17, 2025
186c9c7
fix interface observation function
bergio13 Aug 18, 2025
1062488
delete old files and rename examples
bergio13 Aug 18, 2025
b1e37e8
delete old files
bergio13 Aug 18, 2025
e600392
add tests for rlabm
bergio13 Aug 18, 2025
5fc8d06
create tutorial + fix training wolfsheep + add deps for tests
bergio13 Aug 20, 2025
bfa27a7
fix tests
bergio13 Aug 20, 2025
7625bd2
fix makie version
bergio13 Aug 20, 2025
b918301
Update Project.toml
Tortar Aug 20, 2025
b8a9a4f
Update Project.toml
Tortar Aug 20, 2025
1fde568
remove debug prints
bergio13 Aug 21, 2025
0ee83ea
edit api docs
bergio13 Aug 21, 2025
30d5e6d
edit docs
bergio13 Aug 21, 2025
3eeec19
Update Agents.jl
Tortar Aug 21, 2025
2fb36da
Update rl_boltzmann.jl
Tortar Aug 21, 2025
f7df9ce
fix
Tortar Aug 21, 2025
9657ec4
improve boltzmann tutorial + fixes
bergio13 Aug 21, 2025
33bb8b2
refactor observation_radius + fixes
bergio13 Aug 21, 2025
3a638d2
fix example
bergio13 Aug 21, 2025
ac20b28
fix tutorial
bergio13 Aug 21, 2025
bf86bd0
add tests for extension + improve docs for RLABM
bergio13 Aug 22, 2025
c3c984b
fix tests
bergio13 Aug 22, 2025
f1bf7f6
Update examples/rl_boltzmann.jl
Tortar Aug 22, 2025
1b367d0
Update examples/rl_boltzmann.jl
Tortar Aug 22, 2025
de7a081
Update examples/rl_boltzmann.jl
Tortar Aug 22, 2025
73a7c49
Update examples/rl_boltzmann.jl
Tortar Aug 22, 2025
d336fd0
Update examples/rl_boltzmann.jl
Tortar Aug 22, 2025
c37b554
Update examples/rl_boltzmann.jl
Tortar Aug 22, 2025
aa0222a
update example
Tortar Aug 22, 2025
47a3240
Update Project.toml
Tortar Aug 22, 2025
988821f
fix stepping
Tortar Aug 23, 2025
2e21eca
Update rl_boltzmann.jl
Tortar Aug 23, 2025
4b9edd1
Update rl_boltzmann.jl
Tortar Aug 23, 2025
19b85bb
Update rl_boltzmann.jl
Tortar Aug 23, 2025
4eb1985
Update ext/AgentsVisualizations/src/interaction.jl
bergio13 Aug 26, 2025
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
112 changes: 112 additions & 0 deletions examples/rl/boltzmann.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using Agents, Random, CairoMakie

## 1. Agent Definition
@agent struct BoltzmannAgent(GridAgent{2})
wealth::Int
end

## 2. Gini Coefficient Calculation Function
function gini(wealths::Vector{Int})
n = length(wealths)
if n <= 1
return 0.0
end
sorted_wealths = sort(wealths)
sum_wi = sum(sorted_wealths)
if sum_wi == 0
return 0.0
end
numerator = sum((2i - n - 1) * w for (i, w) in enumerate(sorted_wealths))
denominator = n * sum_wi
return numerator / denominator
end

## 3. Model Properties
function boltzmann_money_model(; num_agents=100, dims=(10, 10), seed=1234, initial_wealth=1)
space = GridSpace(dims; periodic=true)
rng = MersenneTwister(seed)
properties = Dict(
:gini_coefficient => 0.0, # Initialize gini_coefficient
)

model = StandardABM(BoltzmannAgent, space;
(agent_step!)=boltz_step!,
(model_step!)=boltz_model_step!,
rng,
scheduler=Schedulers.Randomly(),
properties=properties
)

for _ in 1:num_agents
add_agent_single!(BoltzmannAgent, model, rand(1:initial_wealth))
end
return model
end

## 4. Agent Step Function
function boltz_step!(agent::BoltzmannAgent, model::ABM)
nearby_neighbors = collect(nearby_positions(agent.pos, model))
if !isempty(nearby_neighbors)
move_agent!(agent, rand(nearby_neighbors), model)
end

if agent.wealth > 0
other_agents_in_cell = [a for a in agents_in_position(agent.pos, model) if a.id != agent.id]
if !isempty(other_agents_in_cell)
other_agent = rand(other_agents_in_cell)
agent.wealth -= 1
other_agent.wealth += 1
end
end
end

## 5. Model Step Function
function boltz_model_step!(model::ABM)
wealths = [agent.wealth for agent in allagents(model)]
model.gini_coefficient = gini(wealths)
end


## 6. Example Simulation and Analysis
model = boltzmann_money_model(num_agents=5, dims=(10, 10), initial_wealth=10)
figure, _ = abmplot(model;
agent_color=a -> a.wealth,
agent_size=a -> 8 + a.wealth * 0.5,
title="Boltzmann Money Model - Final State (Post-Simulation)"
)
figure

adata = [:wealth]
mdata = [:gini_coefficient]

iterations = 200
data, mdata_df = run!(model, iterations; adata, mdata)
data.wealth


figure, _ = abmplot(model;
agent_color=a -> a.wealth,
agent_size=a -> 8 + a.wealth * 0.5,
title="Boltzmann Money Model - Final State (Post-Simulation)"
)
figure

abmvideo(
"boltz_model.mp4",
model;
frames=iterations,
framerate=5,
title="Boltzmann Money Model Simulation",
agent_color=a -> a.wealth,
agent_size=a -> 8 + a.wealth * 0.5,
)

# Plot Gini Coefficient over time
function plot_gini(mdata_df)
figure = Figure()
ax = Axis(figure[1, 1], title="Gini Coefficient Over Time")
lines!(ax, 1:iterations+1, mdata_df.gini_coefficient,
color=:blue, linewidth=2, label="Gini Coefficient")
return figure
end
figure_gini = plot_gini(mdata_df)
Loading
Loading