-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
46 lines (38 loc) · 1.22 KB
/
test.py
File metadata and controls
46 lines (38 loc) · 1.22 KB
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
36
37
38
39
40
41
42
43
44
45
46
from time import time, sleep
from ga.ga import GeneticAlgorithm
from ga.chromosome import Chromosome
def fitness(data):
limit = 15
items = {0: [10, 2], 1: [5, 5], 2: [1, 1], 3: [3, 2], 4: [4, 5], 5: [3, 3]}
sleep(0.001)
val = 0.0
vol = 0.0
for i in range(len(data)):
if data[i]:
val += items[i][1]
vol += items[i][0]
if vol > limit:
return 0
return val
if __name__ == '__main__':
# set chromosome shape
chromosome = Chromosome()
chromosome.add_gene('choice', [0, 1])
chromosome.add_gene('choice', [0, 1])
chromosome.add_gene('choice', [0, 1])
chromosome.add_gene('choice', [0, 1])
chromosome.add_gene('choice', [0, 1])
chromosome.add_gene('choice', [0, 1])
# make object for genetic algorithm
genetic = GeneticAlgorithm(fitness, 50, chromosome, 50, thread_count=3)
print(genetic.help)
# set operation using genetic algorithm
genetic.add_method('selection', 'roulette')
genetic.add_method('crossover', 'one_point')
genetic.add_method('mutation', 'elitist')
genetic.add_method('survivor', 'elitist')
# learning
t = time()
genetic.run()
print((time() - t) / 60)
print(genetic.population[0])