Skip to content

Instantly share code, notes, and snippets.

View pjpetersik's full-sized avatar
🍇

Paul Petersik pjpetersik

🍇
View GitHub Profile
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
using StatsBase
using Plots
mutable struct Agent
position::Vector{Real}
cohere_factor::Real
repel_factor::Real
energy::Real
desired_rank::Real
end
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
using Plots
model = Model(50)
@gif for t ∈ 1:150
step!(model)
postions = [ agent.position for agent in model.peloton ]
arr = hcat(postions...)
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
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
mutable struct Agent
position::Vector{Real}
cohere_factor::Real
repel_factor::Real
energy::Real
desired_rank::Real
end
struct Model
peloton::Vector{Agent}
using StatsBase
using Plots
nt = 500
nx = 200
ny = 200
# state
x = zeros(Int8,nx,ny,nt)