Skip to content

Commit ae830db

Browse files
committed
Improve code clarity
1 parent 7ee25d4 commit ae830db

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/parameters.jl

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ end
1313

1414
function initialize(rs::AbstractVector, tuning::AbstractParticleSwarm)
1515
n = tuning.n_particles
16-
ranges, parameters, lens, Xᵢ = zip(_initialize.(tuning.rng, rs, n)...)
17-
indices = _to_indices(lens)
16+
# `length` is 1 for `NumericRange` and the number of categories for `NominalRange`
17+
ranges, parameters, lengths, Xᵢ = zip(_initialize.(tuning.rng, rs, n)...)
18+
indices = _to_indices(lengths)
1819
X = hcat(Xᵢ...)
1920
V = zero(X)
2021
pbest_X = copy(X)
@@ -83,13 +84,14 @@ function _initialize(rng, r::NumericRange{T}, d::UnivariateDistribution, n) wher
8384
end
8485

8586
# Helper function to get ranges' corresponding indices
86-
87-
function _to_indices(lens)
88-
curr = Ref(1)
89-
return map(lens) do len
90-
start = curr[]
91-
stop = start + len - 1
92-
curr[] = stop + 1
87+
# E.g., `_to_indices((1, 2, 1, 3))` returns `(1, 2:3, 4, 5:7)`
88+
89+
function _to_indices(lengths)
90+
curr = 1
91+
return map(lengths) do length
92+
start = curr
93+
stop = start + length - 1
94+
curr = stop + 1
9395
start == stop ? stop : (start:stop)
9496
end
9597
end
@@ -98,6 +100,9 @@ end
98100
### Retrieval
99101
###
100102

103+
# For updating `state.parameters`, the model hyperparameters to be returned, from their
104+
# internal representation `state.X`
105+
101106
function retrieve!(state::ParticleSwarmState, tuning::AbstractParticleSwarm)
102107
ranges, params, indices, X = state.ranges, state.parameters, state.indices, state.X
103108
rng = tuning.rng

0 commit comments

Comments
 (0)