Hello, ![]()
Solution to problem is : move_agent!(agent, new_position, model)
Use above line in agent_step() before moving the agent.
Check out this code:
@agent struct PoorSoul(ContinuousAgent{2, Float64})
mass::Float64
end
function handle_collision(agent, model)
x, y = agent.pos
vx, vy = agent.vel
if x ≤ 0.0
x = 0.0
vx < 0.0 && (vx = -vx)
end
if x ≥ model.max_bound
x = model.max_bound
vx > 0.0 && (vx = -vx)
end
if y ≤ 0.0
y = 0.0
vy < 0.0 && (vy = -vy)
end
if y ≥ model.max_bound
y = model.max_bound
vy > 0.0 && (vy = -vy)
end
return @SVector([x, y]), @SVector([vx, vy])
end
function initialize(;
speed = .002,
interaction_radius = 0.012,
dt = 1.0,
max_bound = 1,
n_agents,
)
properties = (;
interaction_radius,
dt,
max_bound,
)
space = ContinuousSpace((1, 1); spacing = 0.02, periodic = false)
model = StandardABM(
PoorSoul,
space;
agent_step!,
model_step!,
properties
)
# Add initial individuals
for _ ∈ 1:n_agents
pos = Tuple(rand(Uniform(0, max_bound), 2))
mass = 1
vel = sincos(2π * rand(abmrng(model))) .* speed
add_agent!(pos, model; vel, mass)
end
return model
end
function model_step!(model)
r = model.interaction_radius
for (a1, a2) in interacting_pairs(model, r, :nearest)
elastic_collision!(a1, a2, :mass)
end
end
function agent_step!(agent, model)
new_position, new_velocity = handle_collision(agent, model)
agent.vel = new_velocity
# this move_agent function changes agent to new position
# Before you were giving new value to position but agent position was not
#actually getting updated.
move_agent!(agent, new_position, model)
move_agent!(agent, model, model.dt)
end
Random.seed!(658)
model = initialize(; n_agents = 10, max_bound = .40)
run!(model, 40)
abmvideo(
“socialdist4.mp4”,
model;
title = “SIR model”,
frames = 300,
#agent_color = sir_colors,
agent_size = 10,
spf = 1,
framerate = 20
)