gridsearch()#
- gridsearch(func: Callable, candidates: ndarray, gridsearch_only: bool = False, label: str = 'parameter', verbose: bool = False) float | ndarray [source]#
Minimize a function by first checking a collection of candidates, then following up with a derivative-free optimization routine.
If the candidates are one-dimensional, meaning
func()
is a scalar map \(f:\RR_+\to\RR\), the optimization is carried out viascipy.optimize.minimize_scalar()
withmethod='brent'
.Otherwise (\(f:\RR_+^p\to\RR\) for some \(p > 1\)), the optimization is carried out via
scipy.optimize.minimize()
withmethod='Nelder-Mead'
.- Parameters:
- funccallable(candidate) -> float
Function to minimize.
- candidateslist of floats or ndarrays
Candidates to check before starting the optimization routine.
- gridsearch_onlybool
If
True
, stop after checking allcandidates
and do not follow up with optimization.- labelstr
Label for the types of candidates being checked.
- verbosebool
If
True
, print information at each iteration.
- Returns:
- winnerfloat or ndarray
Optimized candidate.
- Raises:
- RuntimeError
If the grid search fails because
func()
returnsnp.inf
for each candidate.
- Warns:
- OpInfWarning
1) If the
candidates
are one-dimensional and the grid search winner is the smallest or largest candidate, or 2) If the minimization fails or does not result in a solution that improves upon the grid search.
Notes
The optimization minimizes
func(x)
by varyingx
logarithmically. Unlessgridsearch_only=True
, it is assumed that all entries of the argumentx
are positive.If only one scalar candidate is given, the (one-dimensional) optimization sets the bounds to
[candidate / 100, candidate * 100]
.