|
| 1 | +"""Strategy that enables the use of pyATF strategies.""" |
| 2 | + |
| 3 | +from kernel_tuner.searchspace import Searchspace |
| 4 | +from kernel_tuner.strategies import common |
| 5 | +from kernel_tuner.strategies.common import CostFunc, setup_method_arguments, setup_method_options |
| 6 | +from kernel_tuner.util import StopCriterionReached |
| 7 | + |
| 8 | +supported_methods = ["Nelder-Mead", "Powell", "CG", "BFGS", "L-BFGS-B", "TNC", "COBYLA", "SLSQP"] |
| 9 | + |
| 10 | +_options = dict(method=(f"Local optimization algorithm to use, choose any from {supported_methods}", "L-BFGS-B"), |
| 11 | + T=("Temperature parameter for the accept or reject criterion", 1.0)) |
| 12 | + |
| 13 | +def tune(searchspace: Searchspace, runner, tuning_options): |
| 14 | + method, T = common.get_options(tuning_options.strategy_options, _options) |
| 15 | + |
| 16 | + # scale variables in x to make 'eps' relevant for multiple variables |
| 17 | + cost_func = CostFunc(searchspace, tuning_options, runner, scaling=True) |
| 18 | + |
| 19 | + bounds, x0, eps = cost_func.get_bounds_x0_eps() |
| 20 | + |
| 21 | + kwargs = setup_method_arguments(method, bounds) |
| 22 | + options = setup_method_options(method, tuning_options) |
| 23 | + kwargs['options'] = options |
| 24 | + |
| 25 | + |
| 26 | + minimizer_kwargs = dict(**kwargs) |
| 27 | + minimizer_kwargs["method"] = method |
| 28 | + |
| 29 | + opt_result = None |
| 30 | + try: |
| 31 | + opt_result = scipy.optimize.basinhopping(cost_func, x0, T=T, stepsize=eps, |
| 32 | + minimizer_kwargs=minimizer_kwargs, disp=tuning_options.verbose) |
| 33 | + except StopCriterionReached as e: |
| 34 | + if tuning_options.verbose: |
| 35 | + print(e) |
| 36 | + |
| 37 | + if opt_result and tuning_options.verbose: |
| 38 | + print(opt_result.message) |
| 39 | + |
| 40 | + return cost_func.results |
| 41 | + |
| 42 | + |
| 43 | +tune.__doc__ = common.get_strategy_docstring("basin hopping", _options) |
0 commit comments