further debugging
parent
4028d11de1
commit
c865008f0e
|
|
@ -63,7 +63,7 @@ def eval_fitness(bin_pop_values):
|
|||
# Create polynomial function with current parameters
|
||||
approx = lambda x: a*x**3 + b*x**2 + c*x + d
|
||||
fitness = quadratic_error(e_func, approx, 6)
|
||||
print(fitness) # debugging
|
||||
print("Fitness: " + str(fitness)) # debugging
|
||||
|
||||
fitness_arr.append(fitness) # save fitness
|
||||
# save params # already saved in grey_pop
|
||||
|
|
@ -72,7 +72,7 @@ def eval_fitness(bin_pop_values):
|
|||
|
||||
def select(population, fitness_arr):
|
||||
sum_of_fitness = sum(fitness_arr)
|
||||
while len(population) < SELECTION_SIZE:
|
||||
while len(population) < SELECTION_SIZE:
|
||||
# Roulette logic
|
||||
roulette_num = random.random()
|
||||
is_chosen = False
|
||||
|
|
@ -126,33 +126,45 @@ def mutate(population, mutation_rate):
|
|||
|
||||
# Convert back to string and update population
|
||||
population[random_num] = ''.join(bits) # will work because lists are passed by reference
|
||||
|
||||
|
||||
def main():
|
||||
global grey_pop, bin_pop, bin_pop_params, new_pop, fitness
|
||||
|
||||
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:
|
||||
# Evaluate fitness
|
||||
fitness_arr = eval_fitness(bin_pop_values)
|
||||
print("Iteration: " + str(iteration))
|
||||
|
||||
# Evaluate fitness
|
||||
fitness_arr = eval_fitness(bin_pop_values)
|
||||
|
||||
# Selection
|
||||
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)
|
||||
new_pop.extend(offspring) # .extend needed
|
||||
new_pop.extend(offspring) # Add offspring to population
|
||||
|
||||
# Mutation
|
||||
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
|
||||
grey_pop = new_pop.copy()
|
||||
bin_pop_values = []
|
||||
for grey_bin_string in grey_pop:
|
||||
bin_str = utils.grey_to_bin(grey_bin_string)
|
||||
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
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
|||
Loading…
Reference in New Issue