implemented taylor stuff
parent
4de71f8850
commit
cf9131b760
|
|
@ -63,7 +63,7 @@ def eval_fitness(bin_pop_values):
|
|||
quad_error = quadratic_error(e_func, approx, 3) # 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
|
||||
|
||||
return fitness_arr
|
||||
|
|
@ -131,9 +131,9 @@ def mutate(population, mutation_rate):
|
|||
bin_pop_values = generate_random_population(POPULATION_SIZE)
|
||||
|
||||
print("Working...")
|
||||
# iteration = 0 # debugging
|
||||
iteration = 0 # debugging
|
||||
while not np.any((np.array(fitness_arr)) > 200): # Continue while any fitness value is > 1
|
||||
# print("Iteration: " + str(iteration)) # debugging
|
||||
print("Iteration: " + str(iteration)) # debugging
|
||||
|
||||
# Evaluate fitness
|
||||
fitness_arr = eval_fitness(bin_pop_values)
|
||||
|
|
@ -157,7 +157,7 @@ while not np.any((np.array(fitness_arr)) > 200): # Continue while any fitness v
|
|||
bin_pop_values.append(params)
|
||||
|
||||
# time.sleep(0.5) # debugging
|
||||
# iteration += 1 # 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]]
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
import scipy.interpolate as interpolate
|
||||
|
||||
def gray_to_bin(gray):
|
||||
"""
|
||||
|
|
@ -22,12 +23,12 @@ def bin_to_gray(binary):
|
|||
gray = num ^ (num >> 1) # Gray code formula: G = B ^ (B >> 1)
|
||||
return format(gray, '032b') # Always return 32-bit string
|
||||
|
||||
def bin_to_param(binary, q_min = 0.0, q_max = 10.0):
|
||||
def bin_to_param(binary, q_min = 0.0, q_max = 8.0):
|
||||
"""
|
||||
Convert one binary string to float parameter in range [q_min, q_max]
|
||||
:returns: float
|
||||
"""
|
||||
val = int(binary, 2) / 25.5 * 10 # conversion to 0.0 - 10.0 float
|
||||
val = int(binary, 2) / 25.5 * q_max # conversion to 0.0 - 10.0 float
|
||||
# Scale to range [q_min, q_max]
|
||||
q = q_min + ((q_max - q_min) / (2**len(binary))) * val
|
||||
return q
|
||||
|
|
@ -38,7 +39,9 @@ def plot_graph(a, b, c, d):
|
|||
fig, ax = plt.subplots()
|
||||
y_approx = a*x**3 + b*x**2 + c*x + d
|
||||
y_exact = np.e**x
|
||||
y_taylor = interpolate.approximate_taylor_polynomial(np.exp, 0, 3, 1)(x)
|
||||
ax.plot(x, y_approx, label='approx. func.')
|
||||
ax.plot(x, y_taylor, label='taylor')
|
||||
ax.plot(x, y_exact, label='e^x')
|
||||
ax.set_xlim(-5, 5)
|
||||
ax.set_ylim(-1, 5)
|
||||
|
|
|
|||
Loading…
Reference in New Issue