Stop After a Time Budget
Time: 5 min | Difficulty: Beginner
What This Solves
Use TimerStopping to stop launching candidate batches once the search budget expires. Allow extra wall-clock time for a running batch, final selection and refit.
Recipe
python
import time
from sklearn.datasets import load_breast_cancer
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import StratifiedKFold, train_test_split
from sklearn_genetic import GASearchCV, EvolutionConfig, RuntimeConfig
from sklearn_genetic.callbacks import TimerStopping
from sklearn_genetic.space import Categorical, Continuous, Integer
X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, stratify=y, random_state=42
)
BUDGET_SECONDS = 2
ga = GASearchCV(
estimator=RandomForestClassifier(random_state=42, n_jobs=1),
param_grid={
"n_estimators": Integer(50, 300),
"max_depth": Integer(3, 20),
"max_features": Continuous(0.1, 1.0),
"class_weight": Categorical([None, "balanced"]),
},
scoring="roc_auc",
cv=StratifiedKFold(n_splits=5, shuffle=True, random_state=42),
evolution_config=EvolutionConfig(
population_size=20,
generations=100, # large upper limit — timer will stop it sooner
elitism=True,
),
runtime_config=RuntimeConfig(n_jobs=1, verbose=False),
random_state=42,
)
started = time.perf_counter()
ga.fit(X_train, y_train, callbacks=[TimerStopping(total_seconds=BUDGET_SECONDS)])
elapsed = time.perf_counter() - started
last_generation = ga.history["gen"][-1]
print(f"Last logged generation {last_generation} after {elapsed:.1f}s (budget: {BUDGET_SECONDS}s)")
print(f"Best CV ROC AUC: {ga.best_score_:.4f}")Combine Timer with Plateau Detection
python
from sklearn_genetic.callbacks import ConsecutiveStopping, TimerStopping
# Stop when either condition is met
ga.fit(
X_train, y_train,
callbacks=[
TimerStopping(total_seconds=BUDGET_SECONDS),
ConsecutiveStopping(generations=5, metric="fitness_best"),
]
)Key Points
- The initial batch runs even with a very short budget, so generation zero can be the last entry.
- Smaller candidate batches reduce the amount of work already in progress at the deadline. With
n_jobs=1, a single expensive candidate can still take longer than the entire budget. - Compare
ga.fit_stats_["cross_validate_calls"]across runs to see how many candidate evaluations the time window allowed. The elapsed time aroundfitalso includes refitting.
See Also
- Stop When Fitness Plateaus — stop on convergence instead
- Early Stopping and Callbacks — all available callbacks
- Resume a Stopped Search — continue from a checkpoint after stopping