ass 3 done!!

master
Ruben-FreddyLoafers 2025-11-05 10:50:27 +01:00
parent c9869979d3
commit 1df2ad190a
2 changed files with 5 additions and 14 deletions

View File

@ -70,8 +70,8 @@ def eval_fitness(bin_pop_values):
return fitness_arr
def select(fitness_arr):
gray_pop_copy = gray_pop.copy() # Create a copy of the population
fitness_arr_copy = fitness_arr.copy()
gray_pop_copy = gray_pop.copy() # copy of population
fitness_arr_copy = fitness_arr.copy() # copy of fitness array
selected_pop = []
while len(selected_pop) < SELECTION_SIZE:
@ -140,7 +140,7 @@ bin_pop_values = generate_random_population(POPULATION_SIZE)
print("Working...")
iteration = 0 # debugging
while not np.any((np.array(fitness_arr)) > 300): # Continue while any fitness value is > 1
while not np.any((np.array(fitness_arr)) > 200): # Continue while any fitness value is > 1
print("\nIteration: " + str(iteration)) # debugging
# Evaluate fitness

View File

@ -14,15 +14,6 @@ def gray_to_bin(gray):
num ^= mask
return format(num, '032b') # Always return 32-bit string
def bin_to_gray(binary):
"""
Convert binary to Gray code using XOR with right shift
:returns: 32-bit String
"""
num = int(binary, 2) # Convert string to integer
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 = 5.0):
"""
Convert one binary string to float parameter in range [q_min, q_max]
@ -34,7 +25,7 @@ def bin_to_param(binary, q_min = 0.0, q_max = 5.0):
return q
def plot_graph(a, b, c, d):
x = np.arange(-5., 5., 0.1)
x = np.arange(-1., 1., 0.1)
fig, ax = plt.subplots()
y_approx = a*x**3 + b*x**2 + c*x + d
@ -43,7 +34,7 @@ def plot_graph(a, b, c, d):
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_xlim(-1, 1)
ax.set_ylim(-1, 5)
plt.legend()
plt.show()