32# include <gtest/gtest.h>
47 const Array<std::pair<long, long>> & clauses)
49 auto lit_val = [&](
long v) ->
bool {
50 return v > 0 ? assignment(
static_cast<size_t>(v - 1))
51 : not assignment(
static_cast<size_t>(-v - 1));
54 for (
size_t i = 0; i < clauses.size(); ++i)
56 auto [a, b] = clauses(i);
57 if (not lit_val(a)
and not lit_val(b))
71 auto [ok, assignment] = sat.
solve();
73 EXPECT_EQ(assignment.size(), 3U);
82 auto [ok, assignment] = sat.
solve();
86 EXPECT_TRUE(assignment(0) or assignment(1));
89TEST(TwoSat, UnitClauseTrue)
95 auto [ok, assignment] = sat.
solve();
97 EXPECT_TRUE(assignment(0));
106 auto [ok, assignment] = sat.
solve();
108 EXPECT_FALSE(assignment(0));
118 auto [ok, assignment] = sat.
solve();
129 auto [ok, assignment] = sat.
solve();
131 EXPECT_TRUE(assignment(0));
132 EXPECT_TRUE(assignment(1));
144 auto [ok, assignment] = sat.
solve();
146 for (
size_t i = 0; i < 4; ++i)
147 EXPECT_TRUE(assignment(i)) <<
"variable " << i;
150TEST(TwoSat, ImplicationChainContradiction)
169 auto [ok, assignment] = sat.
solve();
171 EXPECT_TRUE(assignment(0));
172 EXPECT_TRUE(assignment(1));
181 auto [ok, assignment] = sat.
solve();
183 EXPECT_NE(assignment(0), assignment(1));
211 auto [ok, assignment] = sat.
solve();
237 auto [ok, assignment] = sat.
solve();
241 for (
size_t i = 0; i < 3; ++i)
247TEST(TwoSat, AtMostOneWithForced)
256 auto [ok, assignment] = sat.
solve();
258 EXPECT_TRUE(assignment(0));
259 EXPECT_FALSE(assignment(1));
260 EXPECT_FALSE(assignment(2));
275TEST(TwoSat, AtMostOneEdgeCases)
293 EXPECT_THROW(sat.
add_at_most_one({sat.pos_lit(5)}), std::domain_error);
300TEST(TwoSat, GraphColoring2Colors)
313TEST(TwoSat, GraphColoring2ColorsPath)
321 auto [ok, assignment] = sat.
solve();
325 EXPECT_NE(assignment(0), assignment(1));
326 EXPECT_NE(assignment(1), assignment(2));
327 EXPECT_NE(assignment(2), assignment(3));
336 auto [ok, assignment] = sat.
solve();
338 EXPECT_TRUE(assignment(0));
368 constexpr size_t N = 8;
369 constexpr size_t NUM_CLAUSES = 20;
370 constexpr size_t NUM_TRIALS = 50;
372 std::mt19937
rng(2024);
373 std::uniform_int_distribution<size_t> var_dist(0,
N - 1);
374 std::uniform_int_distribution<int> sign_dist(0, 1);
376 for (
size_t trial = 0; trial < NUM_TRIALS; ++trial)
381 for (
size_t c = 0; c < NUM_CLAUSES; ++c)
383 long a =
static_cast<long>(var_dist(
rng)) + 1;
384 long b =
static_cast<long>(var_dist(
rng)) + 1;
385 if (sign_dist(
rng)) a = -a;
386 if (sign_dist(
rng)) b = -b;
388 clauses.
append(std::pair<long, long>(a, b));
391 auto [ok, assignment] = sat.
solve();
394 bool brute_sat =
false;
395 for (
unsigned mask = 0; mask < (1U <<
N); ++mask)
398 for (
size_t i = 0; i <
N; ++i)
399 bf(i) = ((mask >> i) & 1) != 0;
408 EXPECT_EQ(ok, brute_sat)
409 <<
"Trial " << trial <<
": solver says "
410 << (ok ?
"SAT" :
"UNSAT") <<
" but brute-force says "
411 << (brute_sat ?
"SAT" :
"UNSAT");
415 <<
"Trial " << trial <<
": assignment does not satisfy formula";
435 auto [ok, assignment] = sat.
solve();
451 auto [ok, assignment] = sat.
solve();
464 constexpr size_t N = 500;
465 constexpr size_t M = 2000;
467 std::mt19937
rng(42);
468 std::uniform_int_distribution<size_t> var_dist(0,
N - 1);
469 std::uniform_int_distribution<int> sign_dist(0, 1);
473 for (
size_t i = 0; i <
N; ++i)
474 target(i) = sign_dist(
rng) != 0;
479 for (
size_t c = 0; c < M; ++c)
481 size_t v1 = var_dist(
rng);
482 size_t v2 = var_dist(
rng);
488 clauses.
append(std::pair<size_t, size_t>(
l1,
l2));
491 auto [ok, assignment] = sat.
solve();
494 for (
size_t c = 0; c < clauses.
size(); ++c)
496 auto [
l1,
l2] = clauses[c];
501 EXPECT_TRUE(val1 or val2);
505TEST(TwoSat, LargerUnsatisfiable)
510 constexpr size_t N = 200;
514 for (
size_t i = 0; i + 1 <
N; ++i)
2-SAT solver based on Tarjan's SCC algorithm.
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
static Array create(const size_t n)
Create an array with n logical elements.
T & append(const T &data)
Append a copy of data
Generic directed graph (digraph) wrapper template.
Arc for graphs implemented with simple adjacency lists.
Number Theoretic Transform over Z / MOD Z.
2-SAT solver using Tarjan's SCC algorithm.
void add_clause_signed(const long a, const long b)
Add a clause using signed 1-based variables.
static constexpr size_t pos_lit(const size_t var) noexcept
Get the positive literal for a variable.
std::pair< bool, Array< bool > > solve()
Solve the 2-SAT instance.
void set_true(const size_t a)
Force literal a to be true.
void add_implication(const size_t a, const size_t b)
Add an implication a -> b.
static constexpr bool is_pos_lit(const size_t lit) noexcept
Check if a literal is positive.
void set_false(const size_t a)
Force literal a to be false.
static constexpr size_t neg_lit(const size_t var) noexcept
Get the negative literal for a variable.
void add_at_most_one(const Array< size_t > &lits)
Add at-most-one constraint over a set of literals.
static constexpr size_t lit_var(const size_t lit) noexcept
Get the variable index of a literal.
void add_equiv(const size_t a, const size_t b)
Add equivalence constraint: a <-> b.
void add_xor(const size_t a, const size_t b)
Add XOR constraint: exactly_one(a, b).
bool is_satisfiable()
Check if the formula is satisfiable.
void add_clause(size_t a, size_t b)
Add a clause (a OR b).
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Array-based graph implementation.
Simple graph implementation with adjacency lists.
static bool verify_assignment(const Array< bool > &assignment, const Array< std::pair< long, long > > &clauses)