|
| 1 | +# Author: Simon Blanke |
| 2 | + |
| 3 | +# License: MIT License |
| 4 | + |
| 5 | +"""Registry of ML functions that support surrogate training. |
| 6 | +
|
| 7 | +This module defines which ML functions can have surrogates trained, |
| 8 | +along with their fixed parameter grids (dataset, cv combinations). |
| 9 | +""" |
| 10 | + |
| 11 | +from typing import Any, Dict, List, Type |
| 12 | + |
| 13 | +# Registry: function_name -> config |
| 14 | +ML_SURROGATE_REGISTRY: Dict[str, Dict[str, Any]] = {} |
| 15 | + |
| 16 | + |
| 17 | +def register_ml_function( |
| 18 | + name: str, |
| 19 | + function_class: Type, |
| 20 | + fixed_params: Dict[str, List], |
| 21 | + hyperparams: List[str], |
| 22 | +): |
| 23 | + """Register an ML function for surrogate training. |
| 24 | +
|
| 25 | + Parameters |
| 26 | + ---------- |
| 27 | + name : str |
| 28 | + Unique identifier (e.g., "k_neighbors_classifier"). |
| 29 | + function_class : Type |
| 30 | + The function class (e.g., KNeighborsClassifierFunction). |
| 31 | + fixed_params : dict |
| 32 | + Grid of fixed parameters to iterate over during training. |
| 33 | + Example: {"dataset": ["iris", "digits"], "cv": [2, 3, 5, 10]} |
| 34 | + hyperparams : list |
| 35 | + Names of hyperparameters in the search space. |
| 36 | + """ |
| 37 | + ML_SURROGATE_REGISTRY[name] = { |
| 38 | + "class": function_class, |
| 39 | + "fixed_params": fixed_params, |
| 40 | + "hyperparams": hyperparams, |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | +def get_registered_functions() -> List[str]: |
| 45 | + """Get list of registered function names.""" |
| 46 | + _ensure_registered() |
| 47 | + return list(ML_SURROGATE_REGISTRY.keys()) |
| 48 | + |
| 49 | + |
| 50 | +def get_function_config(name: str) -> Dict[str, Any]: |
| 51 | + """Get configuration for a registered function.""" |
| 52 | + _ensure_registered() |
| 53 | + if name not in ML_SURROGATE_REGISTRY: |
| 54 | + raise ValueError( |
| 55 | + f"Unknown function '{name}'. " |
| 56 | + f"Available: {get_registered_functions()}" |
| 57 | + ) |
| 58 | + return ML_SURROGATE_REGISTRY[name] |
| 59 | + |
| 60 | + |
| 61 | +# ============================================================================ |
| 62 | +# Register ML functions (lazy to avoid circular imports) |
| 63 | +# ============================================================================ |
| 64 | + |
| 65 | +def _ensure_registered(): |
| 66 | + """Register all ML functions lazily on first access.""" |
| 67 | + if ML_SURROGATE_REGISTRY: |
| 68 | + return # Already registered |
| 69 | + |
| 70 | + from surfaces.test_functions import ( |
| 71 | + KNeighborsClassifierFunction, |
| 72 | + KNeighborsRegressorFunction, |
| 73 | + GradientBoostingRegressorFunction, |
| 74 | + ) |
| 75 | + |
| 76 | + # Classification functions |
| 77 | + register_ml_function( |
| 78 | + name="k_neighbors_classifier", |
| 79 | + function_class=KNeighborsClassifierFunction, |
| 80 | + fixed_params={ |
| 81 | + "dataset": ["digits", "iris", "wine"], |
| 82 | + "cv": [2, 3, 5, 10], |
| 83 | + }, |
| 84 | + hyperparams=["n_neighbors", "algorithm"], |
| 85 | + ) |
| 86 | + |
| 87 | + # Regression functions |
| 88 | + register_ml_function( |
| 89 | + name="k_neighbors_regressor", |
| 90 | + function_class=KNeighborsRegressorFunction, |
| 91 | + fixed_params={ |
| 92 | + "dataset": ["diabetes", "california"], |
| 93 | + "cv": [2, 3, 5, 10], |
| 94 | + }, |
| 95 | + hyperparams=["n_neighbors", "algorithm"], |
| 96 | + ) |
| 97 | + |
| 98 | + register_ml_function( |
| 99 | + name="gradient_boosting_regressor", |
| 100 | + function_class=GradientBoostingRegressorFunction, |
| 101 | + fixed_params={ |
| 102 | + "dataset": ["diabetes", "california"], |
| 103 | + "cv": [2, 3, 5, 10], |
| 104 | + }, |
| 105 | + hyperparams=["n_estimators", "max_depth"], |
| 106 | + ) |
0 commit comments