Implement population selection
This commit is contained in:
parent
c450d65870
commit
ccf3a18a59
|
@ -148,10 +148,10 @@ def mutate(population, n, probability=0.001):
|
||||||
return population
|
return population
|
||||||
|
|
||||||
|
|
||||||
def tournament_selection(population):
|
def tournament_selection(m, population):
|
||||||
individuals = population.sample(n=2)
|
individuals = [population[randint(m)] for _ in range(2)]
|
||||||
best_index = population["distance"].idxmax()
|
best_index = population.index(max(population, key=lambda x: all(x.fitness)))
|
||||||
return individuals.iloc[best_index]
|
return individuals[best_index]
|
||||||
|
|
||||||
|
|
||||||
def generational_replacement(previous_population, current_population):
|
def generational_replacement(previous_population, current_population):
|
||||||
|
@ -200,12 +200,20 @@ def evaluate_population(population, data, cores=4):
|
||||||
return evaluated_population
|
return evaluated_population
|
||||||
|
|
||||||
|
|
||||||
|
def select_new_population(population, n, m, mode):
|
||||||
|
if mode == "generational":
|
||||||
|
parents = [tournament_selection(m, population) for _ in range(n)]
|
||||||
|
else:
|
||||||
|
parents = [tournament_selection(m, population) for _ in range(2)]
|
||||||
|
return parents
|
||||||
|
|
||||||
|
|
||||||
def genetic_algorithm(n, m, data, mode, max_iterations=100000):
|
def genetic_algorithm(n, m, data, mode, max_iterations=100000):
|
||||||
population = [generate_individual(n, m, data) for _ in range(n)]
|
population = [generate_individual(n, m, data) for _ in range(n)]
|
||||||
population = evaluate_population(population, data)
|
population = evaluate_population(population, data)
|
||||||
for _ in range(max_iterations):
|
for _ in range(max_iterations):
|
||||||
pass
|
parents = select_new_population(population, n, m, mode)
|
||||||
|
|
||||||
|
|
||||||
n, m, data = parse_file("data/GKD-c_11_n500_m50.txt")
|
n, m, data = parse_file("data/GKD-c_11_n500_m50.txt")
|
||||||
genetic_algorithm(n=10, m=5, data=data, mode="")
|
genetic_algorithm(n=10, m=5, data=data, mode="generational", max_iterations=1)
|
||||||
|
|
Loading…
Reference in New Issue