Skip to content

Commit 30bd293

Browse files
committed
Stopguard against out of bound accessx
1 parent 0d04e95 commit 30bd293

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

src/Population.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,9 @@ end
212212
# Return best 10 examples
213213
function best_sub_pop(pop::P; topn::Int=10)::P where {P<:Population}
214214
best_idx = sortperm([pop.members[member].cost for member in 1:(pop.n)])
215-
return Population(pop.members[best_idx[1:topn]])
215+
# Ensure we don't try to access more elements than exist in the population
216+
actual_topn = min(topn, pop.n)
217+
return Population(pop.members[best_idx[1:actual_topn]])
216218
end
217219

218220
function record_population(pop::Population, options::AbstractOptions)::RecordType

0 commit comments

Comments
 (0)