further debugging

master
Ruben-FreddyLoafers 2025-10-20 16:34:08 +02:00
parent 4028d11de1
commit c865008f0e
1 changed files with 23 additions and 11 deletions

View File

@ -63,7 +63,7 @@ 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
fitness = quadratic_error(e_func, approx, 6) fitness = quadratic_error(e_func, approx, 6)
print(fitness) # debugging print("Fitness: " + str(fitness)) # debugging
fitness_arr.append(fitness) # save fitness fitness_arr.append(fitness) # save fitness
# save params # already saved in grey_pop # save params # already saved in grey_pop
@ -72,7 +72,7 @@ def eval_fitness(bin_pop_values):
def select(population, fitness_arr): def select(population, fitness_arr):
sum_of_fitness = sum(fitness_arr) sum_of_fitness = sum(fitness_arr)
while len(population) < SELECTION_SIZE: while len(population) < SELECTION_SIZE:
# Roulette logic # Roulette logic
roulette_num = random.random() roulette_num = random.random()
is_chosen = False is_chosen = False
@ -126,33 +126,45 @@ 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(): def main():
global grey_pop, bin_pop, bin_pop_params, new_pop, fitness
bin_pop_values = generate_random_population(POPULATION_SIZE) bin_pop_values = generate_random_population(POPULATION_SIZE)
print(bin_pop_values) new_pop = grey_pop.copy() # Make a copy of the populated grey_pop
iteration = 0
while fitness > 0.01: while fitness > 0.01:
# Evaluate fitness print("Iteration: " + str(iteration))
fitness_arr = eval_fitness(bin_pop_values)
# Evaluate fitness
fitness_arr = eval_fitness(bin_pop_values)
# Selection # Selection
new_pop = select(new_pop, fitness_arr) # Alters new_pop new_pop = select(new_pop, fitness_arr) # Alters new_pop
print(new_pop)
time.sleep(1)
"""# Crossover # Crossover
offspring = xover(new_pop, XOVER_PAIR_SIZE) offspring = xover(new_pop, XOVER_PAIR_SIZE)
new_pop.extend(offspring) # .extend needed new_pop.extend(offspring) # Add offspring to population
# Mutation # Mutation
mutate(new_pop, MUTATION_BITS) mutate(new_pop, MUTATION_BITS)
# Ensure population size stays constant
new_pop = new_pop[(len(new_pop) - POPULATION_SIZE):len(new_pop)]
# Update populations for next generation # Update populations for next generation
grey_pop = new_pop.copy() grey_pop = new_pop.copy()
bin_pop_values = [] bin_pop_values = []
for grey_bin_string in grey_pop: for grey_bin_string in grey_pop:
bin_str = utils.grey_to_bin(grey_bin_string) bin_str = utils.grey_to_bin(grey_bin_string)
params = [bin_str[i:i+7] for i in range(0, 31, 8)] params = [bin_str[i:i+7] for i in range(0, 31, 8)]
bin_pop_values.append(params)""" bin_pop_values.append(params)
# print(new_pop)
time.sleep(1.0)
iteration += 1
return 0 return 0
if __name__ == "__main__": if __name__ == "__main__":