forked from Jack1203187498/PythonAntGenetic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeneticAlgorithm.py
138 lines (121 loc) · 3.54 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import math
import random
import matplotlib.pyplot as plt
import numpy as np
from numpy import cumsum
def initpop(popsize, chromlength):
tmp = np.zeros((popsize, chromlength)).tolist()
for i in range(popsize):
for j in range(chromlength):
tmp[i][j] = round(random.random())
return tmp
def binary2decimal(pop):
tmp = np.zeros((len(pop), 1))
for i in range(len(pop)):
num = ''.join(str(int(_)) for _ in pop[i])
num = int(num, 2)
tmp[i] = num * 10.0 / 1023
return tmp
def cal_objvalue(pop):
x = binary2decimal(pop)
for i in range(len(x)):
tmp = x[i]
x[i] = 10 * math.sin(5 * tmp) + 7 * abs(tmp - 5) + 10
return x
def selection(pop, fitvalue):
px = len(pop)
py = len(pop[0])
totalfit = sum(fitvalue)
p_fitvalue = fitvalue / totalfit
p_fitvalue = cumsum(p_fitvalue)
newpop = np.zeros((px, py)).tolist()
ms = []
for i in range(px):
ms.append(random.random())
ms = sorted(ms)
fitin = 0
newin = 0
while newin <= px and fitin < px and newin < px:
if ms[newin] < p_fitvalue[fitin]:
newpop[newin][:] = pop[fitin][:]
newin += 1
else:
fitin += 1
return newpop
def crossover(pop, pc):
px = len(pop)
py = len(pop[0])
newpop = []
for i in range(0, px-1, 2):
if random.random() < pc:
cpoint = round(random.random() * py)
if cpoint == py:
cpoint -= 1
newpop.append(pop[i][0:cpoint] + pop[i+1][cpoint:py])
newpop.append(pop[i+1][0:cpoint] + pop[i][cpoint:py])
else:
newpop.append(pop[i])
newpop.append(pop[i+1])
return newpop
def mutation(pop, pm):
px = len(pop)
py = len(pop[0])
newpop = np.ones((px, py))
for i in range(px):
if(random.random() < pm):
mpoint = round(random.random() * py)
if mpoint == py:
mpoint -= 1
newpop[i] = pop[i]
if newpop[i][mpoint] == 1:
newpop[i][mpoint] = 0
else:
newpop[i][mpoint] = 1
else:
newpop[i] = pop[i]
return newpop.tolist()
def cal_function(min, max, step):
x = np.arange(min, max, step)
y = np.arange(min, max, step)
for i in range(len(x)):
tmp = x[i]
y[i] = 10 * math.sin(5 * tmp) + 7 * abs(tmp - 5) + 10
return x, y
def best(pop, fitvalue):
px = len(pop)
py = len(pop[0])
bestindividual = pop[0]
bestfit = fitvalue[0][0]
for i in range(px):
if fitvalue[i][0] > bestfit:
bestindividual = pop[i]
bestfit = fitvalue[i][0]
return [bestindividual, bestfit]
def main():
popsize = 1000
chromlength = 10
pc = 0.6
pm = 0.01
pop = initpop(popsize, chromlength)
x2 = 0
bestfit = 0
for i in range(500):
objvalue = cal_objvalue(pop)
fitvalue = objvalue
newpop = selection(pop, fitvalue)
newpop = crossover(newpop, pc)
newpop = mutation(newpop, pm)
pop = newpop
[bestindividual, bestfit] = best(pop, fitvalue)
x2 = binary2decimal([bestindividual])[0][0]
x1 = binary2decimal(pop)
y1 = cal_objvalue(pop)
if i % 499 == 0 and i != 0:
xbase, ybase = cal_function(0, 10, 0.001)
plt.plot(xbase, ybase)
plt.plot(x1, y1, '.')
plt.plot(x2, bestfit, '*')
print("The best X is --->>", str(x2))
print("The best Y is --->>", str(bestfit))
plt.show()
main()