-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquickexplain.pl
185 lines (140 loc) · 7 KB
/
quickexplain.pl
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
%%% -*- Mode: Prolog; Module: quickexplain; -*-
:- module(quickexplain, [
% These assume that Base+Constraints is unsatisfiable:
qexplain/4, % +AssertPredicate, +Base, +Constraints, -Result
qexplain/3, % +AssertPredicate, +Constraints, -Result (empty Base)
qrelax/4, % +AssertPredicate, +Base, +Constraints, -Result
qrelax/3, % +AssertPredicate, +Constraints, -Result (empty Base)
% Use these if you want a consistency check at the start:
check_explain/4, % +AssertPredicate, +Base, +Constraints, -Result
check_explain/3, % +AssertPredicate, +Constraints, -Result (empty Base)
check_relax/4, % +AssertPredicate, +Base, +Constraints, -Result
check_relax/3 % +AssertPredicate, +Constraints, -Result (empty Base)
]).
/** <module> Quickexplain
SWI Prolog implementation of Ulrich Junker's Quickxplain (https://cdn.aaai.org/AAAI/2004/AAAI04-027.pdf),
for finding minimally unsatisfiable subsets, and maximally satisfiable ones.
@author Tomas Uribe
@license MIT
*/
:- meta_predicate qexplain(1, ?, ?).
:- meta_predicate qexplain(1, ?, ?, ?).
:- meta_predicate qrelax(1, ?, ?).
:- meta_predicate qrelax(1, ?, ?, ?).
:- meta_predicate check_explain(1, ?, ?).
:- meta_predicate check_explain(1, ?, ?, ?).
:- meta_predicate check_relax(1, ?, ?).
:- meta_predicate check_relax(1, ?, ?, ?).
:- use_module(utils).
% debuginfo(X) :- writeln(X), flush_output.
% debuginfo(X, Y) :- write(X), writeln(Y), flush_output.
debuginfo(_X) :- true.
debuginfo(_X, _Y) :- true.
%% Both explain and relax assume that Base + Constraints are, together, inconsistent;
%% if they are not, Output = Constraints, which is fine for relax but confusing for explain.
%% use the "check_" procedures to check consistency first.
%! qexplain(:Assert, +Base, +Constraints, -Result)
% Assuming Base + Constraints is unsat, Result is a minimal unsat subset of Constraints.
qexplain(_Assert, _Base, [], Output) :- !, Output = [].
qexplain(Assert, Base, Constraints, Output) :-
rec_qexplain(Assert, Base, Base, Constraints, Output).
%! qexplain(:Assert, +Constraints, -Result)
% Assuming Constraints is unsat, Result is a minimal unsat subset of Constraints.
qexplain(Assert, Constraints, Result) :- qexplain(Assert, [], Constraints, Result).
%% non_empty(Delta) is there to avoid an extra consistency check at the start.
rec_qexplain(Assert, B, Delta, C, Output) :-
((non_empty(Delta), inconsistent(Assert, B)) -> Output = [] ; (
C = [_Alpha] -> Output = C ; (
split_list(C, C1, C2),
append(B, C1, B1),
rec_qexplain(Assert, B1, C1, C2, Delta2),
append(B, Delta2, B2),
rec_qexplain(Assert, B2, Delta2, C1, Delta1),
append(Delta1, Delta2, Output)
)
)).
%! check_explain(:Assert, +Base, +Constraints, -Result)
% Checks whether Base + Constraints is unsat. If so, calls qexplain; otherwise, Result is `consistent`.
check_explain(Assert, Base, Constraints, Output) :-
consistent(Assert, Base, Constraints) -> Output = consistent; qexplain(Assert, Base, Constraints, Output).
%! check_explain(:Assert, +Constraints, -Result)
% Checks whether Constraints is unsat. If so, calls qexplain; otherwise, Result is `consistent`.
check_explain(Assert, Constraints, Output) :- check_explain(Assert, [], Constraints, Output).
%! qrelax(:Assert, +Base, +Constraints, -Result)
% Assuming Base + Constraints is unsat, Result is a maximal satisfiable subset of Constraints, given Base.
qrelax(_Assert, _B, [], Result) :- !, Result = [].
qrelax(Assert, B, Constraints, Result) :-
rec_qrelax(Assert, B, Constraints, Result).
%! qrelax(:Assert, +Constraints, -Result)
% Assuming Constraints is unsat, Result is a maximal satisfiable subset of Constraints.
qrelax(Assert, Constraints, Result) :- qrelax(Assert, [], Constraints, Result).
rec_qrelax(Assert, B, C, Result) :-
%% debuginfo("calling rec_qrelax ", info{base:B, constraints:C})
(C == [] -> Result = [] ;
(consistent(Assert, B, C) -> (Result = C); (
C = [_Alpha] -> Result = [] ; (
split_list(C, C1, C2),
rec_qrelax(Assert, B, C2, Delta1),
append(B, Delta1, B2),
rec_qrelax(Assert, B2, C1, Delta2),
append(Delta1, Delta2, Result)
)))).
%! check_relax(:Assert, +Base, +Constraints, -Result)
% Assuming Base + Constraints is unsat, Result is a maximal satisfiable subset of Constraints, given Base.
check_relax(Assert, Base, Constraints, Output) :-
consistent(Assert, Base, Constraints) -> Output = consistent; qrelax(Assert, Base, Constraints, Output).
%! check_relax(:Assert, +Constraints, -Result)
% Assuming Constraints is unsat, Result is a maximal satisfiable subset of Constraints.
check_relax(Assert, Constraints, Output) :- check_relax(Assert, [], Constraints, Output).
inconsistent(Assert, L) :- \+ checksat(Assert, L).
consistent(Assert,L) :- \+ \+ checksat(Assert, L).
consistent(Assert, L1, L2) :- \+ \+ checksat(Assert, L1, L2).
checksat(Assert, L) :-
debuginfo("Calling checksat/2"),
maplist({Assert}/[X]>>assert_constraint(Assert, X), L),
true.
checksat(Assert, L1, L2) :-
debuginfo("Calling checksat/3"),
maplist({Assert}/[X]>>assert_constraint(Assert, X), L1),
maplist({Assert}/[X]>>assert_constraint(Assert, X), L2),
% term_variables((L1,L2),Variables), % could have custom checks in the future that do this, complicates the API a little.
% labeling(Variables),
true.
assert_constraint(Assert, Constraint) :-
debuginfo("Asserting constraint ", Constraint),
apply(Assert,[Constraint]),
debuginfo("Succeeded asserting ", Constraint).
non_empty([]) :- fail.
non_empty([_|_]) :- true.
%% TODO: add tests using CLP
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% unit tests %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
:- begin_tests(quickexplain_tests).
:- use_module(library(clpfd)).
test(explain1) :-
qrelax(call, [member(X, [1,2,3,4,5])], [X > 30, X > 2, X >= 4], R),
R =@= [X>2, X>=4].
test(explain2) :-
qrelax(call, [X in 1..3], [X #> 30, X#>2, X#>=4], R),
R =@= [X #> 2].
test(explain3) :-
qexplain(call,[X in 1..5], [X=1, X=2, X>1], R),
R =@= [X=1, X=2].
test(explain4) :-
qexplain(call, [member(X,[1,2,3,4,5])], [X=1, X=2, X>1, X < 0], R),
R =@= [X=1, X=2].
test(explain5) :-
qexplain(call, [member(X,[1,2,3,4,5])], [X=1, X < 0, X=2, X>1], R),
R =@= [X<0].
test(explain6) :-
qexplain(call, [(X + Y) #>= 10], [X#<5, Y#<5, X#>2, X#>4], R),
R =@= [X#<5, Y#<5].
test(check_explain1) :-
check_explain(call, [(X + Y) #>= 10], [X#<5, Y#<5, X#>2, X#>4], R),
R =@= [X#<5, Y#<5].
test(check_explain2, [true(R == consistent)]) :-
check_explain(call, [Y #> 0], [X#<5, Y#<5, X#>2], R).
test(check_relax, true(R == [(X #> 2), (X#>4), (Y#<5)])) :-
check_relax(call, [(X + Y) #>= 10], [X#<5, Y#<5, X#>2, X#>4], R).
test(check_relax2, [true(R == consistent)]) :-
check_relax(call, [Y #> 0], [X#<5, Y#<5, X#>2], R).
:- end_tests(quickexplain_tests).