-
Notifications
You must be signed in to change notification settings - Fork 0
/
boa.py
61 lines (53 loc) · 1.48 KB
/
boa.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/python
# coding: UTF-8
from setup import *
import random
class Boa:
def __init__(self, setup):
self.setup = setup
def run(self):
self.population = init_population()
self.generation = 0
eval_population()
sort_population()
suc = setup.terminate(self.population)
while(not suc[0]):
self.generation += 1
breed()
eval_population()
sort_population()
suc = setup.terminate(self.population)
if suc[1]:
pass
else:
pass
def init_population(self):
result = []
for i in range(self.setup.pop_size):
result.append(Individual(self.setup))
return result
def eval_population(self):
for i in self.population:
self.setup.evaluate(i)
def sort_population(self):
self.population.sort(key=lambda x: x.fitness, reverse=True)
#self.population = sorted(self.population, key=(lambda x: x.fitness))
def breed(self):
bn = BayesianNetwork()
bn.estimate_edges()
bn.estimate_params()
new_pop = []
for i in range(self.setup.pop_size):
new_pop.append(bn.sample())
self.population = new_pop
s = SetupEda()
b = Boa(s)
b.population = b.init_population()
for i in b.population:
print i.fitness, i.gene
print
print
b.eval_population()
b.sort_population()
for i in b.population:
print i.fitness, i.gene