-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenetic_algo.py
97 lines (95 loc) · 2.12 KB
/
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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
import random,math
import matplotlib.pyplot as plt
chromosom = []
selected = []
avg = []
def InitialPopulation():
global chromosom
for i in range(0,10):
new_chrom = []
for j in range(0,5):
locus = random.randrange(0,2)
new_chrom.append(locus)
new_chrom.append(0)
new_chrom[5] = Fitness(new_chrom)
chromosom.append(new_chrom)
def TotalFitness(Chrom):
ftness = 0
for val in Chrom:
ftness += val[5]
return ftness
def Fitness(Chrom):
y = 0
base = 1
length = len(Chrom) - 2
while length >= 0:
if Chrom[length] == 1:
y += base
base = base*2
length -= 1
ftness = (-y*y/10)+3*y
return ftness
def CrossOver(ChromX,ChromY):
rnd1 = random.randrange(2,3)
rnd2 = random.randrange(3,5)
for i in range(rnd1,rnd2):
temp = ChromX[i]
ChromX[i] = ChromY[i]
ChromY[i] = temp
return ([ChromX,ChromY])
def Mutation(ChromX):
rnd = random.randrange(2,5)
if ChromX[rnd] == 0:
ChromX[rnd] = 1
else:
ChromX[rnd] = 0
ChromX[5] = Fitness(ChromX)
return ChromX
def RoulateWheel(chromo):
select = []
Sorted = Sort(chromo)
ftness = math.floor(TotalFitness(Sorted))
length = len(Sorted)
while len(select)<2:
rnd = random.randrange(0,ftness)
total = 0
for j in range(0,length):
total += Sorted[j][5]
if total >= rnd:
select.append(Sorted[j])
break
j += 1
return select
def Sort(Chrom):
temp = 0
length = len(Chrom)
for i in range(0,length):
for j in range(i+1,length):
if Chrom[i][5]>Chrom[j][5]:
temp = Chrom[j]
Chrom[j] = Chrom[i]
Chrom[i] = temp
return Chrom
InitialPopulation()
generation = []
for i in range(0,10):
selected = []
for j in range(0,5):
select = []
select = RoulateWheel(chromosom)
sel = CrossOver(select[0],select[1])
first = Mutation(sel[0])
second = Mutation(sel[1])
selected.append(first)
selected.append(second)
total = TotalFitness(selected)
generation.append(i+1)
avg.append(total/10)
chromosom = []
chromosom = selected
print(avg)
plt.plot(generation,avg,color='g')
plt.axis([0,12,0,30])
plt.xlabel('Generation')
plt.ylabel('Average Fitness')
plt.show()