Skip to content

Instantly share code, notes, and snippets.

@pjpetersik
Last active April 6, 2022 11:15
Show Gist options
  • Save pjpetersik/3c443ed66948cb8f35f0dcad538d310e to your computer and use it in GitHub Desktop.
Save pjpetersik/3c443ed66948cb8f35f0dcad538d310e to your computer and use it in GitHub Desktop.
function update_desired_rank!(agent::Agent, position_mean)
if rand() > agent.energy
agent.desired_rank = agent.desired_rank - 0.01 * rand()
else
if rand() > 0.99
agent.desired_rank = agent.desired_rank + max(0, (0.5 - position_mean[1])) * rand()
end
end
end
function step!(model::Model)
postions = [ agent.position for agent in model.peloton ]
postion_mean = mean(postions)
for agent in model.peloton
# random movement
random_move = 0.001 *(rand(2) .- 0.5)
# bias towards center of the road
bias = [0 ; 0.05 * (model.road_center - agent.position[2])]
# bias to desired rank
bias[1] = 0.05 * (agent.desired_rank - agent.position[1])
# flocking algorithm
velocity = flocking(agent, model) + random_move + bias
agent.position += velocity
# energy component
agent.energy -= energy_usage(agent, model)
agent.energy = max(0, min(1, agent.energy))
update_desired_rank!(agent, position_mean)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment