forked from AnuarTB/gengen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evo_test.py
128 lines (96 loc) · 3.1 KB
/
evo_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
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
"""
Not a unittest for now
"""
import logging
import random
import time
from evo import EvoGen, Input
from generic import Float, Int, List
import hash
import logging
def write_log(elapsed_time, input):
LOG_FORMAT = "%(asctime)s - %(message)s"
logging.basicConfig(filename = "gengen_logs.log",
level = logging.DEBUG,
format = LOG_FORMAT)
logger = logging.getLogger()
logger.info((elapsed_time, input))
def hash_test(tmp):
hash.main(tmp)
def insertion_sort(tmp):
arr = tmp[:]
for i in range(1, len(arr)):
key = arr[i]
j = i - 1
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
return arr
def bubble_sort(tmp):
aux = tmp[:]
while True:
relaxed = False
for i in range(len(aux) - 1):
if aux[i] > aux[i + 1]:
t = aux[i]
aux[i] = aux[i + 1]
aux[i + 1] = t
relaxed = True
if not relaxed:
break
return aux
def quick_sort(tmp):
less = []
equal = []
greater = []
if len(tmp) > 1:
pivot = tmp[0]
for x in tmp:
if x < pivot:
less.append(x)
elif x == pivot:
equal.append(x)
elif x > pivot:
greater.append(x)
return quick_sort(less) + equal + quick_sort(greater)
else:
return tmp
def evaluate(func, inp):
start = time.time()
func(inp)
return time.time() - start
def main():
"""
e = EvoGen(10, 50, parent_selection=0, mutator_list=0)
_, t = e.generate_worst_case(hash_test,
List(10000, 10000, Int(-400, 400)))
print(f"t: {t}")
worst_input = [0] * 10000
print(f"theoretical worst: {evaluate(hash_test, worst_input)}")
random_input = [random.randint(0, 99) for i in range(10000)]
print(f"random input: {evaluate(hash_test, random_input)}")
"""
length = 3000
worst_input = [length - i - 1 for i in range(length)]
print(f"theoretical worst: {evaluate(insertion_sort, worst_input)}")
random_input = [random.randint(-400, 400) for i in range(length)]
print(f"random input: {evaluate(insertion_sort, random_input)}")
e = EvoGen(10, 50, parent_selection=1, mut_prob=0.5, ratio=0.6, mutator_list=1)
_, t = e.generate_worst_case(insertion_sort,
List(length, length, Int(-400, 400)))
print(f"t: {t}")
worst_input = [length - i - 1 for i in range(length)]
print(f"theoretical worst: {evaluate(insertion_sort, worst_input)}")
random_input = [random.randint(-400, 400) for i in range(length)]
print(f"random input: {evaluate(insertion_sort, random_input)}")
"""
_, t = e.generate_worst_case(bubble_sort,
List(3000, 3000, Int(-400, 400)))
print(f"t: {t}")
_, t = e.generate_worst_case(quick_sort,
List(200000, 200000, Int(-400, 400)))
print(f"t: {t}")
"""
if __name__ == "__main__":
main()