forked from chaosite/MeGASampler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregion_counting.h
209 lines (174 loc) · 6.21 KB
/
region_counting.h
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* regioncounting.h
*
* Use region information for model counting
* Author: rainoftime
*
* The information can be very useful, e.g, study the features of real-world formulas
*/
#ifndef REGIONCOUNTING_H_
#define REGIONCOUNTING_H_
#include <string.h>
#include <z3++.h>
#include <vector>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <fstream>
#include "z3_plus.h"
using namespace std;
using namespace z3;
struct RegionCounting {
std::string input_file;
struct timespec start_time;
double solver_time = 0.0;
double check_time = 0.0;
int max_samples;
double max_time;
int m_samples = 0;
int m_success = 0;
int m_unique = 0;
double m_sample_time = 0.0;
z3::context c;
z3::expr smt_formula;
z3::expr_vector m_vars;
std::vector<int> lower_bounds;
std::vector<int> upper_bounds;
std::vector<bool> should_fix;
std::vector<std::vector<int> > unique_models;
RegionCounting(std::string input, int max_samples, double max_time) :
input_file(input), max_samples(max_samples), max_time(max_time), smt_formula(c), m_vars(c) {
}
void parse_smt() {
expr_vector evec = c.parse_file(input_file.c_str());
smt_formula = mk_and(evec);
}
void get_bounds() {
params p(c); p.set("priority", c.str_symbol("box"));
p.set("timeout", (unsigned)15000); // 15 seconds
// 'core_maxsat', 'wmax', 'maxres', 'pd-maxres' ?
//p.set("maxsat_engine", c.str_symbol("maxres"));
optimize opt_sol_min(c); opt_sol_min.set(p);
opt_sol_min.add(smt_formula);
optimize opt_sol_max(c); opt_sol_max.set(p);
opt_sol_max.add(smt_formula);
// Find min
std::vector<optimize::handle> handlers_min;
for (unsigned i = 0; i < m_vars.size(); i++) {
handlers_min.push_back(opt_sol_min.minimize(m_vars[i]));
}
auto min_res = opt_sol_min.check();
for (unsigned i = 0; i < m_vars.size(); i++) {
//std::cout << m_vars[i] <<": " << opt_sol_min.upper(handlers_min[i]) << "\n";
if (min_res == sat) {
lower_bounds.push_back(opt_sol_min.upper(handlers_min[i]).get_numeral_int());
}
else {
lower_bounds.push_back(0);
}
}
// Find max
std::vector<optimize::handle> handlers_max;
for (unsigned i = 0; i < m_vars.size(); i++) {
handlers_max.push_back(opt_sol_max.maximize(m_vars[i]));
}
auto max_res = opt_sol_max.check();
for (unsigned i = 0; i < m_vars.size(); i++) {
//std::cout << m_vars[i] <<": " << opt_sol_max.lower(handlers_max[i]) << "\n";
if (max_res == sat) {
upper_bounds.push_back(opt_sol_max.lower(handlers_max[i]).get_numeral_int());
} else {
unsigned sz = m_vars[i].get_sort().bv_size();
//max integer number of size sz
upper_bounds.push_back((1 << sz) - 1);
}
}
}
// a x b x c x c
std::vector<int> sample_once() {
m_samples++;
std::vector<int> sample;
for (unsigned i = 0; i < m_vars.size(); i++) {
if (should_fix[i]) {
sample.push_back(lower_bounds[i]);
} else {
int output = lower_bounds[i] + (rand() % static_cast<int>(upper_bounds[i] - lower_bounds[i] + 1));
sample.push_back(output);
}
}
return sample;
}
bool check_random_model(std::vector<int>& assignments) {
model rand_model(c);
for (unsigned i = 0; i < m_vars.size(); i++) {
z3::func_decl decl = m_vars[i].decl();
z3::expr val_i = c.bv_val(assignments[i], m_vars[i].get_sort().bv_size());
rand_model.add_const_interp(decl, val_i);
}
if (rand_model.eval(smt_formula).is_true()) {
return true;
} else {
return false;
}
}
// TODO: the total number of assignments are large
// a * b * c * d ..
void run() {
clock_gettime(CLOCK_REALTIME, &start_time);
srand(start_time.tv_sec);
// parse_cnf();
parse_smt();
std::cout << "parse finished...\n";
get_expr_vars(smt_formula, m_vars);
std::cout << "get vars finished...\n";
struct timespec start;
clock_gettime(CLOCK_REALTIME, &start);
get_bounds();
for (unsigned i = 0; i < m_vars.size(); i++) {
if (lower_bounds[i] == upper_bounds[i]) {
should_fix.push_back(true);
} else {
should_fix.push_back(false);
}
}
std::cout << "get bounds finished...\n";
struct timespec end;
clock_gettime(CLOCK_REALTIME, &end);
solver_time += duration(&start, &end);
clock_gettime(CLOCK_REALTIME, &start);
for (int i = 0; i < max_samples; i++) {
if (i % 5000 == 0) print_stats();
struct timespec samp;
clock_gettime(CLOCK_REALTIME, &samp);
double elapsed = duration(&start_time, &samp);
if (elapsed >= max_time) {
std::cout << "Stopping: timeout\n";
finish();
}
m_sample_time += duration(&start, &samp);
std::vector<int> sample = sample_once();
if (check_random_model(sample)) {
m_success++;
}
}
}
double duration(struct timespec * a, struct timespec * b) {
return (b->tv_sec - a->tv_sec) + 1.0e-9 * (b->tv_nsec - a->tv_nsec);
}
void finish() {
std::cout << "-------Good Bye!-----\n";
print_stats();
// TODO: write results to some file
exit(0);
}
void print_stats() {
std::cout << "solver time: " << solver_time << "\n";
std::cout << "sample total time: " << m_sample_time << "\n";
std::cout << "samples number: " << m_samples << "\n";
std::cout << "samples success: " << m_success << "\n";
std::cout << "unique modesl: " << unique_models.size() << "\n";
std::cout << "------------------------------------------\n";
}
};
#endif /* REGIONCOUNTING_H_ */