Skip to content

Instantly share code, notes, and snippets.

@rocreguant
Created January 31, 2021 16:34
Show Gist options
  • Save rocreguant/77b82e4deccab8f531b551516b81794a to your computer and use it in GitHub Desktop.
Save rocreguant/77b82e4deccab8f531b551516b81794a to your computer and use it in GitHub Desktop.
# First step: Create the first population set
def fit_in_knapsack(objects_list, max_weight, objects_dict):
r = []
for o in objects_list:
r.append(o)
weight, value = get_current_weight_value(r, objects_dict)
if weight > max_weight:
r.pop()
return r
return r
def genesis(object_list, n_population, max_weight, objects_dict):
population_set = []
for i in range(n_population):
#Randomly generating a new solution
sol_i = object_list[np.random.choice(list(range(n_objects)), n_objects, replace=False)]
sol_i = fit_in_knapsack(sol_i, max_weight, objects_dict)
population_set.append(sol_i)
return np.array(population_set)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment