forked from StanislavUshakov/ArtificialImmuneSystem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
45 lines (36 loc) · 1.48 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
__author__ = 'Stanislav Ushakov'
import math
import time
from expression import Expression
from immune import ExpressionsImmuneSystem, FitnessFunction, DataFileStorageHelper, ExpressionsImmuneSystemConfig
from exchanger import SimpleRandomExchanger
def update_progress(progress:int):
"""
Shows progress bar. Progress is passed in percent.
"""
print('\r[{0}] {1}%'.format('#' * (progress // 10), progress), end='')
if __name__ == "__main__":
number_of_lymphocytes = 100
max_height = 4
DataFileStorageHelper.save_to_file('test_x_y.txt', ['x', 'y'], lambda x, y: x*x + x*y*math.sin(x*y), 100)
variables, values = DataFileStorageHelper.load_from_file('test_x_y.txt')
f = FitnessFunction(values)
exchanger = SimpleRandomExchanger(
lambda: [Expression.generate_random(max_height=max_height, variables=variables)
for i in range(0, number_of_lymphocytes // 2)])
config = ExpressionsImmuneSystemConfig()
results = []
iterations = 5
start = time.clock()
for i in range(0, iterations):
immuneSystem = ExpressionsImmuneSystem(exact_values=values,
variables=variables,
exchanger=exchanger,
config=config)
best = immuneSystem.solve()
results.append((f.expression_value(best), str(best)))
update_progress(int((i+1) / iterations * 100))
end = time.clock()
print('\n{0} seconds'.format(end - start))
for result in sorted(results):
print(result, sep='\n')