diff --git a/kernel_tuner/strategies/common.py b/kernel_tuner/strategies/common.py index d01eae93..00700cd7 100644 --- a/kernel_tuner/strategies/common.py +++ b/kernel_tuner/strategies/common.py @@ -3,6 +3,7 @@ from time import perf_counter import numpy as np +import numbers from kernel_tuner import util from kernel_tuner.searchspace import Searchspace @@ -109,8 +110,15 @@ def __call__(self, x, check_restrictions=True): self.runner.last_strategy_start_time = perf_counter() # get numerical return value, taking optimization direction into account - return_value = result[self.tuning_options.objective] or sys.float_info.max - return_value = return_value if not self.tuning_options.objective_higher_is_better else -return_value + return_value = result[self.tuning_options.objective] + + if isinstance(return_value, numbers.Number): + if self.tuning_options.objective_higher_is_better: + # flip the sign if higher means better + return_value = -return_value + else: + # this is not a valid configuration, just return max + return_value = sys.float_info.max return return_value diff --git a/kernel_tuner/strategies/simulated_annealing.py b/kernel_tuner/strategies/simulated_annealing.py index dce929b7..3a89ad54 100644 --- a/kernel_tuner/strategies/simulated_annealing.py +++ b/kernel_tuner/strategies/simulated_annealing.py @@ -87,20 +87,24 @@ def tune(searchspace: Searchspace, runner, tuning_options): def acceptance_prob(old_cost, new_cost, T, tuning_options): """Annealing equation, with modifications to work towards a lower value.""" - error_val = sys.float_info.max if not tuning_options.objective_higher_is_better else -sys.float_info.max + error_val = sys.float_info.max + res = 0.0 # if start pos is not valid, always move if old_cost == error_val: - return 1.0 + res = 1.0 # if we have found a valid ps before, never move to nonvalid pos - if new_cost == error_val: - return 0.0 + elif new_cost == error_val: + res = 0.0 # always move if new cost is better - if new_cost < old_cost: - return 1.0 + elif new_cost < old_cost: + res = 1.0 # maybe move if old cost is better than new cost depending on T and random value - if tuning_options.objective_higher_is_better: - return np.exp(((new_cost-old_cost)/new_cost)/T) - return np.exp(((old_cost-new_cost)/old_cost)/T) + else: + if tuning_options.objective_higher_is_better: + res = np.exp(((new_cost-old_cost)/new_cost)/T) + else: + res = np.exp(((old_cost-new_cost)/old_cost)/T) + return res def neighbor(pos, searchspace: Searchspace):