Implement a Mu Plus Lambda Evolutionary Hyperparameter Search
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `sample_config`, `mutate_config` and `evolutionary_search`, a (mu + lambda) search that minimises `objective(config)`. `space` maps each parameter name to `{"type": "log", "low": ..., "high": ...}` or `{"type": "categorical", "choices": [...]}`. RNG draw order is graded exactly, so iterate parameters in dict order.
```python
def sample_config(space, rng): ...
def mutate_config(config, space, sigma, p_cat, rng): ...
def evolutionary_search(objective, space, mu, lam, n_generations,
sigma=0.5, p_cat=0.2, seed=0): ...
```
Requirements:
- `sample_config` draws a log parameter as `exp(rng.uniform(log(low), log(high)))` and a categorical as `choices[rng.integers(len(choices))]`.
- `mutate_config` returns a new dict and leaves the parent untouched. A log parameter draws `z = rng.standard_normal()` and becomes `exp(log(v) + sigma * z)`, clipped to `[low, high]`.
- Categorical mutation always draws `u = rng.random()`. If `u < p_cat` and other choices exist, pick `others[rng.integers(len(others))]`, where `others` is the choices minus the parent's value, in their original order. Otherwise keep the parent's value. An unknown type raises `ValueError`.
- The search sets `rng = np.random.default_rng(seed)` and samples `mu` configs as generation 0 with `parent_id = -1`. Each generation creates `lam` children. Each child takes its parent as `population[rng.integers(mu)]` from the sorted population, is mutated, and is evaluated immediately.
- Survivors are the top `mu` of population plus offspring, sorted by `(score, trial_id)`. `mu < 1`, `lam < 1` or `n_generations < 0` raises `ValueError`.
Return shape:
```python
{
"trials": [ # every evaluation, in order; trial_id = index
{"trial_id": 0, "generation": 0, "parent_id": -1,
"config": {...}, "score": float(objective(config))}, ...
],
"population": [ids of final survivors, best first],
"best_config": {...}, "best_score": float, "best_trial_id": int
}
```
```hint RNG call order
List every RNG call that one child consumes, in order, before writing any code. A draw that is skipped on one branch, or made in a different place, shifts every draw after it.
```
```hint Shared objects
Check which dicts your mutation writes to. The parent's config and the config stored in its trial record must look the same after the child is built.
```
### Constraints & Assumptions
- `mu >= 1`, `lam >= 1` and `n_generations >= 0`. A run makes `mu + n_generations * lam` objective calls.
- Log parameters have `0 < low <= high`.
- `objective` takes a config dict, returns a number, and does not draw from `rng`.
- Defaults are `sigma = 0.5`, `p_cat = 0.2` and `seed = 0`.
### What a Strong Answer Covers
- Sampling and mutation that consume the generator in exactly the specified order, including the categorical draw `u` that happens even when the value does not change.
- Log-space mutation with clipping to the bounds, and why a multiplicative step suits scale parameters such as a learning rate.
- Elitist survivor selection over the merged pool with the `(score, trial_id)` tie-break, and what it guarantees about the best score.
- Trial bookkeeping: sequential ids, generation numbers, parent ids, and logged configs that never change afterwards.
- Input validation, plus time and space complexity in terms of mu, lambda, the number of generations and the number of parameters.
### Follow-up Questions
- How does a (mu, lambda) comma strategy differ, and when would you choose it over the plus strategy?
- How would you adapt `sigma` during the search instead of fixing it?
- How would you evaluate the `lam` children of a generation in parallel and still get reproducible results?
- How would you handle a noisy objective, where one lucky evaluation can keep a weak config in the population forever?
Overview: Implement a seeded (mu + lambda) evolutionary search over log-scale and categorical hyperparameters. It tests exact random draw order, log-space mutation with clipping, elitist survivor selection with a deterministic tie-break, and careful trial bookkeeping.