Skip to content

Instantly share code, notes, and snippets.

@fedden
Created December 11, 2017 10:27
Show Gist options
  • Save fedden/977834a7984b8735d251322140f2bd93 to your computer and use it in GitHub Desktop.
Save fedden/977834a7984b8735d251322140f2bd93 to your computer and use it in GitHub Desktop.
elitism = 0.1
population_size = 500
mutation_rate = 0.1
mutation_sigma = 0.1
mutation_decay = 0.999
mutation_limit = 0.01
amount_optimisation_steps = 250
sizes = [10, 5, 6, 7, 8]
for i in range(1, 6):
print("\nproblem:", i)
ga = GA(sizes[i-1],
elitism,
population_size,
mutation_rate,
mutation_sigma,
mutation_decay,
mutation_limit)
stop = False
while not stop:
positions = ga.get_solutions()
fitnesses = []
for pos in positions:
fitness, done = knapsack_evaluate(pos, i)
if done:
stop = True
print(fitness, pos)
fitnesses.append(fitness)
if not stop:
ga.set_fitnesses(fitnesses)
best_position, best_fitness = ga.get_best()
else:
print("stopping")
#-------------------------------------------------------
# Output:
#-------------------------------------------------------
#
# problem: 1
# 309 [1 1 1 1 0 1 0 0 0 0]
#
# problem: 2
# 51 [0 1 1 1 0]
#
# problem: 3
# 150 [1 1 0 0 1 0]
#
# problem: 4
# 107 [1 0 0 1 0 0 0]
#
# problem: 5
# 900 [1 0 1 1 1 0 1 1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment