nlsq.stability.fallback.FallbackOrchestrator¶
- class nlsq.stability.fallback.FallbackOrchestrator(strategies=None, max_attempts=10, verbose=False)[source]¶
Bases:
objectOrchestrates automatic fallback strategies for robust optimization.
The orchestrator tries multiple recovery strategies when optimization fails, including alternative methods, perturbed initial guesses, relaxed tolerances, and robust loss functions.
- Parameters:
strategies (list of FallbackStrategy, optional) – List of strategies to try. If None, uses default strategies.
max_attempts (int, optional) – Maximum total attempts across all strategies. Default: 10
verbose (bool, optional) – Print progress messages. Default: False
- strategies¶
Active fallback strategies, sorted by priority
- Type:
Examples
>>> from nlsq.stability.fallback import FallbackOrchestrator >>> import numpy as np >>> >>> def model(x, a, b): ... return a * np.exp(-b * x) >>> >>> x = np.linspace(0, 10, 100) >>> y = 2.5 * np.exp(-0.5 * x) + 0.1 * np.random.randn(100) >>> >>> orchestrator = FallbackOrchestrator(verbose=True) >>> result = orchestrator.fit_with_fallback( ... model, x, y, p0=[1, 0.1] # Deliberately poor p0 ... ) >>> >>> if result.fallback_strategy_used: ... print(f"Recovered using: {result.fallback_strategy_used}")
- DEFAULT_STRATEGIES: ClassVar[list[StrategyFactory]] = [<class 'nlsq.stability.fallback.AlternativeMethodStrategy'>, <class 'nlsq.stability.fallback.PerturbInitialGuessStrategy'>, <class 'nlsq.stability.fallback.AdjustTolerancesStrategy'>, <class 'nlsq.stability.fallback.AddParameterBoundsStrategy'>, <class 'nlsq.stability.fallback.UseRobustLossStrategy'>, <class 'nlsq.stability.fallback.RescaleProblemStrategy'>]¶
- __init__(strategies=None, max_attempts=10, verbose=False)[source]¶
Initialize fallback orchestrator.
- fit_with_fallback(f, xdata, ydata, **kwargs)[source]¶
Attempt curve fit with automatic fallback on failure.
- Parameters:
f (callable) – Model function
xdata (array_like) – Independent variable data
ydata (array_like) – Dependent variable data
**kwargs – Additional arguments passed to curve_fit
- Returns:
result – Optimization result with fallback metadata
- Return type:
- Raises:
RuntimeError – If all fallback strategies fail