Skip to content

Commit

Permalink
create extent attr for GridSpaces (#880)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tortar authored Sep 24, 2023
1 parent e1e58a7 commit a9263ec
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
10 changes: 5 additions & 5 deletions src/spaces/grid_general.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ end

npositions(space::AbstractGridSpace) = length(space.stored_ids)

# ALright, so here is the design for basic nearby_stuff looping.
# ALright, so here is the design for basic nearby_stuff looping.
# We initialize a vector of tuples of indices within radius `r` from origin position.
# We store this vector. When we have to loop over nearby_stuff, we call this vector
# and add it to the given position. That is what the concrete implementations of
Expand Down Expand Up @@ -175,27 +175,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 @@ -74,6 +76,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 @@ -112,7 +115,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 @@ -247,7 +250,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 @@ -32,7 +34,7 @@ function GridSpaceSingle(d::NTuple{D,Int};
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 @@ -88,7 +90,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 @@ -107,7 +109,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 a9263ec

Please sign in to comment.