golden_section
GoldenSection
¶
Bases: Search
A search class that performs the golden-section search on a single variable.
Golden-section search is good at finding minimal or maximal values of a unimodal function. Each search step reduces the search range by a constant factor: the inverse of golden ratio. More details are available at: https://en.wikipedia.org/wiki/Golden-section_search.
search = GoldenSection(eval_fn=lambda search_idx, n: (n - 3)**2, x_min=0, x_max=6, max_iter=10, best_mode="min")
search.fit()
print(search.get_best_parameters()) # {'param': {'n': 3, 'search_idx': 2}, 'result': {'value': 0}}
Parameters:
Name | Type | Description | Default |
---|---|---|---|
eval_fn |
Callable[[int, Union[int, float]], float]
|
Function that evaluates result given parameter. One of its arguments must be 'search_idx' which will be automatically provided by the search routine. This can help with file saving / logging during the search. The eval_fn should return a dictionary, or else the return would be wrapped inside one. The other argument should be the variable to be searched over. |
required |
x_min |
Union[int, float]
|
Lower limit (inclusive) of the search space. |
required |
x_max |
Union[int, float]
|
Upper limit (inclusive) of the search space. |
required |
max_iter |
int
|
Maximum number of iterations to run. The range at a given iteration i is 0.618**i * (x_max - x_min). Note that the eval_fn will always be evaluated twice before any iterations begin. |
required |
best_mode |
str
|
Whether maximal or minimal objective is desired. Must be either 'min' or 'max'. |
required |
optimize_field |
Optional[str]
|
the key corresponding to the target value when deciding the best. If None and multiple keys exist in result dictionary, the optimization is ambiguous therefore an error will be raised. |
None
|
integer |
bool
|
Whether the optimized variable is a discrete integer. |
True
|
best_mode |
str
|
Whether maximal or minimal fitness is desired. Must be either 'min' or 'max'. |
required |
name |
str
|
The name of the search instance. This is used for saving and loading purposes. |
'golden_section_search'
|
Raises:
Type | Description |
---|---|
AssertionError
|
If |