Skip to content

Instantly share code, notes, and snippets.

@rocreguant
Created January 31, 2021 16:36
Show Gist options
  • Save rocreguant/e239834877c61b827a665c05b4aebc43 to your computer and use it in GitHub Desktop.
Save rocreguant/e239834877c61b827a665c05b4aebc43 to your computer and use it in GitHub Desktop.
def mutate_offspring(offspring, max_weight, object_list, objects_dict):
for mutation_number in range(int(len(offspring)*mutation_rate)):
a = np.random.randint(0,len(object_list))
b = np.random.randint(0,len(offspring))
offspring.insert(b, object_list[a])
offspring = fit_in_knapsack(offspring, max_weight, objects_dict)
return offspring
def mutate_population(new_population_set, max_weight, object_list, objects_dict):
mutated_pop = []
for offspring in new_population_set:
mutated_pop.append(mutate_offspring(offspring, max_weight, object_list, objects_dict))
return mutated_pop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment