forked from berkeley-abc/abc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcadical_solver.cpp
More file actions
1774 lines (1546 loc) · 52.4 KB
/
cadical_solver.cpp
File metadata and controls
1774 lines (1546 loc) · 52.4 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
#include "global.h"
#include "internal.hpp"
ABC_NAMESPACE_IMPL_START
/*------------------------------------------------------------------------*/
namespace CaDiCaL {
/*------------------------------------------------------------------------*/
// See corresponding header file 'cadical.hpp' (!) for more information.
//
// Again, to avoid confusion, note that, 'cadical.hpp' is the header file of
// this file 'solver.cpp', since we want to call the application and main
// file 'cadical.cpp', while at the same time using 'cadical.hpp' as the
// main header file of the library (and not 'solver.hpp').
/*------------------------------------------------------------------------*/
#ifdef LOGGING
// Needs to be kept in sync with the color schemes used in 'logging.cpp'.
//
#define api_code blue_code // API call color
#define log_code magenta_code // standard/default logging color
#define emph_code bright_magenta_code // emphasized logging color
#endif
/*------------------------------------------------------------------------*/
// Log state transitions.
#define STATE(S) \
do { \
CADICAL_assert (is_power_of_two (S)); \
if (_state == S) \
break; \
_state = S; \
LOG ("API enters state %s" #S "%s", tout.emph_code (), \
tout.normal_code ()); \
} while (0)
void Solver::transition_to_steady_state () {
if (state () == CONFIGURING) {
LOG ("API leaves state %sCONFIGURING%s", tout.emph_code (),
tout.normal_code ());
if (internal->opts.check && internal->opts.checkproof) {
internal->check ();
}
} else if (state () == SATISFIED) {
LOG ("API leaves state %sSATISFIED%s", tout.emph_code (),
tout.normal_code ());
external->reset_assumptions ();
external->reset_concluded ();
external->reset_constraint ();
} else if (state () == UNSATISFIED) {
LOG ("API leaves state %sUNSATISFIED%s", tout.emph_code (),
tout.normal_code ());
external->reset_assumptions ();
external->reset_concluded ();
external->reset_constraint ();
} else if (state() == INCONCLUSIVE) {
external->reset_assumptions ();
external->reset_concluded ();
external->reset_constraint ();
}
if (state () != STEADY)
STATE (STEADY);
}
/*------------------------------------------------------------------------*/
#ifdef LOGGING
/*------------------------------------------------------------------------*/
// The following logging code is useful for debugging mostly (or trying to
// understand what the solver is actually doing). It needs to be enabled
// during configuration using the '-l' option for './configure', which
// forces 'LOGGING' to be defined during compilation. This includes all the
// logging code, which then still needs to enabled during run-time with the
// '-l' or 'log' option.
static void log_api_call (Internal *internal, const char *name,
const char *suffix) {
Logger::log (internal, "API call %s'%s ()'%s %s", tout.api_code (), name,
tout.log_code (), suffix);
}
static void log_api_call (Internal *internal, const char *name, int arg,
const char *suffix) {
Logger::log (internal, "API call %s'%s (%d)'%s %s", tout.api_code (),
name, arg, tout.log_code (), suffix);
}
static void log_api_call (Internal *internal, const char *name,
const char *arg, const char *suffix) {
Logger::log (internal, "API call %s'%s (\"%s\")'%s %s", tout.api_code (),
name, arg, tout.log_code (), suffix);
}
static void log_api_call (Internal *internal, const char *name,
const char *a1, int a2, const char *s) {
Logger::log (internal, "API call %s'%s (\"%s\", %d)'%s %s",
tout.api_code (), name, a1, a2, tout.log_code (), s);
}
/*------------------------------------------------------------------------*/
// We factored out API call begin/end logging and use overloaded functions.
static void log_api_call_begin (Internal *internal, const char *name) {
Logger::log_empty_line (internal);
log_api_call (internal, name, "started");
}
static void log_api_call_begin (Internal *internal, const char *name,
int arg) {
Logger::log_empty_line (internal);
log_api_call (internal, name, arg, "started");
}
static void log_api_call_begin (Internal *internal, const char *name,
const char *arg) {
Logger::log_empty_line (internal);
log_api_call (internal, name, arg, "started");
}
static void log_api_call_begin (Internal *internal, const char *name,
const char *arg1, int arg2) {
Logger::log_empty_line (internal);
log_api_call (internal, name, arg1, arg2, "started");
}
/*------------------------------------------------------------------------*/
static void log_api_call_end (Internal *internal, const char *name) {
log_api_call (internal, name, "succeeded");
}
static void log_api_call_end (Internal *internal, const char *name,
int lit) {
log_api_call (internal, name, lit, "succeeded");
}
static void log_api_call_end (Internal *internal, const char *name,
const char *arg) {
Logger::log_empty_line (internal);
log_api_call (internal, name, arg, "succeeded");
}
static void log_api_call_end (Internal *internal, const char *name,
const char *arg, bool res) {
log_api_call (internal, name, arg, res ? "succeeded" : "failed");
}
static void log_api_call_end (Internal *internal, const char *name,
const char *arg, int val, bool res) {
log_api_call (internal, name, arg, val, res ? "succeeded" : "failed");
}
static void log_api_call_returns (Internal *internal, const char *name,
bool res) {
log_api_call (internal, name, res ? "returns 'true'" : "returns 'false'");
}
static void log_api_call_returns (Internal *internal, const char *name,
int res) {
char fmt[32];
snprintf (fmt, sizeof fmt, "returns '%d'", res);
log_api_call (internal, name, fmt);
}
static void log_api_call_returns (Internal *internal, const char *name,
int64_t res) {
char fmt[32];
snprintf (fmt, sizeof fmt, "returns '%" PRId64 "'", res);
log_api_call (internal, name, fmt);
}
static void log_api_call_returns (Internal *internal, const char *name,
int lit, int res) {
char fmt[32];
snprintf (fmt, sizeof fmt, "returns '%d'", res);
log_api_call (internal, name, lit, fmt);
}
static void log_api_call_returns (Internal *internal, const char *name,
const char *arg, bool res) {
log_api_call (internal, name, arg,
res ? "returns 'true'" : "returns 'false'");
}
static void log_api_call_returns (Internal *internal, const char *name,
int lit, bool res) {
log_api_call (internal, name, lit,
res ? "returns 'true'" : "returns 'false'");
}
static void log_api_call_returns (Internal *internal, const char *name,
const char *arg, const char *res) {
Logger::log (internal, "API call %s'%s (\"%s\")'%s returns '%s'",
tout.api_code (), name, arg, tout.log_code (),
res ? res : "<null>");
}
static void log_api_call_returns (Internal *internal, const char *name,
const char *arg1, int arg2,
const char *res) {
Logger::log (internal, "API call %s'%s (\"%s\", %d)'%s returns '%s'",
tout.api_code (), name, arg1, arg2, tout.log_code (),
res ? res : "<null>");
}
/*------------------------------------------------------------------------*/
#define LOG_API_CALL_BEGIN(...) \
do { \
if (!internal->opts.log) \
break; \
log_api_call_begin (internal, __VA_ARGS__); \
} while (0)
#define LOG_API_CALL_END(...) \
do { \
if (!internal->opts.log) \
break; \
log_api_call_end (internal, __VA_ARGS__); \
} while (0)
#define LOG_API_CALL_RETURNS(...) \
do { \
if (!internal->opts.log) \
break; \
log_api_call_returns (internal, __VA_ARGS__); \
} while (0)
/*------------------------------------------------------------------------*/
#else // end of 'then' part of 'ifdef LOGGING'
/*------------------------------------------------------------------------*/
#define LOG_API_CALL_BEGIN(...) \
do { \
} while (0)
#define LOG_API_CALL_END(...) \
do { \
} while (0)
#define LOG_API_CALL_RETURNS(...) \
do { \
} while (0)
/*------------------------------------------------------------------------*/
#endif // end of 'else' part of 'ifdef LOGGING'
/*------------------------------------------------------------------------*/
/*------------------------------------------------------------------------*/
#ifndef CADICAL_NTRACING
/*------------------------------------------------------------------------*/
#define TRACE(...) \
do { \
/*if ((this == 0)) break; */ /* gcc-12 produces warning */ \
if ((internal == 0)) \
break; \
LOG_API_CALL_BEGIN (__VA_ARGS__); \
if (!trace_api_file) \
break; \
trace_api_call (__VA_ARGS__); \
} while (0)
void Solver::trace_api_call (const char *s0) const {
CADICAL_assert (trace_api_file);
LOG ("TRACE %s", s0);
fprintf (trace_api_file, "%s\n", s0);
fflush (trace_api_file);
}
void Solver::trace_api_call (const char *s0, int i1) const {
CADICAL_assert (trace_api_file);
LOG ("TRACE %s %d", s0, i1);
fprintf (trace_api_file, "%s %d\n", s0, i1);
fflush (trace_api_file);
}
void Solver::trace_api_call (const char *s0, const char *s1) const {
CADICAL_assert (trace_api_file);
LOG ("TRACE %s %s", s0, s1);
fprintf (trace_api_file, "%s %s\n", s0, s1);
fflush (trace_api_file);
}
void Solver::trace_api_call (const char *s0, const char *s1, int i2) const {
CADICAL_assert (trace_api_file);
LOG ("TRACE %s %s %d", s0, s1, i2);
fprintf (trace_api_file, "%s %s %d\n", s0, s1, i2);
fflush (trace_api_file);
}
/*------------------------------------------------------------------------*/
// The global 'tracing_api_calls_through_environment_variable_method' flag
// is used to ensure that only one solver traces to a file. Otherwise the
// method to use an environment variable to point to the trace file is
// bogus, since those different solver instances would all write to the same
// file producing garbage. A more sophisticated solution would use a
// different mechanism to tell the solver to which file to trace to, but in
// our experience it is quite convenient to get traces out of applications
// which use the solver as library by just setting an environment variable
// without requiring to change any application code.
//
static bool tracing_api_calls_through_environment_variable_method;
/*------------------------------------------------------------------------*/
#else // CADICAL_NTRACING
/*------------------------------------------------------------------------*/
#define TRACE(...) \
do { \
} while (0)
/*------------------------------------------------------------------------*/
#endif
/*------------------------------------------------------------------------*/
static bool tracing_nb_lidrup_env_var_method = false;
Solver::Solver () {
#ifndef CADICAL_NTRACING
const char *path = getenv ("CADICAL_API_TRACE");
if (!path)
path = getenv ("CADICALAPITRACE");
if (path) {
if (tracing_api_calls_through_environment_variable_method)
FATAL ("can not trace API calls of two solver instances "
"using environment variable 'CADICAL_API_TRACE'");
if (!(trace_api_file = fopen (path, "w")))
FATAL ("failed to open file '%s' to trace API calls "
"using environment variable 'CADICAL_API_TRACE'",
path);
close_trace_api_file = true;
tracing_api_calls_through_environment_variable_method = true;
} else {
tracing_api_calls_through_environment_variable_method = false;
close_trace_api_file = false;
trace_api_file = 0;
}
#endif
adding_clause = false;
adding_constraint = false;
_state = INITIALIZING;
internal = new Internal ();
DeferDeletePtr<Internal> delete_internal (internal);
TRACE ("init");
external = new External (internal);
DeferDeletePtr<External> delete_external (external);
STATE (CONFIGURING);
#ifndef CADICAL_NTRACING
if (tracing_api_calls_through_environment_variable_method)
message ("tracing API calls to '%s'", path);
#endif
const char *lidrup_path = getenv ("CADICAL_LIDRUP_TRACE");
if (!lidrup_path)
lidrup_path = getenv ("CADICALLIDRUPTRACE");
if (lidrup_path) {
// if (tracing_nb_lidrup_env_var_method)
// FATAL ("can not trace LIDRUP of two solver instances "
// "using environment variable 'CADICAL_LIDRUP_TRACE'");
// Here we use the solver interface to setup non-binary IDRUP tracing to
// the defined file. Options set by the user can and will overwrite
// these settings if neeed be.
set ("lidrup", 1);
set ("binary", 0);
trace_proof (lidrup_path);
tracing_nb_lidrup_env_var_method = true;
} else {
tracing_nb_lidrup_env_var_method = false;
}
delete_internal.release ();
delete_external.release ();
}
Solver::~Solver () {
TRACE ("reset");
REQUIRE_VALID_OR_SOLVING_STATE ();
STATE (DELETING);
tracing_nb_lidrup_env_var_method = false;
#ifdef LOGGING
//
// After deleting 'internal' logging does not work anymore.
//
bool logging = internal->opts.log;
int level = internal->level;
string prefix = internal->prefix;
#endif
delete internal;
delete external;
#ifndef CADICAL_NTRACING
if (close_trace_api_file) {
close_trace_api_file = false;
CADICAL_assert (trace_api_file);
CADICAL_assert (tracing_api_calls_through_environment_variable_method);
fclose (trace_api_file);
tracing_api_calls_through_environment_variable_method = false;
}
#endif
#ifdef LOGGING
//
// Need to log success of this API call manually.
//
if (logging) {
printf ("%s%sLOG %s%d%s API call %s'reset ()'%s succeeded%s\n",
prefix.c_str (), tout.log_code (), tout.emph_code (), level,
tout.log_code (), tout.api_code (), tout.log_code (),
tout.normal_code ());
fflush (stdout);
}
#endif
}
/*------------------------------------------------------------------------*/
int Solver::vars () {
TRACE ("vars");
REQUIRE_VALID_OR_SOLVING_STATE ();
int res = external->max_var;
LOG_API_CALL_RETURNS ("vars", res);
return res;
}
void Solver::reserve (int min_max_var) {
TRACE ("reserve", min_max_var);
REQUIRE_VALID_STATE ();
transition_to_steady_state ();
external->reset_extended ();
external->init (min_max_var);
LOG_API_CALL_END ("reserve", min_max_var);
}
int Solver::reserve_difference (int number_of_vars) {
TRACE ("reserve_difference", number_of_vars);
REQUIRE_VALID_STATE ();
transition_to_steady_state ();
external->reset_extended ();
int new_max_var = external->max_var + number_of_vars;
external->init (new_max_var);
LOG_API_CALL_END ("reserve_difference", number_of_vars);
return new_max_var;
}
/*------------------------------------------------------------------------*/
#ifndef CADICAL_NTRACING
void Solver::trace_api_calls (FILE *file) {
LOG_API_CALL_BEGIN ("trace_api_calls");
REQUIRE_VALID_STATE ();
REQUIRE (file != 0, "invalid zero file argument");
REQUIRE (!tracing_api_calls_through_environment_variable_method,
"already tracing API calls "
"using environment variable 'CADICAL_API_TRACE'");
REQUIRE (!trace_api_file, "called twice");
trace_api_file = file;
LOG_API_CALL_END ("trace_api_calls");
trace_api_call ("init");
}
#endif
/*------------------------------------------------------------------------*/
bool Solver::is_valid_option (const char *name) {
return Options::has (name);
}
bool Solver::is_preprocessing_option (const char *name) {
return Options::is_preprocessing_option (name);
}
bool Solver::is_valid_long_option (const char *arg) {
string name;
int tmp;
return Options::parse_long_option (arg, name, tmp);
}
int Solver::get (const char *arg) {
REQUIRE_VALID_OR_SOLVING_STATE ();
return internal->opts.get (arg);
}
bool Solver::set (const char *arg, int val) {
TRACE ("set", arg, val);
REQUIRE_VALID_STATE ();
if (strcmp (arg, "log") && strcmp (arg, "quiet") &&
strcmp (arg, "report") && strcmp (arg, "verbose")) {
REQUIRE (
state () == CONFIGURING,
"can only set option 'set (\"%s\", %d)' right after initialization",
arg, val);
}
bool res = internal->opts.set (arg, val);
LOG_API_CALL_END ("set", arg, val, res);
return res;
}
bool Solver::set_long_option (const char *arg) {
LOG_API_CALL_BEGIN ("set", arg);
REQUIRE_VALID_STATE ();
REQUIRE (state () == CONFIGURING,
"can only set option '%s' right after initialization", arg);
bool res;
if (arg[0] != '-' || arg[1] != '-')
res = false;
else {
int val;
string name;
res = Options::parse_long_option (arg, name, val);
if (res)
set (name.c_str (), val);
}
LOG_API_CALL_END ("set", arg, res);
return res;
}
void Solver::optimize (int arg) {
LOG_API_CALL_BEGIN ("optimize", arg);
REQUIRE_VALID_STATE ();
internal->opts.optimize (arg);
LOG_API_CALL_END ("optimize", arg);
}
bool Solver::limit (const char *arg, int val) {
TRACE ("limit", arg, val);
REQUIRE_VALID_STATE ();
bool res = internal->limit (arg, val);
LOG_API_CALL_END ("limit", arg, val, res);
return res;
}
bool Solver::is_valid_limit (const char *arg) {
return Internal::is_valid_limit (arg);
}
void Solver::prefix (const char *str) {
LOG_API_CALL_BEGIN ("prefix", str);
REQUIRE_VALID_OR_SOLVING_STATE ();
internal->prefix = str;
LOG_API_CALL_END ("prefix", str);
}
bool Solver::is_valid_configuration (const char *name) {
return Config::has (name);
}
bool Solver::configure (const char *name) {
TRACE ("configure", name);
LOG_API_CALL_BEGIN ("configure", name);
REQUIRE_VALID_STATE ();
REQUIRE (state () == CONFIGURING,
"can only set configuration '%s' right after initialization",
name);
bool res = Config::set (internal->opts, name);
LOG_API_CALL_END ("configure", name, res);
return res;
}
/*===== IPASIR BEGIN =====================================================*/
void Solver::add (int lit) {
TRACE ("add", lit);
REQUIRE_VALID_STATE ();
if (lit)
REQUIRE_VALID_LIT (lit);
transition_to_steady_state ();
external->add (lit);
adding_clause = lit;
if (adding_clause)
STATE (ADDING);
else if (!adding_constraint)
STATE (STEADY);
LOG_API_CALL_END ("add", lit);
}
void Solver::clause (int a) {
REQUIRE_VALID_LIT (a);
add (a), add (0);
}
void Solver::clause (int a, int b) {
REQUIRE_VALID_LIT (a);
REQUIRE_VALID_LIT (b);
add (a), add (b), add (0);
}
void Solver::clause (int a, int b, int c) {
REQUIRE_VALID_LIT (a);
REQUIRE_VALID_LIT (b);
REQUIRE_VALID_LIT (c);
add (a), add (b), add (c), add (0);
}
void Solver::clause (int a, int b, int c, int d) {
REQUIRE_VALID_LIT (a);
REQUIRE_VALID_LIT (b);
REQUIRE_VALID_LIT (c);
REQUIRE_VALID_LIT (d);
add (a), add (b), add (c), add (d), add (0);
}
void Solver::clause (int a, int b, int c, int d, int e) {
REQUIRE_VALID_LIT (a);
REQUIRE_VALID_LIT (b);
REQUIRE_VALID_LIT (c);
REQUIRE_VALID_LIT (d);
REQUIRE_VALID_LIT (e);
add (a), add (b), add (c), add (d), add (e), add (0);
}
void Solver::clause (const int *lits, size_t size) {
REQUIRE (!size || lits,
"first argument 'lits' zero while second argument 'size' not");
const int *end = lits + size;
for (const int *p = lits; p != end; p++) {
const int lit = *p;
REQUIRE_VALID_LIT (lit);
add (lit);
}
add (0);
}
void Solver::clause (const std::vector<int> &lits) {
for (auto lit : lits) {
REQUIRE_VALID_LIT (lit);
add (lit);
}
add (0);
}
bool Solver::inconsistent () { return internal->unsat; }
void Solver::constrain (int lit) {
TRACE ("constrain", lit);
REQUIRE_VALID_STATE ();
if (lit)
REQUIRE_VALID_LIT (lit);
transition_to_steady_state ();
external->constrain (lit);
adding_constraint = lit;
if (adding_constraint)
STATE (ADDING);
else if (!adding_clause)
STATE (STEADY);
LOG_API_CALL_END ("constrain", lit);
}
void Solver::assume (int lit) {
TRACE ("assume", lit);
REQUIRE_VALID_STATE ();
REQUIRE_VALID_LIT (lit);
transition_to_steady_state ();
external->assume (lit);
LOG_API_CALL_END ("assume", lit);
}
int Solver::lookahead () {
TRACE ("lookahead");
REQUIRE_VALID_OR_SOLVING_STATE ();
int lit = external->lookahead ();
TRACE ("lookahead");
return lit;
}
Solver::CubesWithStatus Solver::generate_cubes (int depth, int min_depth) {
TRACE ("lookahead_cubes");
REQUIRE_VALID_OR_SOLVING_STATE ();
auto cubes = external->generate_cubes (depth, min_depth);
TRACE ("lookahead_cubes");
CubesWithStatus cubes2;
cubes2.status = cubes.status;
cubes2.cubes = cubes.cubes;
return cubes2;
}
void Solver::reset_assumptions () {
TRACE ("reset_assumptions");
REQUIRE_VALID_STATE ();
transition_to_steady_state ();
external->reset_assumptions ();
external->reset_concluded ();
LOG_API_CALL_END ("reset_assumptions");
}
void Solver::reset_constraint () {
TRACE ("reset_constraint");
REQUIRE_VALID_STATE ();
transition_to_steady_state ();
external->reset_constraint ();
external->reset_concluded ();
LOG_API_CALL_END ("reset_constraint");
}
/*------------------------------------------------------------------------*/
int Solver::propagate () {
TRACE ("propagate_assumptions");
REQUIRE_VALID_STATE ();
transition_to_steady_state ();
const int res = external->propagate_assumptions ();
if (tracing_nb_lidrup_env_var_method)
flush_proof_trace (true);
LOG_API_CALL_RETURNS ("propagate_assumptions", res);
if (res == 10)
STATE (SATISFIED);
else if (res == 20)
STATE (UNSATISFIED);
else
STATE (INCONCLUSIVE);
return res;
}
void Solver::implied (std::vector<int> &entrailed) {
TRACE ("implied");
REQUIRE_VALID_STATE ();
REQUIRE (state () == INCONCLUSIVE,
"can only get implied literals only in unknown state");
external->conclude_unknown ();
external->implied (entrailed);
if (tracing_nb_lidrup_env_var_method)
flush_proof_trace (true);
LOG_API_CALL_RETURNS ("implied", (int) entrailed.size ());
}
/*------------------------------------------------------------------------*/
int Solver::call_external_solve_and_check_results (bool preprocess_only) {
transition_to_steady_state ();
CADICAL_assert (state () & READY);
STATE (SOLVING);
const int res = external->solve (preprocess_only);
if (res == 10)
STATE (SATISFIED);
else if (res == 20)
STATE (UNSATISFIED);
else
STATE (INCONCLUSIVE);
#if 0 // EXPENSIVE ALTERNATIVE ASSUMPTION CHECKING
// This checks that the set of failed assumptions form a core using the
// external 'copy (...)' function to copy the solver, which can be trusted
// less, since it involves copying the extension stack too. The
// 'External::check_assumptions_failing' is a better alternative and can
// be enabled by options too. We keep this code though to have an
// alternative failed assumption checking available for debugging.
//
if (res == 20 && !external->assumptions.empty ()) {
Solver checker;
// checking restored clauses does not work (because the clauses are not added)
checker.set("checkproof", 1);
checker.set("lrat", 0);
checker.prefix ("checker ");
copy (checker);
checker.set("log", 1);
for (const auto & lit : external->assumptions)
if (failed (lit))
checker.add (lit), checker.add (0);
if (checker.solve () != 20)
FATAL ("copying assumption checker failed");
}
#endif
if (!res) {
external->reset_assumptions ();
external->reset_constraint ();
external->reset_concluded ();
}
return res;
}
int Solver::solve () {
TRACE ("solve");
REQUIRE_READY_STATE ();
const int res = call_external_solve_and_check_results (false);
LOG_API_CALL_RETURNS ("solve", res);
if (tracing_nb_lidrup_env_var_method)
flush_proof_trace (true);
return res;
}
int Solver::simplify (int rounds) {
TRACE ("simplify", rounds);
REQUIRE_READY_STATE ();
REQUIRE (rounds >= 0, "negative number of simplification rounds '%d'",
rounds);
internal->limit ("preprocessing", rounds);
const int res = call_external_solve_and_check_results (true);
LOG_API_CALL_RETURNS ("simplify", rounds, res);
return res;
}
/*------------------------------------------------------------------------*/
int Solver::val (int lit) {
TRACE ("val", lit);
REQUIRE_VALID_STATE ();
REQUIRE_VALID_LIT (lit);
REQUIRE (state () == SATISFIED, "can only get value in satisfied state");
if (!external->extended)
external->extend ();
external->conclude_sat ();
int res = external->ival (lit);
LOG_API_CALL_RETURNS ("val", lit, res);
CADICAL_assert (state () == SATISFIED);
CADICAL_assert (res == lit || res == -lit);
return res;
}
bool Solver::flip (int lit) {
TRACE ("flip", lit);
REQUIRE_VALID_STATE ();
REQUIRE_VALID_LIT (lit);
REQUIRE (state () == SATISFIED, "can only flip value in satisfied state");
REQUIRE (!external->propagator,
"can only flip when no external propagator is present");
bool res = external->flip (lit);
LOG_API_CALL_RETURNS ("flip", lit, res);
CADICAL_assert (state () == SATISFIED);
return res;
}
bool Solver::flippable (int lit) {
TRACE ("flippable", lit);
REQUIRE_VALID_STATE ();
REQUIRE_VALID_LIT (lit);
REQUIRE (state () == SATISFIED, "can only flip value in satisfied state");
REQUIRE (!external->propagator,
"can only flip when no external propagator is present");
bool res = external->flippable (lit);
LOG_API_CALL_RETURNS ("flippable", lit, res);
CADICAL_assert (state () == SATISFIED);
return res;
}
bool Solver::failed (int lit) {
TRACE ("failed", lit);
REQUIRE_VALID_STATE ();
REQUIRE_VALID_LIT (lit);
REQUIRE (state () == UNSATISFIED,
"can only get failed assumptions in unsatisfied state");
bool res = external->failed (lit);
LOG_API_CALL_RETURNS ("failed", lit, res);
CADICAL_assert (state () == UNSATISFIED);
return res;
}
bool Solver::constraint_failed () {
TRACE ("constraint_failed");
REQUIRE_VALID_STATE ();
REQUIRE (state () == UNSATISFIED,
"can only determine if constraint failed in unsatisfied state");
bool res = external->failed_constraint ();
LOG_API_CALL_RETURNS ("constraint_failed", res);
CADICAL_assert (state () == UNSATISFIED);
return res;
}
int Solver::fixed (int lit) const {
TRACE ("fixed", lit);
REQUIRE_VALID_STATE ();
REQUIRE_VALID_LIT (lit);
int res = external->fixed (lit);
LOG_API_CALL_RETURNS ("fixed", lit, res);
return res;
}
void Solver::phase (int lit) {
TRACE ("phase", lit);
REQUIRE_VALID_OR_SOLVING_STATE ();
REQUIRE_VALID_LIT (lit);
external->phase (lit);
LOG_API_CALL_END ("phase", lit);
}
void Solver::unphase (int lit) {
TRACE ("unphase", lit);
REQUIRE_VALID_OR_SOLVING_STATE ();
REQUIRE_VALID_LIT (lit);
external->unphase (lit);
LOG_API_CALL_END ("unphase", lit);
}
/*------------------------------------------------------------------------*/
void Solver::terminate () {
LOG_API_CALL_BEGIN ("terminate");
REQUIRE_VALID_OR_SOLVING_STATE ();
external->terminate ();
LOG_API_CALL_END ("terminate");
}
void Solver::connect_terminator (Terminator *terminator) {
LOG_API_CALL_BEGIN ("connect_terminator");
REQUIRE_VALID_STATE ();
REQUIRE (terminator, "can not connect zero terminator");
#ifdef LOGGING
if (external->terminator)
LOG ("connecting new terminator (disconnecting previous one)");
else
LOG ("connecting new terminator (no previous one)");
#endif
external->terminator = terminator;
LOG_API_CALL_END ("connect_terminator");
}
void Solver::disconnect_terminator () {
LOG_API_CALL_BEGIN ("disconnect_terminator");
REQUIRE_VALID_STATE ();
#ifdef LOGGING
if (external->terminator)
LOG ("disconnecting previous terminator");
else
LOG ("ignoring to disconnect terminator (no previous one)");
#endif
external->terminator = 0;
LOG_API_CALL_END ("disconnect_terminator");
}
/*------------------------------------------------------------------------*/
void Solver::connect_learner (Learner *learner) {
LOG_API_CALL_BEGIN ("connect_learner");
REQUIRE_VALID_STATE ();
REQUIRE (learner, "can not connect zero learner");
#ifdef LOGGING
if (external->learner)
LOG ("connecting new learner (disconnecting previous one)");
else
LOG ("connecting new learner (no previous one)");
#endif
external->learner = learner;
LOG_API_CALL_END ("connect_learner");
}
void Solver::disconnect_learner () {
LOG_API_CALL_BEGIN ("disconnect_learner");
REQUIRE_VALID_STATE ();
#ifdef LOGGING
if (external->learner)
LOG ("disconnecting previous learner");
else
LOG ("ignoring to disconnect learner (no previous one)");
#endif
external->learner = 0;
LOG_API_CALL_END ("disconnect_learner");
}
/*===== IPASIR END =======================================================*/
void Solver::connect_fixed_listener (
FixedAssignmentListener *fixed_listener) {
LOG_API_CALL_BEGIN ("connect_fixed_listener");
REQUIRE_VALID_STATE ();
REQUIRE (fixed_listener, "can not connect zero fixed listener");
#ifdef LOGGING
if (external->fixed_listener)
LOG ("connecting new listener of fixed assignments (disconnecting "
"previous one)");
else
LOG ("connecting new listener of fixed assigments (no previous one)");
#endif
if (external->fixed_listener)
disconnect_fixed_listener ();
external->fixed_listener = fixed_listener;
// Listeners are treated as real-time listeners, thus previously found
// fixed assignments are not sent out (would be rather expensive to
// recover it retrospect, see external_propagate.cpp/get_fixed_literals ()
// function).
LOG_API_CALL_END ("connect_fixed_listener");
}
void Solver::disconnect_fixed_listener () {
LOG_API_CALL_BEGIN ("disconnect_fixed_listener");
REQUIRE_VALID_STATE ();
#ifdef LOGGING
if (external->fixed_listener)
LOG ("disconnecting previous listener of fixed assignments");
else
LOG ("ignoring to disconnect listener of fixed assignments (no "
"previous one)");
#endif
external->fixed_listener = 0;
LOG_API_CALL_END ("disconnect_fixed_listener");
}