Skip to content

Commit

Permalink
create extent attr for GridSpaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Tortar committed Sep 12, 2023
1 parent e48591d commit 27583ad
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 15 deletions.
8 changes: 4 additions & 4 deletions src/spaces/grid_general.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,27 +132,27 @@ function nearby_positions(
pos::ValidPos, space::AbstractGridSpace{D,false}, r = 1,
get_indices_f = offsets_within_radius_no_0 # NOT PUBLIC API! For `ContinuousSpace`.
) where {D}
stored_ids = space.stored_ids
nindices = get_indices_f(space, r)
space_size = size(stored_ids)
space_size = spacesize(space)
# check if we are far from the wall to skip bounds checks
if all(i -> r < pos[i] <= space_size[i] - r, 1:D)
return (n .+ pos for n in nindices)
else
stored_ids = space.stored_ids
return (n .+ pos for n in nindices if checkbounds(Bool, stored_ids, (n .+ pos)...))
end
end
function nearby_positions(
pos::ValidPos, space::AbstractGridSpace{D,true}, r = 1,
get_indices_f = offsets_within_radius_no_0 # NOT PUBLIC API! For `ContinuousSpace`.
) where {D}
stored_ids = space.stored_ids
nindices = get_indices_f(space, r)
space_size = size(space)
space_size = spacesize(space)
# check if we are far from the wall to skip bounds checks
if all(i -> r < pos[i] <= space_size[i] - r, 1:D)
return (n .+ pos for n in nindices)
else
stored_ids = space.stored_ids
return (checkbounds(Bool, stored_ids, (n .+ pos)...) ?
n .+ pos : mod1.(n .+ pos, space_size) for n in nindices)
end
Expand Down
7 changes: 5 additions & 2 deletions src/spaces/grid_multi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ export GridSpace
# type P stands for Periodic and is a boolean
struct GridSpace{D,P} <: AbstractGridSpace{D,P}
stored_ids::Array{Vector{Int},D}
extent::NTuple{D,Int}
metric::Symbol
offsets_at_radius::Dict{Int,Vector{NTuple{D,Int}}}
offsets_within_radius::Dict{Int,Vector{NTuple{D,Int}}}
offsets_within_radius_no_0::Dict{Int,Vector{NTuple{D,Int}}}
indices_within_radius_tuple::Dict{NTuple{D,Int},Vector{NTuple{D,Int}}}
end
spacesize(space::GridSpace) = space.extent

