I have a loss function that opens an external .exe file for calculation, and running this executable file usually takes around 2 or 3 minutes. It works very well, but When I want to pass this function to the Genetic Algorithm for optimization, I face this error:
AssertionError: After 10.0 seconds delay func_timeout: the given function does not provide any output.
So it obviously says that GA waited 10 sec and did not receive anything so bye-bye… Since the function needs more time to return the result (loss), it will not continue.
Is there any way to set time_out
to more than 10.0 sec? I couldn’t find the option in algorithm_param or ga_model itself. These are my parameters:
from geneticalgorithm import geneticalgorithm as ga algorithm_param = {'max_num_iteration': None, 'population_size':100, 'mutation_probability':0.1, 'elit_ratio': 0.01, 'crossover_probability': 0.5, 'parents_portion': 0.3, 'crossover_type':'uniform', 'max_iteration_without_improv':None} ga_model = ga(function=loss_fn, dimension=(2), variable_type='real', variable_boundaries=varbound, algorithm_parameters=algorithm_param) ga_model.run()
I searched here and other forums, but almost all the related questions are for actually limiting a function to a particular time (manually setting a time out for a process), not the opposite thing; I want to remove the restriction.
Advertisement
Answer
From the geneticalgorithm
library Github page:
Function timeout
geneticalgorithm is designed such that if the given function does not provide any output before timeout (the default value is 10 seconds), the algorithm would be terminated and raise the appropriate error. In such a case make sure the given function works correctly (i.e. there is no infinite loop in the given function). Also if the given function takes more than 10 seconds to complete the work make sure to increase function_timeout in arguments.
From the Arguments section:
@param function_timeout – if the given function does not provide output before function_timeout (unit is seconds) the algorithm raise error. For example, when there is an infinite loop in the given function.
So, if you want a time out of, say, 20 sec, you should change your ga
function to:
ga_model = ga(function=loss_fn, dimension=(2), variable_type='real', variable_boundaries=varbound, algorithm_parameters=algorithm_param, function_timeout = 20) # <- added