Last active
April 6, 2022 10:26
-
-
Save pjpetersik/d1a96ba55a5470e9a14ffcead038a930 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using StatsBase | |
| function flocking(agent::Agent, model::Model) | |
| N = 0 | |
| repel = [0.0; 0.0] | |
| cohere = [0.0; 0.0] | |
| for other_agent in model.peloton | |
| if agent != other_agent | |
| N += 1 | |
| heading = other_agent.position - agent.position | |
| distance = sum((heading).^2)^0.5 | |
| cohere += heading | |
| if distance < 0.1 && heading[1] > 0 | |
| angle_factor = abs(acot(heading[2] / heading[1])) / π * 2 | |
| repel -= heading ./ distance^2 * angle_factor | |
| end | |
| end | |
| end | |
| velocity = agent.cohere_factor * cohere / N + agent.repel_factor * repel / N | |
| return velocity | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment