Skip to content

Commit

Permalink
Simplify signatures (#916)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tortar authored Nov 9, 2023
1 parent ba624d0 commit fa03585
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 33 deletions.
10 changes: 5 additions & 5 deletions src/core/model_standard.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ agenttype(::StandardABM{S,A}) where {S,A} = A
nextid(model::DictStandardABM) = getfield(model, :maxid)[] + 1
nextid(model::VecStandardABM) = nagents(model) + 1

function add_agent_to_model!(agent::A, model::DictStandardABM) where {A<:AbstractAgent}
function add_agent_to_model!(agent::AbstractAgent, model::DictStandardABM)
if haskey(agent_container(model), agent.id)
error("Can't add agent to model. There is already an agent with id=$(agent.id)")
else
Expand All @@ -148,19 +148,19 @@ function add_agent_to_model!(agent::A, model::DictStandardABM) where {A<:Abstrac
return
end

function add_agent_to_model!(agent::A, model::VecStandardABM) where {A<:AbstractAgent}
function add_agent_to_model!(agent::AbstractAgent, model::VecStandardABM)
agent.id != nagents(model) + 1 && error("Cannot add agent of ID $(agent.id) in a vector ABM of $(nagents(model)) agents. Expected ID == $(nagents(model)+1).")
push!(agent_container(model), agent)
return
end

function remove_agent_from_model!(agent::A, model::DictStandardABM) where {A<:AbstractAgent}
function remove_agent_from_model!(agent::AbstractAgent, model::DictStandardABM)
delete!(agent_container(model), agent.id)
return
end

function remove_agent_from_model!(agent::A, model::VecStandardABM) where {A<:AbstractAgent}
error("Cannot remove agents in a `StandardABM` with a vector container.")
function remove_agent_from_model!(agent::AbstractAgent, model::VecStandardABM)
error("Cannot remove agents in `UnremovableABM`.")
end

random_id(model::StandardABM) = rand(abmrng(model), agent_container(model)).first
Expand Down
4 changes: 2 additions & 2 deletions src/core/space_interaction_API.jl
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ end
Add the agent to the `model` at the agent's own position.
"""
function add_agent_pos!(agent::A, model::ABM) where {A<:AbstractAgent}
function add_agent_pos!(agent::AbstractAgent, model::ABM)
add_agent_to_model!(agent, model)
add_agent_to_space!(agent, model)
return agent
Expand Down Expand Up @@ -280,7 +280,7 @@ end
Same as `nearby_ids(agent.pos, model, r)` but the iterable *excludes* the given
`agent`'s id.
"""
function nearby_ids(agent::A, model::ABM, r = 1; kwargs...) where {A<:AbstractAgent}
function nearby_ids(agent::AbstractAgent, model::ABM, r = 1; kwargs...)
all = nearby_ids(agent.pos, model, r; kwargs...)
Iterators.filter(i -> i agent.id, all)
end
Expand Down
2 changes: 1 addition & 1 deletion src/spaces/continuous.jl
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ function nearby_ids_exact(pos::ValidPos, model::ABM{<:ContinuousSpace{D,A,T}}, r
end

# Do the standard extensions for `_exact` as in space API
function nearby_ids_exact(agent::A, model::ABM, r = 1) where {A<:AbstractAgent}
function nearby_ids_exact(agent::AbstractAgent, model::ABM, r = 1)
all = nearby_ids_exact(agent.pos, model, r)
Iterators.filter(i -> i agent.id, all)
end
Expand Down
6 changes: 2 additions & 4 deletions src/spaces/discrete.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,15 @@ npositions(model::ABM) = npositions(abmspace(model))
Return the ids of agents in the position corresponding to `position` or position
of `agent`.
"""
ids_in_position(agent::A, model) where {A<:AbstractAgent} =
ids_in_position(agent.pos, model)
ids_in_position(agent::AbstractAgent, model) = ids_in_position(agent.pos, model)

"""
agents_in_position(position, model::ABM{<:DiscreteSpace})
agents_in_position(agent, model::ABM{<:DiscreteSpace})
Return an iterable of the agents in `position``, or in the position of `agent`.
"""
agents_in_position(agent::A, model) where {A<:AbstractAgent} =
agents_in_position(agent.pos, model)
agents_in_position(agent::AbstractAgent, model) = agents_in_position(agent.pos, model)
agents_in_position(pos, model) = (model[id] for id in ids_in_position(pos, model))

"""
Expand Down
10 changes: 4 additions & 6 deletions src/spaces/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ sense: currently `AbstractGridSpace` and `ContinuousSpace`.
Example usage in the [Flocking model](@ref).
"""
euclidean_distance(a::A, b::B, model::ABM,
) where {A <: AbstractAgent,B <: AbstractAgent} = euclidean_distance(a.pos, b.pos, abmspace(model))

euclidean_distance(a::AbstractAgent, b::AbstractAgent, model::ABM) =
euclidean_distance(a.pos, b.pos, abmspace(model))
euclidean_distance(p1, p2, model::ABM) = euclidean_distance(p1, p2, abmspace(model))

function euclidean_distance(
Expand Down Expand Up @@ -75,9 +74,8 @@ Return the manhattan distance between `a` and `b` (either agents or agent positi
respecting periodic boundary conditions (if in use). Works with any space where it makes
sense: currently `AbstractGridSpace` and `ContinuousSpace`.
"""
manhattan_distance(a::A, b::B, model::ABM
) where {A <: AbstractAgent,B <: AbstractAgent} = manhattan_distance(a.pos, b.pos, abmspace(model))

manhattan_distance(a::AbstractAgent, b::AbstractAgent, model::ABM) =
manhattan_distance(a.pos, b.pos, abmspace(model))
manhattan_distance(p1, p2, model::ABM) = manhattan_distance(p1, p2, abmspace(model))

function manhattan_distance(
Expand Down
6 changes: 3 additions & 3 deletions src/submodules/io/csv_integration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ If `"test.csv"` contains the following columns: `pos_1, pos_2, foo_1, foo_2`, th
can be populated as `AgentsIO.populate_from_csv!(model, "test.csv"; row_number_is_id = true)`.
"""
function populate_from_csv!(
model::ABM{S},
model::ABM,
filename,
agent_type::B = Agents.agenttype(model),
agent_type::Union{Type{<:AbstractAgent},Function} = Agents.agenttype(model),
col_map::Dict{Symbol,Int} = Dict{Symbol,Int}();
row_number_is_id = false,
kwargs...,
) where {B <: Union{Type{<:AbstractAgent},Function},S}
)
@assert(
agent_type isa Function || !(agent_type isa Union),
"agent_type cannot be a Union. It must be a Function or concrete subtype of AbstractAgent"
Expand Down
4 changes: 2 additions & 2 deletions src/submodules/pathfinding/astar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,9 @@ Base.isempty(id::Int, pathfinder::AStar) =
Same, but for pathfinding with A*.
"""
Agents.is_stationary(
agent::A,
agent::AbstractAgent,
pathfinder::AStar,
) where {A<:AbstractAgent} = isempty(agent.id, pathfinder)
) = isempty(agent.id, pathfinder)

"""
Pathfinding.penaltymap(pathfinder)
Expand Down
12 changes: 6 additions & 6 deletions src/submodules/pathfinding/astar_continuous.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,21 @@ function find_continuous_path(
end

function Agents.plan_route!(
agent::A,
agent::AbstractAgent,
dest::Agents.ValidPos,
pathfinder::AStar{D,P,M,Float64},
) where {A<:AbstractAgent,D,P,M}
) where {D,P,M}
path = find_continuous_path(pathfinder, agent.pos, dest)
isnothing(path) && return
pathfinder.agent_paths[agent.id] = path
end

function Agents.plan_best_route!(
agent::A,
agent::AbstractAgent,
dests,
pathfinder::AStar{D,P,M,Float64};
condition::Symbol = :shortest,
) where {A<:AbstractAgent,D,P,M}
) where {D,P,M}
@assert condition (:shortest, :longest)
compare = condition == :shortest ? (a, b) -> a < b : (a, b) -> a > b
best_path = Path{D,Float64}()
Expand Down Expand Up @@ -106,12 +106,12 @@ For pathfinding in models with [`ContinuousSpace`](@ref)
If the agent does not have a precalculated path or the path is empty, it remains stationary.
"""
function Agents.move_along_route!(
agent::A,
agent::AbstractAgent,
model::ABM{<:ContinuousSpace{D}},
pathfinder::AStar{D},
speed::Float64,
dt::Real = 1.0,
) where {D,A<:AbstractAgent}
) where {D}
isempty(agent.id, pathfinder) && return
from = agent.pos
next_pos = agent.pos
Expand Down
8 changes: 4 additions & 4 deletions src/submodules/pathfinding/astar_grid.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ Calculate and store the shortest path to move the agent from its current positio
Use this method in conjunction with [`move_along_route!`](@ref).
"""
function Agents.plan_route!(
agent::A,
agent::AbstractAgent,
dest::Dims{D},
pathfinder::AStar{D},
) where {D,A<:AbstractAgent}
) where {D}
path = find_path(pathfinder, agent.pos, dest)
isnothing(path) && return
pathfinder.agent_paths[agent.id] = path
Expand All @@ -27,11 +27,11 @@ Return the position of the chosen destination. Return `nothing` if none of the s
destinations are reachable.
"""
function Agents.plan_best_route!(
agent::A,
agent::AbstractAgent,
dests,
pathfinder::AStar{D,P,M,Int64};
condition::Symbol = :shortest,
) where {A<:AbstractAgent,D,P,M}
) where {D,P,M}
@assert condition (:shortest, :longest)
compare = condition == :shortest ? (a, b) -> a < b : (a, b) -> a > b
best_path = Path{D,Int64}()
Expand Down

0 comments on commit fa03585

Please sign in to comment.