forked from Z3Prover/z3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnlsat_explain.cpp
More file actions
2147 lines (1973 loc) · 82.5 KB
/
Copy pathnlsat_explain.cpp
File metadata and controls
2147 lines (1973 loc) · 82.5 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*++
Copyright (c) 2012 Microsoft Corporation
Module Name:
nlsat_explain.cpp
Abstract:
Functor that implements the "explain" procedure defined in Dejan and Leo's paper.
Author:
Leonardo de Moura (leonardo) 2012-01-13.
Revision History:
--*/
#include "nlsat/nlsat_explain.h"
#include "nlsat/nlsat_assignment.h"
#include "nlsat/nlsat_evaluator.h"
#include "math/polynomial/algebraic_numbers.h"
#include "util/ref_buffer.h"
namespace nlsat {
typedef polynomial::polynomial_ref_vector polynomial_ref_vector;
typedef ref_buffer<poly, pmanager> polynomial_ref_buffer;
struct explain::imp {
solver & m_solver;
assignment const & m_assignment;
atom_vector const & m_atoms;
atom_vector const & m_x2eq;
anum_manager & m_am;
polynomial::cache & m_cache;
pmanager & m_pm;
polynomial_ref_vector m_ps;
polynomial_ref_vector m_ps2;
polynomial_ref_vector m_psc_tmp;
polynomial_ref_vector m_factors, m_factors_save;
scoped_anum_vector m_roots_tmp;
bool m_simplify_cores;
bool m_full_dimensional;
bool m_minimize_cores;
bool m_factor;
bool m_signed_project;
bool m_cell_sample;
struct todo_set {
polynomial::cache & m_cache;
polynomial_ref_vector m_set;
svector<char> m_in_set;
todo_set(polynomial::cache & u):m_cache(u), m_set(u.pm()) {}
void reset() {
pmanager & pm = m_set.m();
unsigned sz = m_set.size();
for (unsigned i = 0; i < sz; i++) {
m_in_set[pm.id(m_set.get(i))] = false;
}
m_set.reset();
}
void insert(poly * p) {
pmanager & pm = m_set.m();
p = m_cache.mk_unique(p);
unsigned pid = pm.id(p);
if (m_in_set.get(pid, false))
return;
m_in_set.setx(pid, true, false);
m_set.push_back(p);
}
bool empty() const { return m_set.empty(); }
// Return max variable in todo_set
var max_var() const {
pmanager & pm = m_set.m();
var max = null_var;
unsigned sz = m_set.size();
for (unsigned i = 0; i < sz; i++) {
var x = pm.max_var(m_set.get(i));
SASSERT(x != null_var);
if (max == null_var || x > max)
max = x;
}
return max;
}
/**
\brief Remove the maximal polynomials from the set and store
them in max_polys. Return the maximal variable
*/
var extract_max_polys(polynomial_ref_vector & max_polys) {
max_polys.reset();
var x = max_var();
pmanager & pm = m_set.m();
unsigned sz = m_set.size();
unsigned j = 0;
for (unsigned i = 0; i < sz; i++) {
poly * p = m_set.get(i);
var y = pm.max_var(p);
SASSERT(y <= x);
if (y == x) {
max_polys.push_back(p);
m_in_set[pm.id(p)] = false;
}
else {
m_set.set(j, p);
j++;
}
}
m_set.shrink(j);
return x;
}
};
// temporary field for store todo set of polynomials
todo_set m_todo;
// temporary fields for preprocessing core
scoped_literal_vector m_core1;
scoped_literal_vector m_core2;
// temporary fields for storing the result
scoped_literal_vector * m_result = nullptr;
svector<char> m_already_added_literal;
evaluator & m_evaluator;
imp(solver & s, assignment const & x2v, polynomial::cache & u, atom_vector const & atoms, atom_vector const & x2eq,
evaluator & ev, bool is_sample):
m_solver(s),
m_assignment(x2v),
m_atoms(atoms),
m_x2eq(x2eq),
m_am(x2v.am()),
m_cache(u),
m_pm(u.pm()),
m_ps(m_pm),
m_ps2(m_pm),
m_psc_tmp(m_pm),
m_factors(m_pm),
m_factors_save(m_pm),
m_roots_tmp(m_am),
m_cell_sample(is_sample),
m_todo(u),
m_core1(s),
m_core2(s),
m_evaluator(ev) {
m_simplify_cores = false;
m_full_dimensional = false;
m_minimize_cores = false;
m_signed_project = false;
}
std::ostream& display(std::ostream & out, polynomial_ref const & p) const {
m_pm.display(out, p, m_solver.display_proc());
return out;
}
std::ostream& display(std::ostream & out, polynomial_ref_vector const & ps, char const * delim = "\n") const {
for (unsigned i = 0; i < ps.size(); i++) {
if (i > 0)
out << delim;
m_pm.display(out, ps.get(i), m_solver.display_proc());
}
return out;
}
std::ostream& display(std::ostream & out, literal l) const { return m_solver.display(out, l); }
std::ostream& display_var(std::ostream & out, var x) const { return m_solver.display(out, x); }
std::ostream& display(std::ostream & out, unsigned sz, literal const * ls) const { return m_solver.display(out, sz, ls); }
std::ostream& display(std::ostream & out, literal_vector const & ls) const { return display(out, ls.size(), ls.data()); }
std::ostream& display(std::ostream & out, scoped_literal_vector const & ls) const { return display(out, ls.size(), ls.data()); }
/**
\brief Add literal to the result vector.
*/
void add_literal(literal l) {
SASSERT(m_result != 0);
SASSERT(l != true_literal);
if (l == false_literal)
return;
unsigned lidx = l.index();
if (m_already_added_literal.get(lidx, false))
return;
TRACE(nlsat_explain, tout << "adding literal: " << lidx << "\n"; m_solver.display(tout, l) << "\n";);
m_already_added_literal.setx(lidx, true, false);
m_result->push_back(l);
}
/**
\brief Reset m_already_added vector using m_result
*/
void reset_already_added() {
SASSERT(m_result != nullptr);
for (literal lit : *m_result)
m_already_added_literal[lit.index()] = false;
SASSERT(check_already_added());
}
/**
\brief evaluate the given polynomial in the current interpretation.
max_var(p) must be assigned in the current interpretation.
*/
::sign sign(polynomial_ref const & p) {
SASSERT(max_var(p) == null_var || m_assignment.is_assigned(max_var(p)));
auto s = m_am.eval_sign_at(p, m_assignment);
TRACE(nlsat_explain, tout << "p: " << p << " var: " << max_var(p) << " sign: " << s << "\n";);
return s;
}
/**
\brief Wrapper for factorization
*/
void factor(polynomial_ref & p, polynomial_ref_vector & fs) {
// TODO: add params, caching
TRACE(nlsat_factor, tout << "factor\n" << p << "\n";);
fs.reset();
m_cache.factor(p.get(), fs);
}
/**
\brief Wrapper for psc chain computation
*/
void psc_chain(polynomial_ref & p, polynomial_ref & q, unsigned x, polynomial_ref_vector & result) {
// TODO caching
SASSERT(max_var(p) == max_var(q));
SASSERT(max_var(p) == x);
m_cache.psc_chain(p, q, x, result);
}
/**
\brief Store in ps the polynomials occurring in the given literals.
*/
void collect_polys(unsigned num, literal const * ls, polynomial_ref_vector & ps) {
ps.reset();
for (unsigned i = 0; i < num; i++) {
atom * a = m_atoms[ls[i].var()];
SASSERT(a != 0);
if (a->is_ineq_atom()) {
unsigned sz = to_ineq_atom(a)->size();
for (unsigned j = 0; j < sz; j++)
ps.push_back(to_ineq_atom(a)->p(j));
}
else {
ps.push_back(to_root_atom(a)->p());
}
}
}
/**
\brief Add literal p != 0 into m_result.
*/
ptr_vector<poly> m_zero_fs;
bool_vector m_is_even;
struct restore_factors {
polynomial_ref_vector& m_factors, &m_factors_save;
unsigned num_saved = 0;
restore_factors(polynomial_ref_vector&f, polynomial_ref_vector& fs):
m_factors(f), m_factors_save(fs)
{
num_saved = m_factors_save.size();
m_factors_save.append(m_factors);
}
~restore_factors() {
m_factors.reset();
m_factors.append(m_factors_save.size() - num_saved, m_factors_save.data() + num_saved);
m_factors_save.shrink(num_saved);
}
};
void add_zero_assumption(polynomial_ref& p) {
// If p is of the form p1^n1 * ... * pk^nk,
// then only the factors that are zero in the current interpretation needed to be considered.
// I don't want to create a nested conjunction in the clause.
// Then, I assert p_i1 * ... * p_im != 0
{
restore_factors _restore(m_factors, m_factors_save);
factor(p, m_factors);
unsigned num_factors = m_factors.size();
m_zero_fs.reset();
m_is_even.reset();
polynomial_ref f(m_pm);
for (unsigned i = 0; i < num_factors; i++) {
f = m_factors.get(i);
if (is_zero(sign(f))) {
m_zero_fs.push_back(m_factors.get(i));
m_is_even.push_back(false);
}
}
}
SASSERT(!m_zero_fs.empty()); // one of the factors must be zero in the current interpretation, since p is zero in it.
literal l = m_solver.mk_ineq_literal(atom::EQ, m_zero_fs.size(), m_zero_fs.data(), m_is_even.data());
l.neg();
TRACE(nlsat_explain, tout << "adding (zero assumption) literal:\n"; display(tout, l); tout << "\n";);
add_literal(l);
}
void add_simple_assumption(atom::kind k, poly * p, bool sign = false) {
SASSERT(k == atom::EQ || k == atom::LT || k == atom::GT);
bool is_even = false;
bool_var b = m_solver.mk_ineq_atom(k, 1, &p, &is_even);
literal l(b, !sign);
add_literal(l);
}
void add_assumption(atom::kind k, poly * p, bool sign = false) {
// TODO: factor
add_simple_assumption(k, p, sign);
}
/**
\brief Eliminate "vanishing leading coefficients" of p.
That is, coefficients that vanish in the current
interpretation. The resultant p is a reduct of p s.t. its
leading coefficient does not vanish in the current
interpretation. If all coefficients of p vanish, then
the resultant p is the zero polynomial.
*/
void elim_vanishing(polynomial_ref & p) {
SASSERT(!is_const(p));
var x = max_var(p);
unsigned k = degree(p, x);
SASSERT(k > 0);
polynomial_ref lc(m_pm);
polynomial_ref reduct(m_pm);
while (true) {
if (is_const(p))
return;
if (k == 0) {
// x vanished from p, peek next maximal variable
x = max_var(p);
SASSERT(x != null_var);
k = degree(p, x);
}
if (m_pm.nonzero_const_coeff(p, x, k)) {
TRACE(nlsat_explain, tout << "nonzero const x" << x << "\n";);
return; // lc is a nonzero constant
}
lc = m_pm.coeff(p, x, k, reduct);
TRACE(nlsat_explain, tout << "lc: " << lc << " reduct: " << reduct << "\n";);
if (!is_zero(lc)) {
if (!::is_zero(sign(lc))) {
TRACE(nlsat_explain, tout << "lc does no vaninsh\n";);
return;
}
TRACE(nlsat_explain, tout << "got a zero sign on lc\n";);
// lc is not the zero polynomial, but it vanished in the current interpretation.
// so we keep searching...
TRACE(nlsat_explain, tout << "adding zero assumption for var:"; m_solver.display_var(tout, x); tout << ", degree k:" << k << ", p:" ; display(tout, p) << "\n";);
add_zero_assumption(lc);
}
if (k == 0) {
// all coefficients of p vanished in the current interpretation,
// and were added as assumptions.
p = m_pm.mk_zero();
TRACE(nlsat_explain, tout << "all coefficients of p vanished\n";);
return;
}
k--;
p = reduct;
}
}
/**
Eliminate vanishing coefficients of polynomials in ps.
The coefficients that are zero (i.e., vanished) are added
as assumptions into m_result.
*/
void elim_vanishing(polynomial_ref_vector & ps) {
unsigned j = 0;
unsigned sz = ps.size();
polynomial_ref p(m_pm);
for (unsigned i = 0; i < sz; i++) {
p = ps.get(i);
elim_vanishing(p);
if (!is_const(p)) {
ps.set(j, p);
j++;
}
}
ps.shrink(j);
}
/**
Normalize literal with respect to given maximal variable.
The basic idea is to eliminate vanishing (leading) coefficients from a (arithmetic) literal,
and factors from lower stages.
The vanishing coefficients and factors from lower stages are added as assumptions to the lemma
being generated.
Example 1)
Assume
- l is of the form (y^2 - 2)*x^3 + y*x + 1 > 0
- x is the maximal variable
- y is assigned to sqrt(2)
Thus, (y^2 - 2) the coefficient of x^3 vanished. This method returns
y*x + 1 > 0 and adds the assumption (y^2 - 2) = 0 to the lemma
Example 2)
Assume
- l is of the form (x + 2)*(y - 1) > 0
- x is the maximal variable
- y is assigned to 0
(x + 2) < 0 is returned and assumption (y - 1) < 0 is added as an assumption.
Remark: root atoms are not normalized
*/
literal normalize(literal l, var max) {
bool_var b = l.var();
if (b == true_bool_var)
return l;
SASSERT(m_atoms[b] != 0);
if (m_atoms[b]->is_ineq_atom()) {
polynomial_ref_buffer ps(m_pm);
sbuffer<bool> is_even;
polynomial_ref p(m_pm);
ineq_atom * a = to_ineq_atom(m_atoms[b]);
int atom_sign = 1;
unsigned sz = a->size();
bool normalized = false; // true if the literal needs to be normalized
for (unsigned i = 0; i < sz; i++) {
p = a->p(i);
if (max_var(p) == max)
elim_vanishing(p); // eliminate vanishing coefficients of max
if (is_const(p) || max_var(p) < max) {
int s = sign(p);
if (!is_const(p)) {
SASSERT(max_var(p) != null_var);
SASSERT(max_var(p) < max);
// factor p is a lower stage polynomial, so we should add assumption to justify p being eliminated
if (s == 0)
add_simple_assumption(atom::EQ, p); // add assumption p = 0
else if (a->is_even(i))
add_simple_assumption(atom::EQ, p, true); // add assumption p != 0
else if (s < 0)
add_simple_assumption(atom::LT, p); // add assumption p < 0
else
add_simple_assumption(atom::GT, p); // add assumption p > 0
}
if (s == 0) {
bool atom_val = a->get_kind() == atom::EQ;
bool lit_val = l.sign() ? !atom_val : atom_val;
return lit_val ? true_literal : false_literal;
}
else if (s < 0 && a->is_odd(i)) {
atom_sign = -atom_sign;
}
normalized = true;
}
else {
if (p != a->p(i)) {
SASSERT(!m_pm.eq(p, a->p(i)));
normalized = true;
}
is_even.push_back(a->is_even(i));
ps.push_back(p);
}
}
if (ps.empty()) {
SASSERT(atom_sign != 0);
// LHS is positive or negative. It is positive if atom_sign > 0 and negative if atom_sign < 0
bool atom_val;
if (a->get_kind() == atom::EQ)
atom_val = false;
else if (a->get_kind() == atom::LT)
atom_val = atom_sign < 0;
else
atom_val = atom_sign > 0;
bool lit_val = l.sign() ? !atom_val : atom_val;
return lit_val ? true_literal : false_literal;
}
else if (normalized) {
atom::kind new_k = a->get_kind();
if (atom_sign < 0)
new_k = atom::flip(new_k);
literal new_l = m_solver.mk_ineq_literal(new_k, ps.size(), ps.data(), is_even.data());
if (l.sign())
new_l.neg();
return new_l;
}
else {
SASSERT(atom_sign > 0);
return l;
}
}
else {
return l;
}
}
/**
Normalize literals (in the conflicting core) with respect
to given maximal variable. The basic idea is to eliminate
vanishing (leading) coefficients (and factors from lower
stages) from (arithmetic) literals,
*/
void normalize(scoped_literal_vector & C, var max) {
unsigned sz = C.size();
unsigned j = 0;
for (unsigned i = 0; i < sz; i++) {
literal new_l = normalize(C[i], max);
if (new_l == true_literal)
continue;
if (new_l == false_literal) {
// false literal was created. The assumptions added are sufficient for implying the conflict.
C.reset();
return;
}
C.set(j, new_l);
j++;
}
C.shrink(j);
}
var max_var(poly const * p) { return m_pm.max_var(p); }
/**
\brief Return the maximal variable in a set of nonconstant polynomials.
*/
var max_var(polynomial_ref_vector const & ps) {
if (ps.empty())
return null_var;
var max = max_var(ps.get(0));
SASSERT(max != null_var); // there are no constant polynomials in ps
unsigned sz = ps.size();
for (unsigned i = 1; i < sz; i++) {
var curr = m_pm.max_var(ps.get(i));
SASSERT(curr != null_var);
if (curr > max)
max = curr;
}
return max;
}
polynomial::var max_var(literal l) {
atom * a = m_atoms[l.var()];
if (a != nullptr)
return a->max_var();
else
return null_var;
}
/**
\brief Return the maximal variable in the given set of literals
*/
var max_var(unsigned sz, literal const * ls) {
var max = null_var;
for (unsigned i = 0; i < sz; i++) {
literal l = ls[i];
atom * a = m_atoms[l.var()];
if (a != nullptr) {
var x = a->max_var();
SASSERT(x != null_var);
if (max == null_var || x > max)
max = x;
}
}
return max;
}
/**
\brief Move the polynomials in q in ps that do not contain x to qs.
*/
void keep_p_x(polynomial_ref_vector & ps, var x, polynomial_ref_vector & qs) {
unsigned sz = ps.size();
unsigned j = 0;
for (unsigned i = 0; i < sz; i++) {
poly * q = ps.get(i);
if (max_var(q) != x) {
qs.push_back(q);
}
else {
ps.set(j, q);
j++;
}
}
ps.shrink(j);
}
/**
\brief Add factors of p to todo
*/
void insert_fresh_factors_in_todo(polynomial_ref & p) {
if (is_const(p))
return;
elim_vanishing(p);
if (is_const(p))
return;
if (m_factor) {
restore_factors _restore(m_factors, m_factors_save);
factor(p, m_factors);
TRACE(nlsat_explain, display(tout << "adding factors of\n", p); tout << "\n" << m_factors << "\n";);
polynomial_ref f(m_pm);
for (unsigned i = 0; i < m_factors.size(); i++) {
f = m_factors.get(i);
elim_vanishing(f);
if (!is_const(f)) {
TRACE(nlsat_explain, tout << "adding factor:\n"; display(tout, f); tout << "\n";);
m_todo.insert(f);
}
}
}
else {
m_todo.insert(p);
}
}
// For each p in ps add the leading coefficent to the projection,
void add_lc(polynomial_ref_vector &ps, var x) {
polynomial_ref p(m_pm);
polynomial_ref coeff(m_pm);
// Add coefficients based on well-orientedness
for (unsigned i = 0; i < ps.size(); i++) {
p = ps.get(i);
unsigned k_deg = m_pm.degree(p, x);
if (k_deg == 0) continue;
// p depends on x
TRACE(nlsat_explain, tout << "processing poly of degree " << k_deg << " w.r.t x" << x << ": "; display(tout, p) << "\n";);
coeff = m_pm.coeff(p, x, k_deg);
TRACE(nlsat_explain, tout << " coeff deg " << k_deg << ": "; display(tout, coeff) << "\n";);
insert_fresh_factors_in_todo(coeff);
}
}
void psc_resultant_sample(polynomial_ref_vector &ps, var x, polynomial_ref_vector & samples){
polynomial_ref p(m_pm);
polynomial_ref q(m_pm);
SASSERT(samples.size() <= 2);
for (unsigned i = 0; i < ps.size(); i++){
p = ps.get(i);
for (unsigned j = 0; j < samples.size(); j++){
q = samples.get(j);
if (!m_pm.eq(p, q)) {
psc(p, q, x);
}
}
}
}
void add_zero_assumption_on_factor(polynomial_ref& f) {
display(std::cout << "zero factors \n", f);
}
// this function also explains the value 0, if met
bool coeffs_are_zeroes(polynomial_ref &s) {
restore_factors _restore(m_factors, m_factors_save);
factor(s, m_factors);
unsigned num_factors = m_factors.size();
m_zero_fs.reset();
m_is_even.reset();
polynomial_ref f(m_pm);
bool have_zero = false;
for (unsigned i = 0; i < num_factors; i++) {
f = m_factors.get(i);
if (coeffs_are_zeroes_in_factor(f)) {
have_zero = true;
break;
}
}
if (!have_zero)
return false;
var x = max_var(f);
unsigned n = degree(f, x);
auto c = polynomial_ref(this->m_pm);
for (unsigned j = 0; j <= n; j++) {
c = m_pm.coeff(s, x, j);
SASSERT(sign(c) == 0);
ensure_sign(c);
}
return true;
}
bool coeffs_are_zeroes_in_factor(polynomial_ref & s) {
var x = max_var(s);
unsigned n = degree(s, x);
auto c = polynomial_ref(this->m_pm);
for (unsigned j = 0; j <= n; j++) {
c = m_pm.coeff(s, x, j);
if (sign(c) != 0)
return false;
}
return true;
}
/**
\brief Add v-psc(p, q, x) into m_todo
*/
void psc(polynomial_ref & p, polynomial_ref & q, var x) {
polynomial_ref_vector & S = m_psc_tmp;
polynomial_ref s(m_pm);
psc_chain(p, q, x, S);
unsigned sz = S.size();
TRACE(nlsat_explain, tout << "computing psc of\n"; display(tout, p); tout << "\n"; display(tout, q); tout << "\n";
for (unsigned i = 0; i < sz; ++i) {
s = S.get(i);
tout << "psc: " << s << "\n";
});
for (unsigned i = 0; i < sz; i++) {
s = S.get(i);
TRACE(nlsat_explain, display(tout << "processing psc(" << i << ")\n", s) << "\n";);
if (is_zero(s)) {
TRACE(nlsat_explain, tout << "skipping psc is the zero polynomial\n";);
continue;
}
if (is_const(s)) {
TRACE(nlsat_explain, tout << "done, psc is a constant\n";);
return;
}
if (is_zero(sign(s))) {
TRACE(nlsat_explain, tout << "psc vanished, adding zero assumption\n";);
add_zero_assumption(s);
continue;
}
TRACE(nlsat_explain,
tout << "adding v-psc of\n";
display(tout, p);
tout << "\n";
display(tout, q);
tout << "\n---->\n";
display(tout, s);
tout << "\n";);
// s did not vanish completely, but its leading coefficient may have vanished
insert_fresh_factors_in_todo(s);
return;
}
}
/**
\brief For each p in ps, add v-psc(x, p, p') into m_todo
\pre all polynomials in ps contain x
Remark: the leading coefficients do not vanish in the current model,
since all polynomials in ps were pre-processed using elim_vanishing.
*/
void psc_discriminant(polynomial_ref_vector & ps, var x) {
polynomial_ref p(m_pm);
polynomial_ref p_prime(m_pm);
unsigned sz = ps.size();
for (unsigned i = 0; i < sz; i++) {
p = ps.get(i);
if (degree(p, x) < 2)
continue;
p_prime = derivative(p, x);
psc(p, p_prime, x);
}
}
/**
\brief For each p and q in ps, p != q, add v-psc(x, p, q) into m_todo
\pre all polynomials in ps contain x
Remark: the leading coefficients do not vanish in the current model,
since all polynomials in ps were pre-processed using elim_vanishing.
*/
void psc_resultant(polynomial_ref_vector & ps, var x) {
polynomial_ref p(m_pm);
polynomial_ref q(m_pm);
unsigned sz = ps.size();
for (unsigned i = 0; i < sz - 1; i++) {
p = ps.get(i);
for (unsigned j = i + 1; j < sz; j++) {
q = ps.get(j);
psc(p, q, x);
}
}
}
void test_root_literal(atom::kind k, var y, unsigned i, poly * p, scoped_literal_vector& result) {
m_result = &result;
add_root_literal(k, y, i, p);
reset_already_added();
m_result = nullptr;
}
void add_root_literal(atom::kind k, var y, unsigned i, poly * p) {
polynomial_ref pr(p, m_pm);
TRACE(nlsat_explain,
display(tout << "x" << y << " " << k << "[" << i << "](", pr); tout << ")\n";);
if (!mk_linear_root(k, y, i, p) &&
!mk_quadratic_root(k, y, i, p)) {
bool_var b = m_solver.mk_root_atom(k, y, i, p);
literal l(b, true);
TRACE(nlsat_explain, tout << "adding literal\n"; display(tout, l); tout << "\n";);
add_literal(l);
}
}
/**
* literal can be expressed using a linear ineq_atom
*/
bool mk_linear_root(atom::kind k, var y, unsigned i, poly * p) {
scoped_mpz c(m_pm.m());
if (m_pm.degree(p, y) == 1 && m_pm.const_coeff(p, y, 1, c)) {
SASSERT(!m_pm.m().is_zero(c));
mk_linear_root(k, y, i, p, m_pm.m().is_neg(c));
return true;
}
return false;
}
/**
Create pseudo-linear root depending on the sign of the coefficient to y.
*/
bool mk_plinear_root(atom::kind k, var y, unsigned i, poly * p) {
if (m_pm.degree(p, y) != 1) {
return false;
}
polynomial_ref c(m_pm);
c = m_pm.coeff(p, y, 1);
int s = sign(c);
if (s == 0) {
return false;
}
ensure_sign(c);
mk_linear_root(k, y, i, p, s < 0);
return true;
}
/**
Encode root conditions for quadratic polynomials.
Basically implements Thom's theorem. The roots are characterized by the sign of polynomials and their derivatives.
b^2 - 4ac = 0:
- there is only one root, which is -b/2a.
- relation to root is a function of the sign of
- 2ax + b
b^2 - 4ac > 0:
- assert i == 1 or i == 2
- relation to root is a function of the signs of:
- 2ax + b
- ax^2 + bx + c
*/
bool mk_quadratic_root(atom::kind k, var y, unsigned i, poly * p) {
if (m_pm.degree(p, y) != 2) {
return false;
}
if (i != 1 && i != 2) {
return false;
}
SASSERT(m_assignment.is_assigned(y));
polynomial_ref A(m_pm), B(m_pm), C(m_pm), q(m_pm), p_diff(m_pm), yy(m_pm);
A = m_pm.coeff(p, y, 2);
B = m_pm.coeff(p, y, 1);
C = m_pm.coeff(p, y, 0);
// TBD throttle based on degree of q?
q = (B*B) - (4*A*C);
yy = m_pm.mk_polynomial(y);
p_diff = 2*A*yy + B;
p_diff = m_pm.normalize(p_diff);
int sq = ensure_sign(q);
if (sq < 0) {
return false;
}
int sa = ensure_sign(A);
if (sa == 0) {
q = B*yy + C;
return mk_plinear_root(k, y, i, q);
}
ensure_sign(p_diff);
if (sq > 0) {
polynomial_ref pr(p, m_pm);
ensure_sign(pr);
}
return true;
}
int ensure_sign(polynomial_ref & p) {
#if 0
polynomial_ref f(m_pm);
factor(p, m_factors);
m_is_even.reset();
unsigned num_factors = m_factors.size();
int s = 1;
for (unsigned i = 0; i < num_factors; i++) {
f = m_factors.get(i);
s *= sign(f);
m_is_even.push_back(false);
}
if (num_factors > 0) {
atom::kind k = atom::EQ;
if (s == 0) k = atom::EQ;
if (s < 0) k = atom::LT;
if (s > 0) k = atom::GT;
bool_var b = m_solver.mk_ineq_atom(k, num_factors, m_factors.c_ptr(), m_is_even.c_ptr());
add_literal(literal(b, true));
}
return s;
#else
int s = sign(p);
if (!is_const(p)) {
TRACE(nlsat_explain, tout << p << "\n";);
add_simple_assumption(s == 0 ? atom::EQ : (s < 0 ? atom::LT : atom::GT), p);
}
return s;
#endif
}
/**
Auxiliary function to linear roots.
y > root[1](-2*y - z)
y > -z/2
y + z/2 > 0
2y + z > 0
*/
void mk_linear_root(atom::kind k, var y, unsigned i, poly * p, bool mk_neg) {
TRACE(nlsat_explain, display_var(tout, y); m_pm.display(tout << ": ", p, m_solver.display_proc()); tout << "\n");
polynomial_ref p_prime(m_pm);
p_prime = p;
bool lsign = false;
if (mk_neg)
p_prime = neg(p_prime);
p = p_prime.get();
switch (k) {
case atom::ROOT_EQ: k = atom::EQ; lsign = false; break;
case atom::ROOT_LT: k = atom::LT; lsign = false; break;
case atom::ROOT_GT: k = atom::GT; lsign = false; break;
case atom::ROOT_LE: k = atom::GT; lsign = true; break;
case atom::ROOT_GE: k = atom::LT; lsign = true; break;
default:
UNREACHABLE();
break;
}
add_simple_assumption(k, p, lsign);
}
void cac_add_cell_lits(polynomial_ref_vector & ps, var y, polynomial_ref_vector & res) {
res.reset();
SASSERT(m_assignment.is_assigned(y));
bool lower_inf = true;
bool upper_inf = true;
scoped_anum_vector & roots = m_roots_tmp;
scoped_anum lower(m_am);
scoped_anum upper(m_am);
anum const & y_val = m_assignment.value(y);
TRACE(nlsat_explain, tout << "adding literals for "; display_var(tout, y); tout << " -> ";
m_am.display_decimal(tout, y_val); tout << "\n";);
polynomial_ref p_lower(m_pm);
unsigned i_lower = UINT_MAX;
polynomial_ref p_upper(m_pm);
unsigned i_upper = UINT_MAX;
polynomial_ref p(m_pm);
unsigned sz = ps.size();
for (unsigned k = 0; k < sz; k++) {
p = ps.get(k);
if (max_var(p) != y)
continue;
roots.reset();
// Variable y is assigned in m_assignment. We must temporarily unassign it.
// Otherwise, the isolate_roots procedure will assume p is a constant polynomial.
m_am.isolate_roots(p, undef_var_assignment(m_assignment, y), roots);
unsigned num_roots = roots.size();
bool all_lt = true;
for (unsigned i = 0; i < num_roots; i++) {
int s = m_am.compare(y_val, roots[i]);
TRACE(nlsat_explain,
m_am.display_decimal(tout << "comparing root: ", roots[i]); tout << "\n";
m_am.display_decimal(tout << "with y_val:", y_val);
tout << "\nsign " << s << "\n";
tout << "poly: " << p << "\n";
);
if (s == 0) {
// y_val == roots[i]
// add literal
// ! (y = root_i(p))
add_root_literal(atom::ROOT_EQ, y, i+1, p);
res.push_back(p);
return;
}
else if (s < 0) {
// y_val < roots[i]
if (i > 0) {
// y_val > roots[j]
int j = i - 1;