forked from kiecodes/genetic-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genetic_algo.py
35 lines (27 loc) · 875 Bytes
/
genetic_algo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from functools import partial
from problems import knapsack
from algorithms.bruteforce import bruteforce
from algorithms import genetic
from utils.analyze import timer
things = knapsack.generate_things(22)
things = knapsack.second_example
weight_limit = 3000
print("Weight Limit: %dkg" % weight_limit)
print("")
print("BRUTEFORCE")
print("----------")
with timer():
result = bruteforce(things, weight_limit)
knapsack.print_stats(result[1])
print("")
print("GENETIC ALGORITHM")
print("----------")
with timer():
population, generations = genetic.run_evolution(
populate_func=partial(genetic.generate_population, size=10, genome_length=len(things)),
fitness_func=partial(knapsack.fitness, things=things, weight_limit=weight_limit),
fitness_limit=result[0],
generation_limit=100
)
sack = knapsack.from_genome(population[0], things)
knapsack.print_stats(sack)