-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval.py
172 lines (134 loc) · 4.4 KB
/
eval.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import sys, os
import time
import csv
import argparse
import math
from statistics import mean
from lxml import etree
from functools import partial
from multiprocessing import Pool
from random import *
from ga import GA
from utils import *
from tqdm import tqdm
def parse_arguments():
parser = argparse.ArgumentParser()
parser.add_argument("-d", type=str, required=True, help="Specify path to testfile")
parser.add_argument("-p", type=int, default=50, help="Set an even number population size (default=50)")
parser.add_argument("-f", type=int, default=1e+4, help="Set fitness evaluation limit (default=10000)")
parser.add_argument("-r", type=float, default=0.1, help="Set mutation rate (0.0 ~ 1.0, default=0.3)")
parser.add_argument("-k", type=int, default=3, help="Set mutation limit per child (default=3)")
parser.add_argument("-c", type=float, default=0.8, help="Set crossover_rate (0.0 ~ 1.0, default=0.8)")
parser.add_argument("-t", type=int, default=3, help="Set tournament selection k value(default=3)")
parser.add_argument("-l", action="store_true", help="Use linear ranking (default=tournament selection)")
parser.add_argument("-e", action="store_true", help="Use elitism (default=generational_replacement)")
parser.add_argument("-s", action="store_true", help="Show progress")
parser.add_argument("-P", action="store_true", help="Use parallel computation")
parser.add_argument("-i", type=int, default=0, help="Set results id (default=0)")
return parser.parse_args()
def eval_ga(row, arguments, fit_val):
start=time.time()
args=arguments
path_old = row[2]
xpath_old = row[4]
path_new = row[3]
xpath_new = row[5]
fit_val_temp=fit_val # *, tag, position, class, id
ga = GA(
pop_size=args.p,
eval_lim=args.f,
mut_rate=args.r,
mut_k=args.k,
crossover_rate=args.c,
tournament_k=args.t,
use_elitism=args.e,
use_lin_ranking=args.l,
dom_filepath=path_old.strip(),
xpath=xpath_old,
fitness_values=fit_val_temp,
verbose=args.s
)
ga.select_parents()
ga.evolve()
xpath_robust = ga.optimum.xpath
xpath_fitness = ga.optimum.fitness
end=time.time()
# print("{} seconds elapsed.".format(round(end-start,2)))
return xpath_robust, xpath_fitness
if __name__ == '__main__':
args = parse_arguments()
testfile = args.d
testdata = []
with open(testfile,"r") as f:
reader = csv.reader(f)
line_num = -1
for row in reader:
if line_num < 0:
line_num += 1
continue
line_num += 1
testdata.append(row)
fit_vals = []
for i in range(3):
temp = []
for j in range(5):
temp.append(uniform(0,10))
fit_vals.append(temp)
param_results = []
found_list = [False for i in range(len(testdata))]
testnum = args.i
initialtime = time.time()
with open("results{}.csv".format(testnum),"w", newline='') as csvfile:
writer = csv.writer(csvfile)
for fit_val_set in tqdm(fit_vals,desc='Total'):
start = time.time()
result_temp = []
for i in tqdm(range(5), desc='Current', leave=False):
results = []
if args.P:
with Pool(min(len(testdata),8)) as p:
results = p.map(partial(eval_ga, arguments=args, fit_val=fit_val_set), testdata)
else:
for row in testdata:
results.append(eval_ga(row, args, fit_val_set))
time.sleep(1)
success = 0
fail = 0
listfound = 0
line_num = 0
for xpath_robust, xpath_fitness in results:
row=testdata[line_num]
line_num += 1
path_new = row[3]
xpath_new = row[5]
utf8_html_parser = etree.HTMLParser(encoding='utf-8')
new_DOM = etree.parse(cleanup(path_new),parser=utf8_html_parser)
query = new_DOM.xpath(xpath_new)
assert len(query) == 1, "Multiple items found in new query"
target = query[0]
final_path = finalize_xpath(xpath_robust)
found = new_DOM.xpath(xpath_robust)
if len(found) != 1:
if len(found) > 0:
for elem in found:
if elem == target:
listfound += 1
continue
fail += 1
elif (found[0]==target):
success += 1
found_list[line_num-1] = True
else:
fail += 1
result_temp.append(success+listfound*0.01)
result_temp += fit_val_set
param_results.append(result_temp)
writer.writerow(result_temp)
end=time.time()
end = time.time()
print("{} seconds elapsed in total.".format(round(end-initialtime)))
with open("found{}.csv".format(testnum), "w", newline='') as foundfile:
writer = csv.writer(foundfile)
writer.writerow(found_list)
def find_weights():
pass