-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexperiments.py
183 lines (137 loc) · 5.61 KB
/
experiments.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
173
174
175
176
177
178
179
180
181
182
183
import time
import numpy as np
import pandas as pd
import gel_max_sat
import argparse
import random
IS_VERBOSE = False
DEFAULT_SEED = 131191368
def main():
parser = init_argparse()
args = parser.parse_args()
global IS_VERBOSE
IS_VERBOSE = args.verbose
random.seed(args.seed)
axioms_range = generate_axiom_range(args)
data_set = run_experiments(
axioms_range,
args.concepts_count,
args.prob_axioms_count,
test_count=args.test_count,
roles_count=args.roles_count
)
data_frame = create_data_frame(data_set)
export_data_frame(data_frame, vars(args).values(), args.output)
def init_argparse():
parser = argparse.ArgumentParser(
usage='%(prog)s [options]',
description='Run experiments for GEL-MaxSAT algorithm. If not specified, they are saved in data/experiments/',
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument('-m', '--axioms-range-min', nargs='?',
default=11, type=int, help='minimum number of axioms tested')
parser.add_argument('-M', '--axioms-range-max', nargs='?',
default=200, type=int, help='maximum number of axioms tested')
parser.add_argument('-s', '--axioms-range-step', nargs='?',
default=1, type=int, help='step between each number of axioms tested in the range')
parser.add_argument('-n', '--concepts-count', nargs='?',
default=60, type=int, help='number of concepts tested')
parser.add_argument('-p', '--prob-axioms-count', nargs='?', default=10,
type=int, help='number of probabilistic axioms tested')
parser.add_argument('-t', '--test-count', nargs='?', default=100,
type=int, help='number of tests for each axiom number')
parser.add_argument('-r', '--roles-count', nargs='?', default=3,
type=int, help='number of roles tested')
parser.add_argument('-o', '--output', nargs='?', default=None,
type=str, help='custom path of the output')
parser.add_argument('--seed', nargs='?', default=DEFAULT_SEED,
type=int, help='change the seed used in the random processes')
parser.add_argument('-v', '--verbose', action='store_true',
help='print the progress of the experiments')
return parser
def print_verbose(*args, **kwargs):
if IS_VERBOSE:
print(*args, **kwargs)
def generate_axiom_range(args):
return range(args.axioms_range_min,
args.axioms_range_max,
args.axioms_range_step)
def run_experiments(axioms_range, *args, **kwargs):
data_set = []
print_verbose('axioms | time ')
print_verbose('----------------')
for axioms_count in axioms_range:
print_verbose(end=' {:3} | '.format(axioms_count))
experiment = (axioms_count, *args)
data, exec_time = run_experiment(*experiment, **kwargs)
data_set += [data]
print_verbose('{:.5f}'.format(exec_time))
return data_set
def track_time(function):
def wrap(*args, **kwargs):
start = time.time()
result = function(*args, **kwargs)
end = time.time()
return result, end - start
return wrap
@track_time
def run_experiment(*args, **kwargs):
(sat_mean, time_mean), (sat_std,
time_std) = test_gel_max_sat_satisfatibility(*args, **kwargs)
axioms_count, concepts_count, prob_axioms_count = args
return (concepts_count,
axioms_count / concepts_count,
prob_axioms_count,
sat_mean,
time_mean,
sat_std,
time_std)
def test_gel_max_sat_satisfatibility(axioms_count,
concepts_count,
prob_axioms_count,
*args,
test_count,
**kwargs):
def random_knowledge_bases():
for _ in range(test_count):
yield gel_max_sat.KnowledgeBase.random(
concepts_count,
axioms_count,
prob_axioms_count,
*(kwargs.values()))
def random_weights():
for _ in range(test_count):
yield [random.random() for _ in range(prob_axioms_count)]
sat_and_time_results = np.empty((test_count, 2))
random_samples = zip(random_knowledge_bases(), random_weights())
for idx, (kb, weights) in enumerate(random_samples):
sat, time = gel_max_sat_is_satisfiable(kb, weights)
sat_and_time_results[idx, 0] = sat
sat_and_time_results[idx, 1] = time
return np.mean(sat_and_time_results, axis=0), np.std(sat_and_time_results, axis=0)
@track_time
def gel_max_sat_is_satisfiable(knowledge_base, weights):
return gel_max_sat.is_satisfiable(knowledge_base, weights)
def create_data_frame(data_set):
return pd.DataFrame(
data=data_set,
columns=[
'Concepts count',
'Axioms count',
'Uncertain axioms count',
'SAT proportion mean',
'Time mean',
'SAT proportion std',
'Time std',
])
def export_data_frame(data_frame, arg_values, custom_output):
if custom_output is not None:
data_frame.to_csv(custom_output, index=False)
return
filename = 'data/experiments/'
filename += 'm{}-M{}-s{}-n{}-p{}-t{}-r{}'
filename += '.csv'
filename = filename.format(*arg_values)
data_frame.to_csv(filename, index=False)
if __name__ == '__main__':
main()