code cleanup
parent
9a61a7f1d6
commit
4de71f8850
|
|
@ -10,7 +10,6 @@ Taylor-Reihe um 0.
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import random
|
import random
|
||||||
import struct
|
|
||||||
import time
|
import time
|
||||||
import utils
|
import utils
|
||||||
|
|
||||||
|
|
@ -61,9 +60,9 @@ def eval_fitness(bin_pop_values):
|
||||||
# Create polynomial function with current parameters
|
# Create polynomial function with current parameters
|
||||||
approx = lambda x: a*x**3 + b*x**2 + c*x + d
|
approx = lambda x: a*x**3 + b*x**2 + c*x + d
|
||||||
e_func = lambda x: np.e**x
|
e_func = lambda x: np.e**x
|
||||||
quad_error = quadratic_error(e_func, approx, 3)
|
quad_error = quadratic_error(e_func, approx, 3) # the bigger the error, the worse the fitness
|
||||||
|
|
||||||
inverse_fitness = 1 / quad_error # the bigger the error, the worse the fitness
|
inverse_fitness = 1 / quad_error # using inverse to find small errors easier
|
||||||
# print("Fitness: " + str(inverse_fitness)) # debugging
|
# print("Fitness: " + str(inverse_fitness)) # debugging
|
||||||
fitness_arr.append(inverse_fitness) # save fitness
|
fitness_arr.append(inverse_fitness) # save fitness
|
||||||
|
|
||||||
|
|
@ -129,52 +128,41 @@ def mutate(population, mutation_rate):
|
||||||
# Convert back to string and update population
|
# Convert back to string and update population
|
||||||
population[random_num] = ''.join(bits) # will work because lists are passed by reference
|
population[random_num] = ''.join(bits) # will work because lists are passed by reference
|
||||||
|
|
||||||
def main():
|
bin_pop_values = generate_random_population(POPULATION_SIZE)
|
||||||
global gray_pop, bin_pop, bin_pop_params, new_pop, fitness, fitness_arr
|
|
||||||
|
|
||||||
bin_pop_values = generate_random_population(POPULATION_SIZE)
|
|
||||||
|
|
||||||
iteration = 0
|
|
||||||
print("Working...")
|
|
||||||
while not np.any((np.array(fitness_arr)) > 200): # Continue while any fitness value is > 1
|
|
||||||
# print("Iteration: " + str(iteration)) # debugging
|
|
||||||
|
|
||||||
# Evaluate fitness
|
|
||||||
fitness_arr = eval_fitness(bin_pop_values)
|
|
||||||
|
|
||||||
# Selection
|
|
||||||
new_pop = select(fitness_arr) # assigns
|
|
||||||
|
|
||||||
# Crossover
|
|
||||||
offspring = xover(gray_pop)
|
|
||||||
new_pop.extend(offspring) # Add offspring to population
|
|
||||||
|
|
||||||
# Mutation
|
|
||||||
mutate(new_pop, MUTATION_BITS)
|
|
||||||
|
|
||||||
# Update populations for next generation
|
|
||||||
gray_pop = new_pop.copy()
|
|
||||||
bin_pop_values = []
|
|
||||||
for gray_bin_string in gray_pop:
|
|
||||||
bin_str = utils.gray_to_bin(gray_bin_string)
|
|
||||||
params = [bin_str[i:i+7] for i in range(0, 31, 8)]
|
|
||||||
bin_pop_values.append(params)
|
|
||||||
|
|
||||||
# time.sleep(0.5)
|
|
||||||
|
|
||||||
iteration += 1
|
|
||||||
|
|
||||||
max_fitness_index = np.argmax(np.array(fitness_arr))
|
|
||||||
a, b, c, d = [utils.bin_to_param(param) for param in bin_pop_values[max_fitness_index]]
|
|
||||||
|
|
||||||
|
|
||||||
print("index: " + str(max_fitness_index))
|
|
||||||
print("a: " + str(a) + "; b: " + str(b) + "; c: " + str(c) + "; d: " + str(d) )
|
|
||||||
|
|
||||||
utils.plot_graph(a, b, c, d)
|
|
||||||
|
|
||||||
return 0
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
print("Working...")
|
||||||
main()
|
# iteration = 0 # debugging
|
||||||
print("found that shit")
|
while not np.any((np.array(fitness_arr)) > 200): # Continue while any fitness value is > 1
|
||||||
|
# print("Iteration: " + str(iteration)) # debugging
|
||||||
|
|
||||||
|
# Evaluate fitness
|
||||||
|
fitness_arr = eval_fitness(bin_pop_values)
|
||||||
|
|
||||||
|
# Selection
|
||||||
|
new_pop = select(fitness_arr) # assigns
|
||||||
|
|
||||||
|
# Crossover
|
||||||
|
offspring = xover(gray_pop)
|
||||||
|
new_pop.extend(offspring) # Add offspring to population
|
||||||
|
|
||||||
|
# Mutation
|
||||||
|
mutate(new_pop, MUTATION_BITS)
|
||||||
|
|
||||||
|
# Update populations for next generation
|
||||||
|
gray_pop = new_pop.copy()
|
||||||
|
bin_pop_values = []
|
||||||
|
for gray_bin_string in gray_pop:
|
||||||
|
bin_str = utils.gray_to_bin(gray_bin_string)
|
||||||
|
params = [bin_str[i:i+7] for i in range(0, 31, 8)]
|
||||||
|
bin_pop_values.append(params)
|
||||||
|
|
||||||
|
# time.sleep(0.5) # debugging
|
||||||
|
# iteration += 1 # debugging
|
||||||
|
|
||||||
|
max_fitness_index = np.argmax(np.array(fitness_arr))
|
||||||
|
a, b, c, d = [utils.bin_to_param(param) for param in bin_pop_values[max_fitness_index]]
|
||||||
|
|
||||||
|
# print("index: " + str(max_fitness_index)) # debugging
|
||||||
|
print("a: " + str(a) + "; b: " + str(b) + "; c: " + str(c) + "; d: " + str(d) )
|
||||||
|
|
||||||
|
utils.plot_graph(a, b, c, d)
|
||||||
|
|
|
||||||
|
|
@ -6,43 +6,31 @@ def gray_to_bin(gray):
|
||||||
Convert Gray code to binary, operating on the integer value directly.
|
Convert Gray code to binary, operating on the integer value directly.
|
||||||
:returns: 32-bit String
|
:returns: 32-bit String
|
||||||
"""
|
"""
|
||||||
try:
|
num = int(gray, 2) # Convert string to integer
|
||||||
num = int(gray, 2) # Convert string to integer
|
mask = num
|
||||||
mask = num
|
while mask != 0:
|
||||||
while mask != 0:
|
mask >>= 1
|
||||||
mask >>= 1
|
num ^= mask
|
||||||
num ^= mask
|
return format(num, '032b') # Always return 32-bit string
|
||||||
return format(num, '032b') # Always return 32-bit string
|
|
||||||
except ValueError as e:
|
|
||||||
print(f"Error in gray_to_bin with input: '{gray}'")
|
|
||||||
raise e
|
|
||||||
|
|
||||||
def bin_to_gray(binary):
|
def bin_to_gray(binary):
|
||||||
"""
|
"""
|
||||||
Convert binary to Gray code using XOR with right shift
|
Convert binary to Gray code using XOR with right shift
|
||||||
:returns: 32-bit String
|
:returns: 32-bit String
|
||||||
"""
|
"""
|
||||||
try:
|
num = int(binary, 2) # Convert string to integer
|
||||||
num = int(binary, 2) # Convert string to integer
|
gray = num ^ (num >> 1) # Gray code formula: G = B ^ (B >> 1)
|
||||||
gray = num ^ (num >> 1) # Gray code formula: G = B ^ (B >> 1)
|
return format(gray, '032b') # Always return 32-bit string
|
||||||
return format(gray, '032b') # Always return 32-bit string
|
|
||||||
except ValueError as e:
|
|
||||||
print(f"Error in bin_to_gray with input: '{binary}'")
|
|
||||||
raise e
|
|
||||||
|
|
||||||
def bin_to_param(binary, q_min = 0.0, q_max = 10.0):
|
def bin_to_param(binary, q_min = 0.0, q_max = 10.0):
|
||||||
"""
|
"""
|
||||||
Convert one binary string to float parameter in range [q_min, q_max]
|
Convert one binary string to float parameter in range [q_min, q_max]
|
||||||
:returns: float
|
:returns: float
|
||||||
"""
|
"""
|
||||||
try:
|
val = int(binary, 2) / 25.5 * 10 # conversion to 0.0 - 10.0 float
|
||||||
val = int(binary, 2) / 25.5 * 10 # conversion to 0.0 - 10.0 float
|
# Scale to range [q_min, q_max]
|
||||||
# Scale to range [q_min, q_max]
|
q = q_min + ((q_max - q_min) / (2**len(binary))) * val
|
||||||
q = q_min + ((q_max - q_min) / (2**len(binary))) * val
|
return q
|
||||||
return q
|
|
||||||
except ValueError as e:
|
|
||||||
print(f"Error in bin_to_param with input: '{binary}'")
|
|
||||||
raise e
|
|
||||||
|
|
||||||
def plot_graph(a, b, c, d):
|
def plot_graph(a, b, c, d):
|
||||||
x = np.arange(-5., 5., 0.1)
|
x = np.arange(-5., 5., 0.1)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue