Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,9 @@ if(BUILD_TESTING)
target_link_libraries(kissat-allocator-test PRIVATE kissat)
add_test(NAME allocator-failure COMMAND kissat-allocator-test)
set_tests_properties(allocator-failure PROPERTIES TIMEOUT 30 LABELS incremental)
add_test(NAME lucky-interruption
COMMAND "${Python3_EXECUTABLE}"
"${CMAKE_CURRENT_SOURCE_DIR}/test/lucky-interruption.py"
--library "$<TARGET_FILE:${_kissat_test_library}>")
set_tests_properties(lucky-interruption PROPERTIES TIMEOUT 30 LABELS incremental)
endif()
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,31 @@ inprocessing limits, and the stable restart schedule survive repeated solves.
Statistics include `incremental_restorations`, `incremental_restored_literals`,
`incremental_reused_solves`, and `incremental_reused_levels`.

The optional `incrementallucky` search tries greedy SAT completions without
copying the formula. It is disabled by default (0). Mode 1 tries a forward
false-first walk, mode 2 tries four combinations of forward/reverse order and
false/true polarity, mode 3 only backtracks to the assumption prefix as a
diagnostic control, and mode 4 tries the four walks above the current search
trail. Every mode preserves the assumption prefix. A conflicting choice is
flipped once; an unsuccessful walk backtracks without learning conditional
root units, and ordinary CDCL search resumes. Only a complete propagated
assignment can supply a SAT answer. The header capability macro
`KISSAT_HAS_INCREMENTAL_GREEDY` identifies these options.

By default an attempt requires 100,000 original clauses and either 100 new
conflicts or 10,000 new decisions in that solve. The corresponding options
are `incrementalluckyminclauses`, `incrementalluckyinit` and
`incrementalluckydecisions`. Each solve makes at most one attempt, with a
250 ms soft allowance (`incrementalluckymilliseconds`) and termination
polling between propagation calls. Explicit conflict or decision limits
bypass the optional walk. After failure, another attempt requires at least
100,000 new original clauses and four times the previous attempt's clause
count; `incrementalluckygrowth=1` keeps only the absolute growth requirement.
`incrementalluckynewcontext=1` also permits retries after assumptions change.
`incrementalluckyminunassigned` can gate attempts on the remaining variables,
and `incrementalluckyphasecache=1` saves phases after a successful completion.
Statistics report attempts, conflicts, successful completions and timeouts.

Factoring introduces external variable IDs. An incremental API client must
allocate fresh IDs beyond both its already declared IDs and
`kissat_maximum_variable`, or disable `factor` before the first solve.
Expand Down
3 changes: 3 additions & 0 deletions src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,9 @@ int kissat_solve (kissat *solver) {
PEEK_STACK (solver->previous_assumptions, common) ==
PEEK_STACK (solver->assumptions, common))
common++;
if (GET_OPTION (incrementalluckynewcontext) &&
(common != previous || common != current))
solver->incremental_lucky_failed = false;
unsigned keep = 0;
size_t consumed = 0;
const bool all = GET_OPTION (incrementalreuse) == 2 &&
Expand Down
2 changes: 2 additions & 0 deletions src/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ struct kissat {
bool reusable;
bool assumptions_complete;
uint64_t solve_calls;
uint64_t incremental_lucky_last_clauses;
bool incremental_lucky_failed;
int result;
ints pending;
ints assumptions;
Expand Down
2 changes: 2 additions & 0 deletions src/kissat.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#define _kissat_h_INCLUDED

#define KISSAT_HAS_INCREMENTAL 1
#define KISSAT_HAS_INTERRUPTIBLE_LUCKY 1
#define KISSAT_HAS_INCREMENTAL_GREEDY 1

#include <stddef.h>

Expand Down
1 change: 1 addition & 0 deletions src/kitten.c
Original file line number Diff line number Diff line change
Expand Up @@ -1726,6 +1726,7 @@ static void reset_core (kitten *kitten) {
if (is_core_klause (c))
unset_core_klause (c), reset++;
LOG ("reset %zu core clauses", reset);
(void) reset;
CLEAR_STACK (kitten->core);
}

Expand Down
135 changes: 134 additions & 1 deletion src/lucky.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,99 @@
#include "print.h"
#include "proprobe.h"
#include "report.h"
#include "resources.h"
#include "terminate.h"

static int assumption_walk (kissat *solver, unsigned base, bool reverse,
bool positive, double deadline) {
const size_t size = SIZE_STACK (solver->import);
for (size_t offset = 1; offset < size; offset++) {
if (!(offset & 1023) && (TERMINATED (lucky_terminated_1) ||
kissat_wall_clock_time () >= deadline))
return 0;
const size_t position = reverse ? size - offset : offset;
const import *import = &PEEK_STACK (solver->import, position);
if (!import->imported || import->eliminated)
continue;
unsigned lit = import->lit;
if (!ACTIVE (IDX (lit)) || VALUE (lit))
continue;
if (!positive)
lit = NOT (lit);
kissat_internal_assume (solver, lit);
clause *conflict = kissat_probing_propagate (solver, 0, true);
if (!conflict)
continue;
INC (incremental_lucky_conflicts);
assert (solver->level > base);
if (solver->level <= base)
return 0;
kissat_backtrack_without_updating_phases (solver, solver->level - 1);
kissat_internal_assume (solver, NOT (lit));
conflict = kissat_probing_propagate (solver, 0, true);
if (conflict) {
INC (incremental_lucky_conflicts);
return 0;
}
}
return solver->unassigned ? 0 : 10;
}

int kissat_assumption_lucky (kissat *solver, unsigned mode) {
assert (GET_OPTION (incremental));
assert (mode >= 1 && mode <= 4);
assert (!solver->probing);
assert (kissat_propagated (solver));
const unsigned base =
mode == 4 ? solver->level
: (unsigned) SIZE_STACK (solver->assumption_decisions);
assert (base <= solver->level);
if (mode != 4)
kissat_backtrack_in_consistent_state (solver, base);
INC (incremental_lucky_attempts);
if (mode == 3)
return 0;
const double deadline = kissat_wall_clock_time () +
0.001 * GET_OPTION (incrementalluckymilliseconds);
START (lucky);
// Assumptions stay on their decision levels. A failed greedy branch must
// not learn the root units that the ordinary assumption-free lucky walk
// can learn, so unsuccessful walks only restore this prefix.
solver->probing = true;
int result = 0;
const unsigned rounds = mode == 2 || mode == 4 ? 4 : 1;
for (unsigned round = 0; round < rounds && !result; round++) {
if (TERMINATED (lucky_terminated_1) ||
kissat_wall_clock_time () >= deadline)
break;
result = assumption_walk (solver, base, round >= 2, (round & 1) != 0,
deadline);
if (!result)
kissat_backtrack_without_updating_phases (solver, base);
}
solver->probing = false;
STOP (lucky);
if (result) {
assert (kissat_propagated (solver));
assert (!solver->unassigned);
INC (incremental_lucky_successes);
if (GET_OPTION (incrementalluckyphasecache))
for (all_variables (idx)) {
if (!(idx & 1023) && TERMINATED (lucky_terminated_1))
break;
if (ACTIVE (idx))
SAVED (idx) = VALUE (LIT (idx));
}
} else if (kissat_wall_clock_time () >= deadline)
INC (incremental_lucky_timeouts);
return result;
}

static bool no_all_negative_clauses (struct kissat *solver) {
clause *last_irredundant = kissat_last_irredundant_clause (solver);
for (all_clauses (c)) {
if (TERMINATED (lucky_terminated_1))
return false;
if (last_irredundant && last_irredundant < c)
break;
if (c->redundant)
Expand All @@ -26,6 +115,8 @@ static bool no_all_negative_clauses (struct kissat *solver) {
}
assert (solver->watching);
for (all_variables (idx)) {
if (TERMINATED (lucky_terminated_1))
return false;
if (!ACTIVE (idx))
continue;
const unsigned lit = LIT (idx);
Expand All @@ -47,6 +138,8 @@ static bool no_all_negative_clauses (struct kissat *solver) {
static bool no_all_positive_clauses (struct kissat *solver) {
clause *last_irredundant = kissat_last_irredundant_clause (solver);
for (all_clauses (c)) {
if (TERMINATED (lucky_terminated_1))
return false;
if (last_irredundant && last_irredundant < c)
break;
if (c->redundant)
Expand All @@ -62,6 +155,8 @@ static bool no_all_positive_clauses (struct kissat *solver) {
}
assert (solver->watching);
for (all_variables (idx)) {
if (TERMINATED (lucky_terminated_1))
return false;
if (!ACTIVE (idx))
continue;
const unsigned lit = LIT (idx);
Expand All @@ -85,6 +180,10 @@ static int forward_false_satisfiable (struct kissat *solver) {
unsigned conflicts = 0;
#endif
for (all_stack (import, import, solver->import)) {
if (TERMINATED (lucky_terminated_1)) {
kissat_backtrack_without_updating_phases (solver, 0);
return 0;
}
if (!import.imported)
continue;
if (import.eliminated)
Expand Down Expand Up @@ -140,6 +239,10 @@ static int forward_true_satisfiable (struct kissat *solver) {
unsigned conflicts = 0;
#endif
for (all_stack (import, import, solver->import)) {
if (TERMINATED (lucky_terminated_1)) {
kissat_backtrack_without_updating_phases (solver, 0);
return 0;
}
if (!import.imported)
continue;
if (import.eliminated)
Expand Down Expand Up @@ -197,6 +300,10 @@ static int backward_false_satisfiable (struct kissat *solver) {
import *end = END_STACK (solver->import);
import *p = end;
while (p != begin) {
if (TERMINATED (lucky_terminated_1)) {
kissat_backtrack_without_updating_phases (solver, 0);
return 0;
}
const import import = *--p;
if (!import.imported)
continue;
Expand Down Expand Up @@ -255,6 +362,10 @@ static int backward_true_satisfiable (struct kissat *solver) {
import *end = END_STACK (solver->import);
import *p = end;
while (p != begin) {
if (TERMINATED (lucky_terminated_1)) {
kissat_backtrack_without_updating_phases (solver, 0);
return 0;
}
const import import = *--p;
if (!import.imported)
continue;
Expand Down Expand Up @@ -319,9 +430,12 @@ int kissat_lucky (struct kissat *solver) {
assert (kissat_propagated (solver));

int res = 0;
const unsigned active_before = solver->active;

if (no_all_negative_clauses (solver)) {
for (all_variables (idx)) {
if (!res && TERMINATED (lucky_terminated_1))
goto DONE;
if (!ACTIVE (idx))
continue;
const unsigned lit = LIT (idx);
Expand All @@ -340,8 +454,13 @@ int kissat_lucky (struct kissat *solver) {
res = 10;
}

if (!res && TERMINATED (lucky_terminated_1))
goto DONE;

if (!res && no_all_positive_clauses (solver)) {
for (all_variables (idx)) {
if (!res && TERMINATED (lucky_terminated_1))
goto DONE;
if (!ACTIVE (idx))
continue;
const unsigned lit = LIT (idx);
Expand All @@ -361,20 +480,34 @@ int kissat_lucky (struct kissat *solver) {
res = 10;
}

const unsigned active_before = solver->active;
if (!res && TERMINATED (lucky_terminated_1))
goto DONE;

if (!res)
res = forward_false_satisfiable (solver);

if (!res && TERMINATED (lucky_terminated_1))
goto DONE;

if (!res)
res = forward_true_satisfiable (solver);

if (!res && TERMINATED (lucky_terminated_1))
goto DONE;

if (!res)
res = backward_false_satisfiable (solver);

if (!res && TERMINATED (lucky_terminated_1))
goto DONE;

if (!res)
res = backward_true_satisfiable (solver);

DONE:
if (!res && solver->level)
kissat_backtrack_without_updating_phases (solver, 0);

const unsigned active_after = solver->active;
const unsigned units = active_before - active_after;

Expand Down
1 change: 1 addition & 0 deletions src/lucky.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@

struct kissat;
int kissat_lucky (struct kissat *);
int kissat_assumption_lucky (struct kissat *, unsigned mode);

#endif
9 changes: 9 additions & 0 deletions src/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@
OPTION (forwardeffort, 100, 0, 1e6, "effort in per mille") \
OPTION (ifthenelse, 1, 0, 1, "extract and eliminate if-then-else gates") \
OPTION (incremental, 0, 0, 1, "enable incremental solving") \
OPTION (incrementallucky, 0, 0, 4, "bounded greedy walk (3=restart,4=current)") \
OPTION (incrementalluckydecisions, 10000, 0, INT_MAX, "greedy decision trigger") \
OPTION (incrementalluckygrowth, 4, 1, 16, "greedy retry clause growth factor") \
OPTION (incrementalluckyinit, 100, 0, INT_MAX, "greedy conflict trigger") \
OPTION (incrementalluckymilliseconds, 250, 1, 10000, "greedy wall time budget") \
OPTION (incrementalluckyminclauses, 100000, 0, INT_MAX, "greedy formula threshold") \
OPTION (incrementalluckyminunassigned, 0, 0, INT_MAX, "greedy unassigned threshold") \
OPTION (incrementalluckynewcontext, 0, 0, 1, "retry greedy after changed assumptions") \
OPTION (incrementalluckyphasecache, 0, 0, 1, "save phases after greedy SAT") \
OPTION (incrementalreuse, 0, 0, 2, "reuse assumption prefix (1) or entire trail (2)") \
OPTION (jumpreasons, 1, 0, 1, "jump binary reasons") \
LOGOPT (log, 0, 0, 5, "logging level (1=on,2=more,3=check,4/5=mem)") \
Expand Down
27 changes: 26 additions & 1 deletion src/search.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,22 @@ int kissat_search (kissat *solver) {
kissat_classify (solver);
if (!res && searching (solver)) {
start_search (solver);
const unsigned greedy_mode = GET_OPTION (incrementallucky);
const uint64_t original_clauses = GET (clauses_original);
const uint64_t last_greedy = solver->incremental_lucky_last_clauses;
bool greedy_pending =
greedy_mode && GET_OPTION (incremental) &&
!solver->limited.conflicts && !solver->limited.decisions &&
original_clauses >=
(unsigned) GET_OPTION (incrementalluckyminclauses) &&
(!solver->incremental_lucky_failed ||
(original_clauses >= last_greedy + 100000 &&
original_clauses / GET_OPTION (incrementalluckygrowth) >=
last_greedy));
const uint64_t greedy_conflicts =
CONFLICTS + GET_OPTION (incrementalluckyinit);
const uint64_t greedy_decisions =
DECISIONS + GET_OPTION (incrementalluckydecisions);
while (!res) {
clause *conflict = kissat_search_propagate (solver);
if (conflict)
Expand All @@ -211,7 +227,16 @@ int kissat_search (kissat *solver) {
res = 10;
else if (TERMINATED (search_terminated_1))
break;
else if (kissat_reducing (solver))
else if (greedy_pending &&
solver->unassigned >=
(unsigned) GET_OPTION (incrementalluckyminunassigned) &&
(CONFLICTS >= greedy_conflicts ||
DECISIONS >= greedy_decisions)) {
greedy_pending = false;
solver->incremental_lucky_last_clauses = original_clauses;
res = kissat_assumption_lucky (solver, greedy_mode);
solver->incremental_lucky_failed = !res;
} else if (kissat_reducing (solver))
res = kissat_reduce (solver);
else if (kissat_switching_search_mode (solver))
kissat_switch_search_mode (solver);
Expand Down
4 changes: 4 additions & 0 deletions src/statistics.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@
METRIC (initial_decisions, 1, PCNT_DECISIONS, "%", "decisions") \
COUNTER (incremental_restorations, 0, NO_SECONDARY, 0, 0) \
COUNTER (incremental_restored_literals, 0, NO_SECONDARY, 0, 0) \
COUNTER (incremental_lucky_attempts, 0, NO_SECONDARY, 0, 0) \
COUNTER (incremental_lucky_conflicts, 0, NO_SECONDARY, 0, 0) \
COUNTER (incremental_lucky_successes, 0, NO_SECONDARY, 0, 0) \
COUNTER (incremental_lucky_timeouts, 0, NO_SECONDARY, 0, 0) \
COUNTER (incremental_reused_levels, 0, NO_SECONDARY, 0, 0) \
COUNTER (incremental_reused_solves, 0, NO_SECONDARY, 0, 0) \
COUNTER (iterations, 1, PCNT_VARIABLES, "%", "variables") \
Expand Down
1 change: 1 addition & 0 deletions src/terminate.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,6 @@ static inline bool kissat_terminated (kissat *solver, int bit,
#define vivify_terminated_5 41
#define walk_terminated_1 42
#define warmup_terminated_1 43
#define lucky_terminated_1 44

#endif
Loading
Loading