Skip to content

Instantly share code, notes, and snippets.

@NicolleLouis
Created November 27, 2017 17:17
Show Gist options
  • Save NicolleLouis/d4987f73597a47c6dce0ba9c21218807 to your computer and use it in GitHub Desktop.
Save NicolleLouis/d4987f73597a47c6dce0ba9c21218807 to your computer and use it in GitHub Desktop.
Reproduction Knapsack
def select_breeders(population_sorted, size_of_population):
result = []
best_individuals = size_of_population / 5
lucky_few = size_of_population / 5
for i in range(best_individuals):
result.append(population_sorted[i])
for i in range(lucky_few):
result.append(random.choice(population_sorted))
random.shuffle(result)
return result
def create_child(individual1, individual2):
result = []
for i in range(len(individual1)):
if (100 * random.random() < 50):
result.append(individual1[i])
else:
result.append(individual2[i])
return result
def create_children(breeders, number_of_child):
result = []
for i in range(len(breeders) / 2):
for j in range(number_of_child):
result.append(create_child(breeders[i], breeders[len(breeders) - 1 - i]))
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment