-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path__main__.py
executable file
·90 lines (76 loc) · 2.89 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
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
import os
import sys
import traceback
import numpy as np
from config import Config, ConfigError
from verapak.parse_args.tools import parse_args
from algorithm import main
from verapak.utilities.sets import Reporter, DoneInterrupt
def create_witness(config, adversarial_example):
input_values = adversarial_example.flatten(),
output_values = config['graph'].evaluate(adversarial_example).flatten()
witness = "("
for idx, x in np.ndenumerate(input_values):
witness += f"(X_{idx[0]} {x})\\n"
for idx, y in np.ndenumerate(output_values):
witness += f"(Y_{idx[0]} {y})\\n"
witness += ")"
return witness
def write_results(config, adversarial_examples, halt_reason, elapsed_time):
witness = ""
adv_count = 0
if adversarial_examples and adversarial_examples.size() > 0:
witness = create_witness(next(adversarial_examples.elements()))
adv_count = adversarial_examples.size()
adv_examples_numpy = np.array([x for x in adversarial_examples.elements()])
output_file = os.path.join(config['output_dir'], 'adversarial_examples.npy')
np.save(output_file, adv_examples_numpy)
if halt_reason in ["done", "first"]:
halt_reason = "sat" if (adv_count > 0) else "unsat"
if "output_dir" in config:
output_file = os.path.join(config['output_dir'], 'report.csv')
output_file = open(output_file, 'w')
output_file.write(f"{halt_reason},{witness},{adv_count},{elapsed_time}\n")
output_file.close()
def save_state(config, reporter):
pass # TODO
def run(config):
reporter = Reporter()
try:
# Run the algorithm
main(config, reporter)
except KeyboardInterrupt as e:
reporter.halt_reason = "keyboard"
except DoneInterrupt as e:
pass
except BaseException as e:
reporter.halt_reason = "error"
traceback.print_exception(type(e), e, e.__traceback__)
save_state(config, reporter)
if reporter.started:
reporter.give_final_report()
et = reporter.get_elapsed_time()
else:
et = 0
adversarial = reporter.get_adversarial_examples()
halt_reason = reporter.get_halt_reason
write_results(config, adversarial, halt_reason, et)
print('done')
if __name__ == "__main__":
if len(sys.argv) == 1:
sys.argv = [sys.argv[0], "--help"]
config = parse_args(sys.argv[1:], prog=sys.argv[0])
if "error" in config:
print(f"\033[38;2;255;0;0mERROR: {config['error']}\033[0m")
write_results(config, None, "error_" + config["error"], 0)
else:
try:
config = Config(config)
except ConfigError as ex:
print(ex)
else: # Valid Config
for strategy in config["strategy"].values():
strategy.set_config(config)
run(config)
for strategy in config["strategy"].values():
strategy.shutdown()