2023-04-12 11:47:55 +02:00
|
|
|
"""
|
2023-04-12 21:21:02 +02:00
|
|
|
Program that optimizes Rastrigin function: file_ (x_point_value, y_point_value) =
|
|
|
|
|
20 + (x_point_value^2 - 10cos(2πx)) + (y_point_value^2 - 10 cos(2πy)).
|
2023-04-12 11:47:55 +02:00
|
|
|
Using Evolutionary Strategy (μ, λ).
|
|
|
|
|
"""
|
2023-04-12 18:16:46 +02:00
|
|
|
import sys
|
2023-04-16 00:09:18 +02:00
|
|
|
import os
|
2023-04-12 19:21:12 +02:00
|
|
|
import time
|
2023-04-12 21:21:02 +02:00
|
|
|
import tempfile
|
2023-04-16 04:29:00 +02:00
|
|
|
# run pylint with:
|
|
|
|
|
# pylint --generated-members=cv2.* .\main.py
|
2023-04-16 00:09:18 +02:00
|
|
|
import cv2
|
2023-04-12 21:21:02 +02:00
|
|
|
import matplotlib.pyplot as plt
|
2023-04-12 11:47:55 +02:00
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def rastrigin(x_argument, y_argument):
|
|
|
|
|
""" Define the Rastrigin function """
|
|
|
|
|
return 20 + x_argument**2 - 10 * np.cos(2 * np.pi * x_argument) + \
|
|
|
|
|
y_argument**2 - 10 * np.cos(2 * np.pi * y_argument)
|
|
|
|
|
|
|
|
|
|
|
2023-04-16 04:29:00 +02:00
|
|
|
def generate(population,
|
2023-04-12 18:27:17 +02:00
|
|
|
number_of_parents=5,
|
|
|
|
|
size_of_population=20,
|
|
|
|
|
mutation_strength=0.1,
|
|
|
|
|
):
|
|
|
|
|
""" Run single generation """
|
|
|
|
|
# Evaluate the fitness of each individual
|
2023-04-12 21:21:02 +02:00
|
|
|
fitness = np.array([rastrigin(x_point_value, y_point_value)
|
|
|
|
|
for x_point_value, y_point_value in population])
|
2023-04-12 18:27:17 +02:00
|
|
|
|
|
|
|
|
# Select the top number_of_parents individuals
|
|
|
|
|
parents = population[np.argsort(fitness)[:number_of_parents]]
|
|
|
|
|
|
|
|
|
|
# Generate the next generation of lambda individuals by recombination
|
2023-04-16 04:29:00 +02:00
|
|
|
children = np.concatenate(
|
|
|
|
|
[np.random.permutation(parents) for i in range((size_of_population//number_of_parents)+1)])
|
|
|
|
|
children = children[:size_of_population]
|
2023-04-12 18:27:17 +02:00
|
|
|
|
|
|
|
|
# Add mutation to the children
|
|
|
|
|
mutation = np.random.normal(
|
|
|
|
|
loc=0, scale=mutation_strength, size=(
|
|
|
|
|
size_of_population, 2))
|
|
|
|
|
population = children + mutation
|
|
|
|
|
return fitness, population
|
|
|
|
|
|
|
|
|
|
|
2023-04-12 18:16:46 +02:00
|
|
|
def evolution_strategy(
|
|
|
|
|
number_of_parents=5,
|
|
|
|
|
size_of_population=20,
|
|
|
|
|
mutation_strength=0.1,
|
2023-04-16 04:29:00 +02:00
|
|
|
number_of_generations=123,
|
|
|
|
|
min_max=(-5.12, 5.12),
|
2023-04-12 18:16:46 +02:00
|
|
|
):
|
2023-04-12 11:47:55 +02:00
|
|
|
""" Define the Evolutionary Strategy (μ, λ) algorithm """
|
|
|
|
|
# Initialize the population
|
2023-04-16 04:29:00 +02:00
|
|
|
number_of_outputs = 7
|
2023-04-12 18:16:46 +02:00
|
|
|
population = np.random.uniform(
|
|
|
|
|
low=min_max[0], high=min_max[1], size=(
|
|
|
|
|
size_of_population, 2))
|
2023-04-12 11:47:55 +02:00
|
|
|
|
2023-04-16 04:29:00 +02:00
|
|
|
output(population, 0)
|
|
|
|
|
|
|
|
|
|
number_of_outputs = min([number_of_outputs-1, number_of_generations])
|
|
|
|
|
|
|
|
|
|
# Iterate until we reach max number of generate and terminate
|
|
|
|
|
for generation_number in range(1, number_of_generations+1):
|
2023-04-12 18:27:17 +02:00
|
|
|
fitness, population = generate(
|
|
|
|
|
population,
|
|
|
|
|
number_of_parents,
|
|
|
|
|
size_of_population,
|
|
|
|
|
mutation_strength)
|
2023-04-16 04:29:00 +02:00
|
|
|
step = number_of_generations//number_of_outputs \
|
|
|
|
|
if number_of_generations % number_of_outputs == 0 \
|
|
|
|
|
else number_of_generations//(number_of_outputs-1)
|
|
|
|
|
offset = number_of_generations % step
|
|
|
|
|
if (generation_number - offset) % step == 0:
|
|
|
|
|
output(population, generation_number)
|
2023-04-12 11:47:55 +02:00
|
|
|
|
|
|
|
|
# Evaluate the fitness of the final population
|
2023-04-12 21:21:02 +02:00
|
|
|
fitness = np.array([rastrigin(x_point_value, y_point_value)
|
|
|
|
|
for x_point_value, y_point_value in population])
|
2023-04-12 11:47:55 +02:00
|
|
|
|
|
|
|
|
# Return the best individual found
|
|
|
|
|
best_idx = np.argmin(fitness)
|
2023-04-12 21:21:02 +02:00
|
|
|
return population[best_idx], fitness[best_idx], population
|
2023-04-12 11:47:55 +02:00
|
|
|
|
|
|
|
|
|
2023-04-12 18:16:46 +02:00
|
|
|
def print_help():
|
|
|
|
|
""" Print program functionality and how to access it """
|
|
|
|
|
print("""
|
2023-04-12 21:21:02 +02:00
|
|
|
python main.py - Default functionality optimizing Rastrigin function file_ (x_point_value, y_point_value) =
|
|
|
|
|
20 + (x_point_value^2 - 10cos(2πx)) + (y_point_value^2 - 10 cos(2πy))
|
2023-04-12 18:16:46 +02:00
|
|
|
using Evolutionary Strategy (μ, λ), using only default values
|
|
|
|
|
Default values:
|
|
|
|
|
number_of_parents=5,
|
|
|
|
|
size_of_population=20,
|
2023-04-12 20:06:25 +02:00
|
|
|
mutation_strength=0.1,
|
|
|
|
|
number_of_generations=100,
|
2023-04-12 18:16:46 +02:00
|
|
|
min_value=-5.12,
|
|
|
|
|
max_value=5.12
|
|
|
|
|
|
|
|
|
|
python main.py -h --help print this prompt
|
|
|
|
|
Any of the default values an be changed using arguments:
|
|
|
|
|
-nop --number_of_parents [number]
|
|
|
|
|
-sop --size_of_population [number]
|
|
|
|
|
-ms --mutation_strength [number]
|
2023-04-12 18:27:17 +02:00
|
|
|
-nog --number_of_generations [number]
|
2023-04-12 18:16:46 +02:00
|
|
|
-min --min_value [number]
|
|
|
|
|
-max --max_value [number]
|
|
|
|
|
Those arguments can be given in any order and any argument which was not entered will be replaced with default value,
|
|
|
|
|
exemplary use:
|
|
|
|
|
python main.py -nop 5 -sop 20 -s 0.1 -i 100 -min -5.12 -max 5.12
|
|
|
|
|
""")
|
|
|
|
|
|
|
|
|
|
|
2023-04-16 04:29:00 +02:00
|
|
|
def get_output_bounds(x_data, y_data):
|
|
|
|
|
"""Get x and y output limits for pyplot"""
|
|
|
|
|
# min_size = 0.2
|
|
|
|
|
min_output_size = ARGUMENTS["mutation_strength"]*10
|
|
|
|
|
|
|
|
|
|
xmin = min(x_data)
|
|
|
|
|
xmax = max(x_data)
|
|
|
|
|
ymin = min(y_data)
|
|
|
|
|
ymax = max(y_data)
|
|
|
|
|
x_diff = xmax - xmin
|
|
|
|
|
y_diff = ymax - ymin
|
|
|
|
|
|
|
|
|
|
if min_output_size is None:
|
|
|
|
|
min_output_size = max(x_diff, y_diff)
|
|
|
|
|
|
|
|
|
|
margin = max(x_diff, y_diff)/5
|
|
|
|
|
|
|
|
|
|
if x_diff < min_output_size:
|
|
|
|
|
xmax += (min_output_size - x_diff)/2
|
|
|
|
|
xmin -= (min_output_size - x_diff)/2
|
|
|
|
|
if y_diff < min_output_size:
|
|
|
|
|
ymax += (min_output_size - y_diff)/2
|
|
|
|
|
ymin -= (min_output_size - y_diff)/2
|
|
|
|
|
x_bounds = [xmin-margin, xmax+margin]
|
|
|
|
|
y_bounds = [ymin-margin, ymax+margin]
|
|
|
|
|
return x_bounds, y_bounds
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def output(population_output, generation_number):
|
2023-04-12 21:21:02 +02:00
|
|
|
""" Draw result of our function """
|
2023-04-16 04:29:00 +02:00
|
|
|
|
2023-04-12 21:21:02 +02:00
|
|
|
# define the visualization params
|
2023-04-16 04:29:00 +02:00
|
|
|
colors = np.random.rand(len(population_output))
|
2023-04-12 21:21:02 +02:00
|
|
|
|
2023-04-16 00:09:18 +02:00
|
|
|
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as file_:
|
2023-04-12 21:21:02 +02:00
|
|
|
# iterate over the optimization steps
|
|
|
|
|
# generate random 2D data - replace it with the results from your
|
|
|
|
|
# algorithm
|
|
|
|
|
x_data = []
|
|
|
|
|
y_data = []
|
|
|
|
|
for x_point_value, y_point_value in population_output:
|
|
|
|
|
x_data.append(x_point_value)
|
|
|
|
|
y_data.append(y_point_value)
|
2023-04-16 04:29:00 +02:00
|
|
|
|
|
|
|
|
x_lim, y_lim = get_output_bounds(x_data, y_data)
|
|
|
|
|
|
2023-04-12 21:21:02 +02:00
|
|
|
# plot the data
|
|
|
|
|
plt.cla()
|
|
|
|
|
plt.figure()
|
|
|
|
|
plt.scatter(x_data, y_data, c=colors, alpha=0.5)
|
2023-04-16 04:29:00 +02:00
|
|
|
plt.xlim(x_lim)
|
|
|
|
|
plt.ylim(y_lim)
|
2023-04-12 21:21:02 +02:00
|
|
|
plt.savefig(file_.name)
|
|
|
|
|
|
|
|
|
|
# read image
|
|
|
|
|
image = cv2.imread(file_.name)
|
|
|
|
|
|
|
|
|
|
# show the image, provide window name first
|
2023-04-16 04:29:00 +02:00
|
|
|
cv2.imshow(f"Generation {generation_number}", image)
|
2023-04-12 21:21:02 +02:00
|
|
|
|
|
|
|
|
# add wait key. window waits until user presses a key and quits if
|
|
|
|
|
# the key is 'q'
|
|
|
|
|
if cv2.waitKey(0) == 113:
|
|
|
|
|
# and finally destroy/close all open windows
|
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
|
|
cv2.destroyAllWindows()
|
|
|
|
|
|
2023-04-16 04:29:00 +02:00
|
|
|
file_.close()
|
|
|
|
|
os.unlink(file_.name)
|
2023-04-16 00:09:18 +02:00
|
|
|
|
2023-04-12 21:21:02 +02:00
|
|
|
|
2023-04-12 18:16:46 +02:00
|
|
|
def user_input():
|
|
|
|
|
""" Handle user terminal arguments"""
|
|
|
|
|
arguments = {
|
|
|
|
|
"number_of_parents": 5,
|
|
|
|
|
"size_of_population": 20,
|
2023-04-12 18:27:17 +02:00
|
|
|
"mutation_strength": 0.1,
|
|
|
|
|
"number_of_generations": 100,
|
2023-04-12 18:16:46 +02:00
|
|
|
"min": -5.12,
|
|
|
|
|
"max": 5.12}
|
2023-04-16 04:29:00 +02:00
|
|
|
for index, argument in enumerate(sys.argv):
|
2023-04-12 18:16:46 +02:00
|
|
|
if argument in ('-h', '--help'):
|
|
|
|
|
print_help()
|
|
|
|
|
sys.exit()
|
|
|
|
|
if argument in ('-nop', '--number_of_parents'):
|
2023-04-16 04:29:00 +02:00
|
|
|
arguments["number_of_parents"] = int(sys.argv[index+1])
|
2023-04-12 18:16:46 +02:00
|
|
|
if argument in ('-sop', '--size_of_population'):
|
2023-04-16 04:29:00 +02:00
|
|
|
arguments["size_of_population"] = int(sys.argv[index+1])
|
2023-04-12 18:16:46 +02:00
|
|
|
if argument in ('-ms', '--mutation_strength'):
|
2023-04-16 04:29:00 +02:00
|
|
|
arguments["mutation_strength"] = float(sys.argv[index+1])
|
2023-04-12 18:27:17 +02:00
|
|
|
if argument in ('-nog', '--number_of_generations'):
|
2023-04-16 04:29:00 +02:00
|
|
|
arguments["number_of_generations"] = int(sys.argv[index+1])
|
2023-04-12 18:16:46 +02:00
|
|
|
if argument in ('-min', '--min_value'):
|
2023-04-16 04:29:00 +02:00
|
|
|
arguments["min"] = float(sys.argv[index+1])
|
2023-04-12 18:16:46 +02:00
|
|
|
if argument in ('-max', '--max_value'):
|
2023-04-16 04:29:00 +02:00
|
|
|
arguments["max"] = float(sys.argv[index+1])
|
2023-04-12 18:16:46 +02:00
|
|
|
|
|
|
|
|
return arguments
|
|
|
|
|
|
|
|
|
|
|
2023-04-12 11:47:55 +02:00
|
|
|
# Ran first in the code
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
# Run the Evolutionary Strategy algorithm
|
2023-04-12 18:16:46 +02:00
|
|
|
ARGUMENTS = user_input()
|
2023-04-12 19:21:12 +02:00
|
|
|
start_time = time.perf_counter()
|
2023-04-12 21:21:02 +02:00
|
|
|
best_individual, best_fitness, output_population = evolution_strategy(
|
2023-04-12 18:16:46 +02:00
|
|
|
ARGUMENTS["number_of_parents"],
|
|
|
|
|
ARGUMENTS["size_of_population"],
|
|
|
|
|
ARGUMENTS["mutation_strength"],
|
2023-04-12 18:27:17 +02:00
|
|
|
ARGUMENTS["number_of_generations"],
|
2023-04-12 18:16:46 +02:00
|
|
|
(ARGUMENTS["min"], ARGUMENTS["max"]))
|
2023-04-12 19:21:12 +02:00
|
|
|
end_time = time.perf_counter()
|
|
|
|
|
total_generation_time = end_time - start_time
|
|
|
|
|
time_per_generation = total_generation_time / \
|
|
|
|
|
ARGUMENTS["number_of_generations"]
|
2023-04-12 11:47:55 +02:00
|
|
|
|
|
|
|
|
print("Best individual found:", best_individual)
|
|
|
|
|
print("Best fitness found:", best_fitness)
|
2023-04-12 19:21:12 +02:00
|
|
|
print("total_generation_time: ", total_generation_time)
|
|
|
|
|
print("time_per_generation: ", time_per_generation)
|