Agent Conversation: google/gemini-3.1-pro-preview
Task: optimization-hyperparameter-search
Hyperparameter Optimization: Custom Search Strategy Design
Research Question
Design a novel hyperparameter optimization (HPO) strategy that achieves better final validation scores and faster convergence than standard approaches like Random Search, TPE, Hyperband, and their combinations (BOHB, DEHB).
Background
Hyperparameter optimization is a fundamental problem in machine learning: given a model and dataset, find the hyperparameter configuration that maximizes validation performance within a limited evaluation budget. This is a black-box optimization problem where each function evaluation (training + validation) is expensive.
Classic strategies include:
- Random Search: Samples configurations uniformly. Simple but surprisingly effective, especially when some hyperparameters are more important than others (Bergstra & Bengio, 2012).
- TPE (Tree-structured Parzen Estimator): Models p(x|y<y*) and p(x|y>=y*) using kernel density estimation and maximizes their ratio (Bergstra et al., 2011).
- Hyperband: Uses multi-fidelity evaluation (early stopping) with successive halving to allocate resources to promising configurations (Li et al., 2017).
State-of-the-art methods combine these ideas:
- BOHB: Replaces random sampling in Hyperband with TPE-guided suggestions (Falkner et al., 2018).
- DEHB: Uses Differential Evolution within Hyperband's multi-fidelity framework (Awad et al., 2021).
- CMA-ES: Adapts a full covariance matrix of a Gaussian distribution for efficient continuous optimization (Hansen & Ostermeier, 2001).
There is ongoing research into strategies that better adapt to the optimization landscape, leverage multi-fidelity evaluations more effectively, or combine model-based search with evolutionary approaches.
Task
Implement a custom HPO strategy by modifying the CustomHPOStrategy class in scikit-learn/custom_hpo.py. You should implement both __init__ and suggest methods. The class is called repeatedly in a sequential loop where each call proposes one configuration to evaluate.
Interface
class CustomHPOStrategy:
def __init__(self, seed: int = 42):
"""Initialize the strategy with a random seed."""
self.seed = seed
self.rng = np.random.RandomState(seed)
def suggest(
self,
space: SearchSpace,
history: List[Trial],
budget_left: int,
) -> Tuple[Dict[str, Any], float]:
"""Propose the next configuration to evaluate.
Args:
space: SearchSpace with .params (list of HParam), .dim,
.sample_uniform(rng), .clip(config)
history: list of Trial(config, score, budget) from past evals
budget_left: remaining budget in full-fidelity units
Returns:
config: dict mapping hyperparameter names to values
fidelity: float in (0, 1] for multi-fidelity evaluation
"""
The search space pro
Tool Input
1{
2 "op": "replace",
3 "end_line": 326,
4 "filename": "scikit-learn/custom_hpo.py",
5 "content": "class CustomHPOStrategy:\n \"\"\"Surrogate-assisted ASHA (MF-SMAC).\"\"\"\n\n def __init__(self, seed: int = 42):\n self.seed = seed\n self.rng = np.random.RandomState(seed)\n self.f_levels = [0.11, 0.33, 1.0]\n self.eta = 3\n\n def _hash_cfg(self, config):\n return str(sorted(config.items()))\n\n def _encode(self, config, space):\n vec = []\n for p in space.params:\n val = config[p.name]\n if p.type == \"categorical\":\n idx = p.choices.index(val)\n vec.append(idx / max(len(p.choices) - 1, 1))\n elif p.type in (\"float\", \"int\"):\n if p.log_scale:\n v = (np.log(val) - np.log(p.low)) / (np.log(p.high) - np.log(p.low))\n else:\n v = (val - p.low) / (p.high - p.low)\n vec.append(float(np.clip(v, 0, 1)))\n return np.array(vec)\n\n def suggest(\n self,\n space: SearchSpace,\n history: List[Trial],\n budget_left: int,\n ) -> Tuple[Dict[str, Any], float]:\n import math\n from sklearn.ensemble import RandomForestRegressor\n \n # 1. Group history by level\n level_histories = {f: [] for f in self.f_levels}\n for t in history:\n closest_f = min(self.f_levels, key=lambda f: abs(f - t.budget))\n level_histories[closest_f].append(t)\n \n # 2. Check for promotions (top-down)\n for i in range(len(self.f_levels) - 2, -1, -1):\n f_curr = self.f_levels[i]\n f_next = self.f_levels[i+1]\n \n curr_hist = level_histories[f_curr]\n next_hist = level_histories[f_next]\n \n if not curr_hist:\n continue\n \n next_hashes = {self._hash_cfg(t.config) for t in next_hist}\n eligible = [t for t in curr_hist if self._hash_cfg(t.config) not in next_hashes]\n \n if eligible:\n sorted_curr = sorted(curr_hist, key=lambda x: x.score, reverse=True)\n n_promote = max(1, math.ceil(len(curr_hist) / self.eta))\n top_configs = sorted_curr[:n_promote]\n top_hashes = {self._hash_cfg(t.config) for t in top_configs}\n \n promotable = [t for t in eligible if self._hash_cfg(t.config) in top_hashes]\n if promotable:\n best_promotable = max(promotable, key=lambda x: x.score)\n return best_promotable.config.copy(), f_next\n\n # 3. No promotions, sample new config at the lowest level\n if len(history) < 5:\n return space.sample_uniform(self.rng), self.f_levels[0]\n\n # Use Random Forest BO to suggest new configuration\n X_train = []\n y_train = []\n for t in history:\n row = list(self._encode(t.config, space))\n row.append(t.budget)\n X_train.append(row)\n y_train.append(t.score)\n \n rf = RandomForestRegressor(n_estimators=50, random_state=self.seed)\n rf.fit(X_train, y_train)\n \n high_scores = [t.score for t in history if abs(t.budget - 1.0) < 1e-3]\n best_high = max(high_scores) if high_scores else max(y_train)\n \n candidates = []\n for _ in range(100):\n candidates.append(space.sample_uniform(self.rng))\n \n top_history = sorted(history, key=lambda t: t.score, reverse=True)[:3]\n for t in top_history:\n for _ in range(33):\n mutated = t.config.copy()\n n_mut = self.rng.randint(1, max(2, space.dim // 2 + 1))\n params_to_mut = self.rng.choice(space.params, n_mut, replace=False)\n for p in params_to_mut:\n if p.type == \"categorical\":\n mutated[p.name] = self.rng.choice(p.choices)\n elif p.type == \"float\":\n if p.log_scale:\n val = np.log(mutated[p.name])\n val += self.rng.randn() * (np.log(p.high) - np.log(p.low)) * 0.1\n mutated[p.name] = float(np.clip(np.exp(val), p.low, p.high))\n else:\n val = mutated[p.name]\n val += self.rng.randn() * (p.high - p.low) * 0.1\n mutated[p.name] = float(np.clip(val, p.low, p.high))\n elif p.type == \"int\":\n if p.log_scale:\n val = np.log(mutated[p.name])\n val += self.rng.randn() * (np.log(p.high) - np.log(p.low)) * 0.1\n mutated[p.name] = int(round(np.clip(np.exp(val), p.low, p.high)))\n else:\n val = mutated[p.name]\n val += self.rng.randn() * (p.high - p.low) * 0.1\n mutated[p.name] = int(round(np.clip(val, p.low, p.high)))\n candidates.append(mutated)\n \n X_cands = []\n for c in candidates:\n row = list(self._encode(c, space))\n row.append(1.0)\n X_cands.append(row)\n \n X_cands = np.array(X_cands)\n preds = np.array([tree.predict(X_cands) for tree in rf.estimators_])\n mean_pred = preds.mean(axis=0)\n std_pred = preds.std(axis=0)\n \n with np.errstate(divide='ignore', invalid='ignore'):\n z = (mean_pred - best_high) / (std_pred + 1e-9)\n ei = (mean_pred - best_high) * scipy_norm.cdf(z) + std_pred * scipy_norm.pdf(z)\n ei[std_pred == 0.0] = 0.0\n \n best_idx = int(np.argmax(ei))\n return candidates[best_idx], self.f_levels[0]",
6 "start_line": 255
7}OK: Replaced lines 255..326 with 137 line(s) in scikit-learn/custom_hpo.py. Editable range: 255–391. [Current file: scikit-learn/custom_hpo.py | editable: 255–391 | total: 520 lines] 252| # ================================================================ 253| 254| 255 class CustomHPOStrategy: 256 """Surrogate-assisted ASHA (MF-SMAC).""" 257 258 def __init__(self, seed: int = 42): 259 self.seed = seed 260 self.rng = np.random.RandomState(seed) 261 self.f_levels = [0.11, 0.33, 1.0] 262 self.eta = 3 263 264 def _hash_cfg(self, config): 265 return str(sorted(config.items())) 266 267 def _encode(self, config, space): 268 vec = [] 269 for p in space.params: 270 val = config[p.name] 271 if p.type == "categorical": 272 idx = p.choices.index(val) 273 vec.append(idx / max(len(p.ch
Tool Input
1{
2 "content": "class CustomHPOStrategy:\n def __init__(self, seed: int = 42):\n self.seed = seed\n self.rng = np.random.RandomState(seed)\n self.rungs = [0.12, 0.33, 1.0]\n self.eta = 3\n\n def _encode(self, c, s):\n v = []\n for p in s.params:\n val = c[p.name]\n if p.type == \"categorical\":\n v.extend([1.0 if val == ch else 0.0 for ch in p.choices])\n else:\n low, high, val = p.low, p.high, val\n if p.log_scale:\n low, high, val = np.log(low), np.log(high), np.log(val)\n v.append(float(np.clip((val - low) / (high - low), 0., 1.)))\n return np.array(v)\n\n def _mutate(self, c, s):\n nc = c.copy()\n for p in s.params:\n if self.rng.rand() < 0.2:\n if p.type == \"categorical\":\n nc[p.name] = self.rng.choice(p.choices)\n else:\n low, high = (np.log(p.low), np.log(p.high)) if p.log_scale else (p.low, p.high)\n val = np.log(nc[p.name]) if p.log_scale else nc[p.name]\n val = np.clip(val + self.rng.randn() * (high - low) * 0.15, low, high)\n val = np.exp(val) if p.log_scale else val\n nc[p.name] = int(round(val)) if p.type == \"int\" else float(val)\n return nc\n\n def suggest(self, space: SearchSpace, history: List[Trial], budget_left: int) -> Tuple[Dict[str, Any], float]:\n evals = {r: [] for r in self.rungs}\n for t in history:\n evals[min(self.rungs, key=lambda r: abs(r - t.budget))].append(t)\n \n def keys(cfg): return tuple(sorted(cfg.items()))\n \n for i in range(len(self.rungs) - 1, 0, -1):\n prev = sorted(evals[self.rungs[i-1]], key=lambda t: t.score, reverse=True)\n promoted = set()\n for r in self.rungs[i:]:\n promoted.update({keys(t.config) for t in evals[r]})\n for rank, t in enumerate(prev):\n if rank < len(prev) // self.eta and keys(t.config) not in promoted:\n return t.config.copy(), float(self.rungs[i])\n \n if len(history) < 5:\n return space.sample_uniform(self.rng), float(self.rungs[0])\n \n from sklearn.ensemble import RandomForestRegressor\n X = np.array([list(self._encode(t.config, space)) + [t.budget] for t in history])\n y = np.array([t.score for t in history])\n rf = RandomForestRegressor(n_estimators=50, random_state=self.seed)\n rf.fit(X, (y - y.mean()) / (y.std() + 1e-6))\n \n cands = [space.sample_uniform(self.rng) for _ in range(200)]\n for bt in sorted(history, key=lambda t: t.score, reverse=True)[:5]:\n cands.extend([self._mutate(bt.config, space) for _ in range(40)])\n \n X_c = np.array([list(self._encode(c, space)) + [1.0] for c in cands])\n preds = np.array([tree.predict(X_c) for tree in rf.estimators_])\n acq = preds.mean(axis=0) + 1.5 * (preds.std(axis=0) + 1e-6)\n \n return cands[int(np.argmax(acq))], float(self.rungs[0])",
3 "filename": "scikit-learn/custom_hpo.py",
4 "end_line": 326,
5 "start_line": 255,
6 "op": "replace"
7}OK: Replaced lines 255..326 with 68 line(s) in scikit-learn/custom_hpo.py. Editable range: 255–322. [Current file: scikit-learn/custom_hpo.py | editable: 255–322 | total: 451 lines] 252| # ================================================================ 253| 254| 255 class CustomHPOStrategy: 256 def __init__(self, seed: int = 42): 257 self.seed = seed 258 self.rng = np.random.RandomState(seed) 259 self.rungs = [0.12, 0.33, 1.0] 260 self.eta = 3 261 262 def _encode(self, c, s): 263 v = [] 264 for p in s.params: 265 val = c[p.name] 266 if p.type == "categorical": 267 v.extend([1.0 if val == ch else 0.0 for ch in p.choices]) 268 else: 269 low, high, val = p.low, p.high, val 270 if p.log_scale: 271 low, high, val = np.log(low), np.log(high), np.log(val)
[Test #1] (2 tests remaining; call submit(n=1) to submit this result) ### xgboost (scripts/xgboost.sh) Training progress (last evaluations): TRAIN_METRICS eval=190 cost=47.0/50 best_score=-0.393870 elapsed=1548.6s TRAIN_METRICS eval=195 cost=47.8/50 best_score=-0.393870 elapsed=1562.2s TRAIN_METRICS eval=200 cost=48.6/50 best_score=-0.393870 elapsed=1583.8s TRAIN_METRICS eval=205 cost=49.4/50 best_score=-0.393870 elapsed=1612.6s TRAIN_METRICS eval=207 cost=50.8/50 best_score=-0.391863 elapsed=1644.0s Test results (xgboost): best_val_score: -0.391863 convergence_auc: 0.914665 total_evals: 207.000000 ### svm (scripts/svm.sh) Training progress (last evaluations): TRAIN_METRICS eval=150 cost=34.8/40 best_score=0.975423 elapsed=4.8s TRAIN_METRICS eval=155 cost=35.6/40 best_score=0.975423 elapsed=4.9s TRAIN_METRICS eval=160 cost=37.3/40 best_score=0.975423 elapsed=5.1s TRAIN_METRICS eval=165 cost=38.1/40 best_score=0.975423 elapsed=5.3s TRAIN_METRICS eval=170 cost=40.0/40 best_score=
Tool Input
1{
2 "start_line": 255,
3 "op": "replace",
4 "content": "class CustomHPOStrategy:\n def __init__(self, seed: int = 42):\n self.seed = seed\n self.rng = np.random.RandomState(seed)\n self.rungs = [0.25, 1.0]\n self.eta = 2\n\n def _encode(self, c, s):\n v = []\n for p in s.params:\n val = c[p.name]\n if p.type == \"categorical\":\n v.extend([1.0 if val == ch else 0.0 for ch in p.choices])\n else:\n low, high, val = p.low, p.high, val\n if p.log_scale:\n low, high, val = np.log(low), np.log(high), np.log(val)\n v.append(float(np.clip((val - low) / (high - low), 0., 1.)))\n return np.array(v)\n\n def _mutate(self, c, s):\n nc = c.copy()\n for p in s.params:\n if self.rng.rand() < 0.3:\n if p.type == \"categorical\":\n nc[p.name] = self.rng.choice(p.choices)\n else:\n low, high = (np.log(p.low), np.log(p.high)) if p.log_scale else (p.low, p.high)\n val = np.log(nc[p.name]) if p.log_scale else nc[p.name]\n val = np.clip(val + self.rng.randn() * (high - low) * 0.15, low, high)\n val = np.exp(val) if p.log_scale else val\n nc[p.name] = int(round(val)) if p.type == \"int\" else float(val)\n return nc\n\n def suggest(self, space: SearchSpace, history: List[Trial], budget_left: int) -> Tuple[Dict[str, Any], float]:\n evals = {r: [] for r in self.rungs}\n for t in history:\n evals[min(self.rungs, key=lambda r: abs(r - t.budget))].append(t)\n \n def keys(cfg): return tuple(sorted(cfg.items()))\n \n for i in range(len(self.rungs) - 1, 0, -1):\n prev = sorted(evals[self.rungs[i-1]], key=lambda t: t.score, reverse=True)\n promoted = set()\n for r in self.rungs[i:]:\n promoted.update({keys(t.config) for t in evals[r]})\n for rank, t in enumerate(prev):\n if rank < len(prev) // self.eta and keys(t.config) not in promoted:\n return t.config.copy(), float(self.rungs[i])\n \n if len(history) < 8:\n return space.sample_uniform(self.rng), float(self.rungs[0])\n \n from sklearn.ensemble import RandomForestRegressor\n X = np.array([list(self._encode(t.config, space)) + [t.budget] for t in history])\n y = np.array([t.score for t in history])\n rf = RandomForestRegressor(n_estimators=50, min_samples_split=3, random_state=self.seed)\n rf.fit(X, (y - y.mean()) / (y.std() + 1e-6))\n \n cands = [space.sample_uniform(self.rng) for _ in range(200)]\n for bt in sorted(history, key=lambda t: t.score, reverse=True)[:5]:\n cands.extend([self._mutate(bt.config, space) for _ in range(40)])\n \n X_c = np.array([list(self._encode(c, space)) + [1.0] for c in cands])\n preds = np.array([tree.predict(X_c) for tree in rf.estimators_])\n acq = preds.mean(axis=0) + 1.96 * (preds.std(axis=0) + 1e-6)\n \n return cands[int(np.argmax(acq))], float(self.rungs[0])",
5 "end_line": 322,
6 "filename": "scikit-learn/custom_hpo.py"
7}OK: Replaced lines 255..322 with 68 line(s) in scikit-learn/custom_hpo.py. Editable range: 255–322. [Current file: scikit-learn/custom_hpo.py | editable: 255–322 | total: 451 lines] 252| # ================================================================ 253| 254| 255 class CustomHPOStrategy: 256 def __init__(self, seed: int = 42): 257 self.seed = seed 258 self.rng = np.random.RandomState(seed) 259 self.rungs = [0.25, 1.0] 260 self.eta = 2 261 262 def _encode(self, c, s): 263 v = [] 264 for p in s.params: 265 val = c[p.name] 266 if p.type == "categorical": 267 v.extend([1.0 if val == ch else 0.0 for ch in p.choices]) 268 else: 269 low, high, val = p.low, p.high, val 270 if p.log_scale: 271 low, high, val = np.log(low), np.log(high), np.log(val) 27
[Test #2] (1 test remaining; call submit(n=2) to submit this result) ### xgboost (scripts/xgboost.sh) Training progress (last evaluations): TRAIN_METRICS eval=85 cost=42.2/50 best_score=-0.388965 elapsed=960.8s TRAIN_METRICS eval=90 cost=45.0/50 best_score=-0.388965 elapsed=1011.6s TRAIN_METRICS eval=95 cost=47.0/50 best_score=-0.388965 elapsed=1039.5s TRAIN_METRICS eval=100 cost=49.8/50 best_score=-0.388965 elapsed=1112.4s TRAIN_METRICS eval=101 cost=50.0/50 best_score=-0.388965 elapsed=1115.1s Test results (xgboost): best_val_score: -0.388965 convergence_auc: 0.904130 total_evals: 101.000000 ### svm (scripts/svm.sh) Training progress (last evaluations): TRAIN_METRICS eval=60 cost=30.8/40 best_score=0.980686 elapsed=1.6s TRAIN_METRICS eval=65 cost=33.5/40 best_score=0.980686 elapsed=1.8s TRAIN_METRICS eval=70 cost=36.2/40 best_score=0.980686 elapsed=1.9s TRAIN_METRICS eval=75 cost=38.2/40 best_score=0.980686 elapsed=2.1s TRAIN_METRICS eval=79 cost=40.0/40 best_score=0.980686 e
Tool Input
1{
2 "n": 2
3}[submit] Submitting result from test #2 as final. ### xgboost (scripts/xgboost.sh) Training progress (last evaluations): TRAIN_METRICS eval=85 cost=42.2/50 best_score=-0.388965 elapsed=960.8s TRAIN_METRICS eval=90 cost=45.0/50 best_score=-0.388965 elapsed=1011.6s TRAIN_METRICS eval=95 cost=47.0/50 best_score=-0.388965 elapsed=1039.5s TRAIN_METRICS eval=100 cost=49.8/50 best_score=-0.388965 elapsed=1112.4s TRAIN_METRICS eval=101 cost=50.0/50 best_score=-0.388965 elapsed=1115.1s Test results (xgboost): best_val_score: -0.388965 convergence_auc: 0.904130 total_evals: 101.000000 ### svm (scripts/svm.sh) Training progress (last evaluations): TRAIN_METRICS eval=60 cost=30.8/40 best_score=0.980686 elapsed=1.6s TRAIN_METRICS eval=65 cost=33.5/40 best_score=0.980686 elapsed=1.8s TRAIN_METRICS eval=70 cost=36.2/40 best_score=0.980686 elapsed=1.9s TRAIN_METRICS eval=75 cost=38.2/40 best_score=0.980686 elapsed=2.1s TRAIN_METRICS eval=79 cost=40.0/40 best_score=0.980686 elapsed=2.2s Test re