-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmod.rs
More file actions
296 lines (267 loc) · 9.08 KB
/
mod.rs
File metadata and controls
296 lines (267 loc) · 9.08 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//! Meta algorithms: Divide and Conquer, ...
use std::{rc::Rc, time::Instant};
use cache::InitialBoolCache;
use itertools::Itertools;
use log::{debug, info, trace};
use meta_res::{MetaRes, MetaResult};
use crate::{
algos::{atoms, create_initial_cache, enumeration::aux::enum_aux},
cache::FormulaCache,
formula::{rebuild_formula, tree::FormulaTree},
ltl::trace::{Operators, Trace},
ops::binary::LtlBinaryOp,
};
use super::BoolAlgoParams;
pub mod cache;
pub mod meta_res;
/// LTL search followed by Divide and Conquer.
pub fn divide_conquer<P>(
traces: &[Trace],
alphabet: Vec<String>,
operators: Operators,
target: Vec<bool>,
max_size_ltl: usize,
domin_nb: usize,
params: P,
) -> MetaResult<P::Data>
where
P: BoolAlgoParams + Clone,
{
let start = Instant::now();
let atoms = atoms(traces, alphabet);
// Add initial formulas
let (atom, mut ltl_cache) = create_initial_cache(atoms, &target);
// Check if target is an atom
if let Some(f) = atom {
let ltl_time = start.elapsed();
let f_str = rebuild_formula(&f, <l_cache);
return MetaResult {
ltl_time,
ltl_cache_sizes: vec![],
algo_time: None,
algo_data: None,
result: MetaRes::Atom(f_str),
};
}
// Ltl search
let ltl_res = enum_aux(&mut ltl_cache, &operators, &target, max_size_ltl);
let ltl_time = start.elapsed();
let ltl_cache_sizes = ltl_cache.lines.iter().map(|l| l.len()).collect();
if let Some(f) = ltl_res {
let f_str = rebuild_formula(&f, <l_cache);
return MetaResult {
ltl_time,
ltl_cache_sizes,
algo_time: None,
algo_data: None,
result: MetaRes::FoundByLtl(f_str),
};
}
debug!("Ltl cache has size {}", ltl_cache.len());
debug!("Running D&C with algo {}", P::name());
let start = Instant::now();
let initial_cache = InitialBoolCache::from_ltl_cache(domin_nb, ltl_cache, &target);
debug!("Initial bool cache len: {}", initial_cache.len());
let f = solve_or_split(traces, operators, initial_cache, &target, params);
let algo_time = Some(start.elapsed());
MetaResult {
ltl_time,
ltl_cache_sizes,
algo_time,
algo_data: None,
result: match f {
Some(f) => MetaRes::FoundByBool(f),
None => MetaRes::NotFound,
},
}
}
/// Solve Boolean Synthesis problem using Divide and Conquer and the algorithm specified in `params`.
///
/// If the number of traces is more than 128 split immediately.
/// Otherwise, try to solve the instance with the algorithm implemented by `params`.
/// If no solution is found, try to find one by splitting recursively.
/// Splitting is handled using [`split_and_solve_non_overlapping`].
fn solve_or_split<P>(
traces: &[Trace],
operators: Operators,
initial_cache: InitialBoolCache,
target: &[bool],
params: P,
) -> Option<FormulaTree>
where
P: BoolAlgoParams + Clone,
{
let nb_traces = target.len();
// Check whether the fom
if let Some(f) = initial_cache.get_from_cv(target, target) {
// Due to hash collisions, the formula may not be right; we check that
// the formula has the right characteristic vector.
if f.eval(traces).accepted_vec() == target {
debug!("Formula found in cache");
return Some(f);
} else {
debug!("Hash collision found a wrong formula {f} for target {target:?}; continuing");
}
}
if nb_traces > 128 {
split_and_solve_non_overlapping(traces, operators, initial_cache, target, params)
} else {
let (res, _) = params
.clone()
.run(initial_cache.clone(), operators.clone(), target);
match res {
Some(f) => Some(f),
None => {
split_and_solve_non_overlapping(traces, operators, initial_cache, target, params)
}
}
}
}
fn _split_and_solve<P>(
traces: &[Trace],
operators: Operators,
cache: InitialBoolCache,
target: &[bool],
params: P,
) -> Option<FormulaTree>
where
P: BoolAlgoParams + Clone,
{
let (op, left, right) = find_split(target)?;
info!("Splitting on op '{op}'");
let (left_cache, right_cache) = cache.split(&left, &right, target);
info!(
"Cache sizes: {} (left) {} right",
left_cache.len(),
right_cache.len()
);
let left_target = left.iter().map(|&i| target[i]).collect_vec();
let left_traces = left.iter().map(|&i| traces[i].clone()).collect_vec();
let left_res = solve_or_split(
&left_traces,
operators.clone(),
left_cache,
&left_target,
params.clone(),
)?;
let right_target = right.iter().map(|&i| target[i]).collect_vec();
let right_traces = right.iter().map(|&i| traces[i].clone()).collect_vec();
let right_res = solve_or_split(&right_traces, operators, right_cache, &right_target, params)?;
Some(FormulaTree::BinaryNode {
op,
left: Rc::from(left_res),
right: Rc::from(right_res),
})
}
/// Divide and conquer subrouting to split into two subproblems with clever merging.
///
/// We use [`find_split`] to get indices for the left subproblem, and solve it recursively.
/// If we get a solution, use the set of unsatisfied indices for the right subproblem,
/// instead of all the other indices.
/// As the left result might solve traces that were not included in the call, this yields much smaller formulas.
fn split_and_solve_non_overlapping<P>(
traces: &[Trace],
operators: Operators,
cache: InitialBoolCache,
target: &[bool],
params: P,
) -> Option<FormulaTree>
where
P: BoolAlgoParams + Clone,
{
let (op, left, _) = find_split(target)?;
info!("Splitting on op '{op}'");
let left_cache = cache.reduce(&left, target);
info!("Left cache size: {}", left_cache.len());
let left_target = left.iter().map(|&i| target[i]).collect_vec();
let left_traces = left.iter().map(|&i| traces[i].clone()).collect_vec();
let left_res = solve_or_split(
&left_traces,
operators.clone(),
left_cache,
&left_target,
params.clone(),
)?;
debug!("Found left formula {}", left_res);
// Compute the indices of the traces that are not satisfied by the left result,
// and only recurse on these.
let solved = left_res.eval(traces).accepted_vec();
let right = solved
.into_iter()
.zip(target.iter())
.enumerate()
.filter_map(|(i, (b1, &b2))| match op {
// When splitting on a Or, we need to keep all negatives
// and unsat positives, i.e. those for which `cv` is false.
LtlBinaryOp::Or if !b2 | !b1 => Some(i),
// When splitting on an And, we instead keep all positives
// and unsat negatives, i.e. those for which `cv` is true.
LtlBinaryOp::And if b2 | b1 => Some(i),
_ => None,
})
.collect_vec();
let nb_not_sat = right
.iter()
.filter(|&&i| match op {
LtlBinaryOp::Or => target[i],
LtlBinaryOp::And => !target[i],
_ => unreachable!(),
})
.count();
if nb_not_sat == 0 {
debug!("0 left to satisfy, shortcut return");
return Some(left_res);
}
debug!("Number of unsat after left call: {}", right.len());
trace!("Unsat after call: {:?}", &right);
let right_cache = cache.reduce(&right, target);
let right_target = right.iter().map(|&i| target[i]).collect_vec();
let right_traces = right.iter().map(|&i| traces[i].clone()).collect_vec();
let right_res = solve_or_split(&right_traces, operators, right_cache, &right_target, params)?;
debug!("Found right formula {}", right_res);
let res = FormulaTree::BinaryNode {
op,
left: Rc::from(left_res),
right: Rc::from(right_res),
};
debug!("Found formula {}", res);
Some(res)
}
/// Split the largest of the negatives or the positive.
///
/// Returns the operation to use when merging, as well as two vectors of indices
/// of traces to keeps in each split.
///
/// If the split was on the positives, the returned operation is [`LtlBinaryOp::Or`],
/// and otherwise it's [`LtlBinaryOp::And`].
fn find_split(target: &[bool]) -> Option<(LtlBinaryOp, Vec<usize>, Vec<usize>)> {
let nb_traces = target.len();
let nb_pos = target.iter().filter(|b| **b).count();
let nb_neg = nb_traces - nb_pos;
if nb_pos <= 1 && nb_neg <= 1 {
return None;
}
let mut left = vec![];
let mut right = vec![];
let op = if nb_pos > nb_neg {
LtlBinaryOp::Or
} else {
LtlBinaryOp::And
};
let mut j = 0;
for (i, &t) in target.iter().enumerate() {
if t == (nb_pos > nb_neg) {
// Split alternatively between left and right.
if j % 2 == 0 {
left.push(i);
} else {
right.push(i);
}
j += 1;
} else {
left.push(i);
right.push(i);
}
}
Some((op, left, right))
}