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 | |
| using Plots | |
| nt = 500 | |
| nx = 200 | |
| ny = 200 | |
| # state | |
| x = zeros(Int8,nx,ny,nt) |
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
| mutable struct Agent | |
| position::Vector{Real} | |
| cohere_factor::Real | |
| repel_factor::Real | |
| energy::Real | |
| desired_rank::Real | |
| end | |
| struct Model | |
| peloton::Vector{Agent} |
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 |
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
| function energy_usage(agent::Agent, model::Model) | |
| usage = 0.02 | |
| for other_agent in model.peloton | |
| heading = other_agent.position - agent.position | |
| distance = sum((heading).^2)^0.5 | |
| if agent != other_agent | |
| if distance < 0.2 && heading[1] > 0 | |
| angle_factor = abs(acot(heading[2] / heading[1])) / π * 2 |
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 Plots | |
| model = Model(50) | |
| @gif for t ∈ 1:150 | |
| step!(model) | |
| postions = [ agent.position for agent in model.peloton ] | |
| arr = hcat(postions...) | |
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
| 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 |
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 | |
| using Plots | |
| mutable struct Agent | |
| position::Vector{Real} | |
| cohere_factor::Real | |
| repel_factor::Real | |
| energy::Real | |
| desired_rank::Real | |
| end |
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
| import epipack as epk | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| from scipy.interpolate import interp1d | |
| def create_growth_rate_function(r, K): | |
| def growth_rate(t, y): | |
| N = np.sum(y) | |
| return r * N * (1 - N / K) | |
| return growth_rate |