Simulating computational intensity
As mentioned earlier, the most computationally intensive task in a genetic algorithm is often the fitness evaluation of individuals. To simulate this aspect, we will now intentionally extend the execution time of our fitness function.
This modification is implemented in the Python program, 02_one_max_busy.py
, available at https://github.com/PacktPublishing/Hands-On-Genetic-Algorithms-with-Python-Second-Edition/blob/main/chapter_13/02_one_max_busy.py.
This program is based on the previous one, with the following modifications:
- A
busy_wait()
function is added. This function exercises an empty loop for a specified duration (in seconds):def busy_wait(duration): Â Â Â Â current_time = time.time() Â Â Â Â while (time.time() < current_time + duration): Â Â Â Â Â Â Â Â pass
- The original fitness function is updated to incorporate a call to the
busy_wait()
function before calculating...