-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
40 lines (30 loc) · 1.08 KB
/
test.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
from heuristic_algorithm import GeneticAlgorithm, PSO, HillClimbing, Annealing
from test_function import Ackley, Rastrigin, CrossIT, Sphere
import math
import time
import matplotlib.pyplot as plt
from util import AnimatedScatter
def eval(algorithm, func, boundary, repeat=1, **kwargs):
start = time.time()
for _ in range(repeat):
alg = algorithm(func, boundary, **kwargs)
alg.fit()
history = alg.history()
t = time.time() - start
print("Algorithm run %d times.\t Total time: %.2f seconds" % (repeat, t))
func = Ackley(dim=2)
#func = CrossIT()
#func = Sphere(dim=2)
# eval(HillClimbing, func.function, func.boundary())
# eval(PSO, Rastrigin(dim=2).function, boundary, repeat=25, population_size=200)
def create_animation(history, **graph_kargs):
anim = AnimatedScatter(history, **graph_kargs)
anim.save('test.gif')
plt.show()
#func = Ackley(dim=2)
alg = PSO(func.function, func.boundary())
# alg = HillClimbing(func.function, func.boundary())
alg.fit()
history = alg.history()
print()
create_animation(history, func=func, title='PSO', contour=True)