|
13 | 13 |
|
14 | 14 | function initialize(rs::AbstractVector, tuning::AbstractParticleSwarm)
|
15 | 15 | 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) |
18 | 19 | X = hcat(Xᵢ...)
|
19 | 20 | V = zero(X)
|
20 | 21 | pbest_X = copy(X)
|
@@ -83,13 +84,14 @@ function _initialize(rng, r::NumericRange{T}, d::UnivariateDistribution, n) wher
|
83 | 84 | end
|
84 | 85 |
|
85 | 86 | # 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 |
93 | 95 | start == stop ? stop : (start:stop)
|
94 | 96 | end
|
95 | 97 | end
|
|
98 | 100 | ### Retrieval
|
99 | 101 | ###
|
100 | 102 |
|
| 103 | +# For updating `state.parameters`, the model hyperparameters to be returned, from their |
| 104 | +# internal representation `state.X` |
| 105 | + |
101 | 106 | function retrieve!(state::ParticleSwarmState, tuning::AbstractParticleSwarm)
|
102 | 107 | ranges, params, indices, X = state.ranges, state.parameters, state.indices, state.X
|
103 | 108 | rng = tuning.rng
|
|
0 commit comments