-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
51 lines (44 loc) · 1.05 KB
/
main.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
import matplotlib.pyplot as plt
from ypstruct import structure
import GA
# Sphere Test Function
def sphere(x):
return sum(x**2)
# Problem Definition
problem = structure()
problem.costfunc = sphere
coords=[]
city1=[]
city2=[]
with open('./data/att48.txt') as f:
for line in f.readlines():
city = line.split(' ')
coords.append((int(city[1]), int(city[2])))
problem.nvar = len(coords)
for i in range (problem.nvar):
for j in range(2):
if j==1:
city2.append(coords[i][j])
else: city1.append(coords[i][j])
problem.varmin = city2
problem.varmax = city1
# GA Parameters
params = structure()
params.maxit = 1000
params.npop = 50
params.beta = 1
params.pc = 1
params.gamma = 0.1
params.mu = 0.01
params.sigma = 0.1
# Run GA
out = GA.run(problem, params)
# Results
plt.plot(out.bestcost)
# plt.semilogy(out.bestcost)
plt.xlim(0, params.maxit)
plt.xlabel('Iterations')
plt.ylabel('Best Cost')
plt.title('Genetic Algorithm (GA)')
plt.grid(True)
plt.show()