Implement the exploit-and-explore step of population-based training (PBT)
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `pbt_exploit_explore(fitness, weights, hparams, bounds, seed, perturb_factors=(0.8, 1.2))`, one exploit-and-explore step of population-based training (PBT). Each worker in the bottom half of the population copies the weights and hyperparameters of an elite donor, then perturbs the hyperparameters that have bounds. Return `(new_weights, new_hparams, sources)`, where `sources[i]` is the donor id when worker `i` is a loser and `i` otherwise.
The step must follow these rules:
- Rank workers by fitness, descending. On a tie, the lower worker id ranks higher. With `k = n // 2`, the top `k` workers are elites and the bottom `k` are losers. When `n` is odd, the middle worker is kept unchanged.
- Use `rng = np.random.default_rng(seed)` and process losers in increasing worker id. For each loser, first pick its donor as `elites[rng.integers(k)]`, with elites listed in rank order.
- Still for that loser, go through the names in `bounds` in insertion order. For each name, draw `perturb_factors[rng.integers(len(perturb_factors))]` and set the value to `float(clip(donor_value * factor, lo, hi))`. Only then move on to the next loser.
- Hyperparameters not in `bounds` are copied from the donor unchanged.
- Never mutate `fitness`, `weights` or `hparams`.
For example:
```python
fitness = [0.1, 0.9, 0.5]; weights = [[0.0], [1.0], [2.0]]
hparams = [{"lr": 0.001, "momentum": 0.1, "opt": "sgd"},
{"lr": 0.08, "momentum": 0.5, "opt": "adam"},
{"lr": 0.02, "momentum": 0.3, "opt": "rmsprop"}]
bounds = {"lr": (1e-4, 0.1), "momentum": (0.0, 0.9)}
pbt_exploit_explore(fitness, weights, hparams, bounds, seed=0, perturb_factors=(2.0,))
# sources -> [1, 1, 2]
# new_weights -> [[1.0], [1.0], [2.0]]
# new_hparams[0] -> {"lr": 0.1, "momentum": 0.9, "opt": "adam"} # clipped
# new_hparams[2] unchanged (middle worker)
```
```hint Ranking with ties
Look for a sort that keeps equal fitness values in worker id order, and think about how to get a descending order out of it without reversing the ties.
```
```hint Draw order
The expected output replays the generator. Count how many draws each loser consumes and in which order before you write the loop.
```
### Constraints & Assumptions
- `weights` has shape `(n, ...)` and `len(hparams) == n`.
- Every key in `bounds` is present in every `hparams` dict with a numeric value, and `lo <= hi`.
- Fitness values are finite.
### What a Strong Answer Covers
- Ranking that puts higher fitness first and breaks ties toward the lower id, and the split into `k` elites, `k` losers and an untouched middle worker for odd `n`.
- The exact random draw order: losers in increasing id, and for each loser one donor draw followed by one factor draw per bounded name in insertion order.
- Copying the inputs before writing, so nothing the caller passed in is mutated, while unbounded hyperparameters such as `opt` come from the donor.
- Clipping each perturbed value to its bounds and storing it as a Python float.
- Edge cases such as `n` of 0 or 1, tied fitness values and a single perturbation factor.
### Follow-up Questions
- What does a multiplicative perturbation buy over an additive one, and when would you resample a value from the prior instead?
- How would you adapt this step to an asynchronous population where workers finish at different times?
- What risks come from truncation selection on noisy fitness, and how would you reduce them?
Overview: Implement one exploit-and-explore step of population-based training: rank workers by fitness, copy elite weights and hyperparameters into the bottom half, and perturb the bounded values with a seeded generator. It tests stable tie-breaking, a reproducible random draw order, clipping to bounds and copying without mutating the inputs.