"""
GridSpace(d::NTuple{D, Int}; periodic = true, metric = :chebyshev)
Expand Down Expand Up @@ -72,6 +74,7 @@ function GridSpace(
end
return GridSpace{D,periodic}(
stored_ids,
d,
metric,
Dict{Int,Vector{NTuple{D,Int}}}(),
Dict{Int,Vector{NTuple{D,Int}}}(),
Expand Down Expand Up @@ -110,7 +113,7 @@ function nearby_ids(pos::NTuple{D, Int}, space::GridSpace{D,P}, r::Real = 1) whe
nindices = offsets_within_radius(space, r)
L = length(nindices)
stored_ids = space.stored_ids
space_size = size(stored_ids)
space_size = spacesize(space)
nocheck = all(i -> r < pos[i] <= space_size[i] - r, 1:D)
return GridSpaceIdIterator{P, D}(stored_ids, nindices, pos, L, space_size, nocheck)
end
Expand Down Expand Up @@ -231,7 +234,7 @@ function nearby_ids(
end

function bound_range(unbound, d, space::GridSpace{D,false}) where {D}
return range(max(unbound.start, 1), stop = min(unbound.stop, size(space)[d]))
return range(max(unbound.start, 1), stop = min(unbound.stop, spacesize(space)[d]))
end


Expand Down
8 changes: 5 additions & 3 deletions src/spaces/grid_single.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ export GridSpaceSingle, id_in_position
# type P stands for Periodic and is a boolean
struct GridSpaceSingle{D,P} <: AbstractGridSpace{D,P}
stored_ids::Array{Int,D}
extent::NTuple{D,Int}
metric::Symbol
offsets_at_radius::Dict{Int,Vector{NTuple{D,Int}}}
offsets_within_radius::Dict{Int,Vector{NTuple{D,Int}}}
offsets_within_radius_no_0::Dict{Int,Vector{NTuple{D,Int}}}
end
spacesize(space::GridSpaceSingle) = space.extent

"""
GridSpaceSingle(d::NTuple{D, Int}; periodic = true, metric = :chebyshev)
Expand All @@ -29,7 +31,7 @@ All arguments and keywords behave exactly as in [`GridSpace`](@ref).
"""
function GridSpaceSingle(d::NTuple{D,Int}; periodic = true, metric = :chebyshev) where {D}
s = zeros(Int, d)
return GridSpaceSingle{D,periodic}(s, metric,
return GridSpaceSingle{D,periodic}(s, d, metric,
Dict{Int,Vector{NTuple{D,Int}}}(),
Dict{Int,Vector{NTuple{D,Int}}}(),
Dict{Int,Vector{NTuple{D,Int}}}(),
Expand Down Expand Up @@ -85,7 +87,7 @@ function nearby_ids(pos::NTuple{D, Int}, model::ABM{<:GridSpaceSingle{D,true}},
) where {D}
nindices = get_offset_indices(model, r)
stored_ids = abmspace(model).stored_ids
space_size = size(stored_ids)
space_size = spacesize(model)
position_iterator = (pos .+ β for β in nindices)
# check if we are far from the wall to skip bounds checks
if all(i -> r < pos[i] <= space_size[i] - r, 1:D)
Expand All @@ -104,7 +106,7 @@ function nearby_ids(pos::NTuple{D, Int}, model::ABM{<:GridSpaceSingle{D,false}},
) where {D}
nindices = get_offset_indices(model, r)
stored_ids = abmspace(model).stored_ids
space_size = size(stored_ids)
space_size = spacesize(model)
position_iterator = (pos .+ β for β in nindices)
# check if we are far from the wall to skip bounds checks
if all(i -> r < pos[i] <= space_size[i] - r, 1:D)
Expand Down
8 changes: 4 additions & 4 deletions src/submodules/io/jld2_integration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ function to_serializable(t::ABM{S}) where {S}
end

to_serializable(t::GridSpace{D,P}) where {D,P} =
SerializableGridSpace{D,P}(size(t), t.metric)
SerializableGridSpace{D,P}(spacesize(t), t.metric)
to_serializable(t::GridSpaceSingle{D,P}) where {D,P} =
SerializableGridSpaceSingle{D,P}(size(t), t.metric)
SerializableGridSpaceSingle{D,P}(spacesize(t), t.metric)

to_serializable(t::ContinuousSpace{D,P,T}) where {D,P,T} =
SerializableContinuousSpace{D,P,T}(to_serializable(t.grid), t.dims, t.spacing, t.extent)
Expand Down Expand Up @@ -142,11 +142,11 @@ function from_serializable(t::SerializableGridSpace{D,P}; kwargs...) where {D,P}
for i in eachindex(s)
s[i] = Int[]
end
return GridSpace{D,P}(s, t.metric, Dict(), Dict(), Dict(), Dict())
return GridSpace{D,P}(s, t.dims, t.metric, Dict(), Dict(), Dict(), Dict())
end
function from_serializable(t::SerializableGridSpaceSingle{D,P}; kwargs...) where {D,P}
s = zeros(Int, t.dims)
return GridSpaceSingle{D,P}(s, t.metric, Dict(), Dict(), Dict())
return GridSpaceSingle{D,P}(s, t.dims, t.metric, Dict(), Dict(), Dict())
end

function from_serializable(t::SerializableContinuousSpace{D,P,T}; kwargs...) where {D,P,T}
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 @@ -52,7 +52,7 @@ to specify the level of discretisation of the space.
- `admissibility = 0.0` allows the algorithm to approximate paths to speed up pathfinding.
A value of `admissibility` allows paths with at most `(1+admissibility)` times the optimal
length.
- `walkmap = trues(size(space))` specifies the (un)walkable positions of the
- `walkmap = trues(spacesize(space))` specifies the (un)walkable positions of the
space. If specified, it should be a `BitArray` of the same size as the corresponding
`GridSpace`. By default, agents can walk anywhere in the space.
- `cost_metric = DirectDistance{D}()` is an instance of a cost metric and specifies the
Expand Down Expand Up @@ -84,7 +84,7 @@ AStar(
space::GridSpace{D,periodic};
diagonal_movement::Bool = true,
admissibility::Float64 = 0.0,
walkmap::BitArray{D} = trues(size(space)),
walkmap::BitArray{D} = trues(spacesize(space)),
cost_metric::CostMetric{D} = DirectDistance{D}(),
) where {D,periodic} =
AStar(size(space); periodic, diagonal_movement, admissibility, walkmap, cost_metric)
Expand Down

0 comments on commit 27583ad

Please sign in to comment.