/
GeneticAlgorithm.py
84 lines (75 loc) · 1.65 KB
/
GeneticAlgorithm.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import matplotlib.pyplot as plt
import QuickSort
import BubbleSort
from Binary import Binary
from random import random
from math import floor
def generate(size):
population = list()
for val in range(size):
randPaul = floor(random() * 31)
population.append(Binary(randPaul))
return population
def f(x):
return x.toInt()**2
def sort(l):
l = QuickSort.sort(l, f)
#l = BubbleSort.sort(l, f)
return l
def mutate(l, num):
new = list()
for i in range(num):
index = floor(random() * len(l))
member = l[index]
index = floor(random() * len(member))
if member[index] == 1:
member[index] = 0
else:
member[index] = 1
new.append(member)
return new
def mixPopulation(l):
newlist = list()
olength = len(l)
for i in range(floor(len(l) / 2)):
index = floor(random() * len(l))
l1 = l.pop(index)
index = floor(random() * len(l))
l2 = l.pop(index)
newlist.extend(cross(l1.bs, l2.bs))
newlist.extend(mutate(newlist, olength - len(newlist)))
for i in range(len(newlist)):
newlist[i] = Binary(newlist[i])
return newlist
def cross(l1, l2):
if len(l1) == len(l2):
pivot = floor(random() * len(l1))
ltemp1 = l1[pivot:len(l1)]
ltemp2 = l2[pivot:len(l2)]
l1 = l1[0:pivot]
l2 = l2[0:pivot]
l1.extend(ltemp2)
l2.extend(ltemp1)
return [l1, l2]
def out(pop):
for val in pop:
val.out()
def run(times):
pop = generate(5)
top = list()
print("original population")
out(pop)
print('----------------')
for i in range(times):
pop = sort(pop)
top.append(pop[0].toInt())
pop = mixPopulation(pop)
#out(pop)
#print()
plt.plot(top)
pop = sort(pop)
top.append(pop[0].toInt())
pop[0].out()
plt.show()
if __name__ == '__main__':
run(30)