Skip to content

nano-byte/sat-solver

Repository files navigation

NanoByte SAT Solver

Build NuGet API documentation
NanoByte SAT Solver is a CDCL Boolean Satisfiability Solver for .NET.

Usage

Add a reference to the NanoByte.SatSolver NuGet package to your project.

You need to choose the underlying type to use for Literals in Boolean Formulas. This will often be int or string but you can also use any other type that implements the IEquatable<T> interface. Create an instance of SatProblem<T> and declare your variables:

var problem = new SatProblem<string>();

var a = problem.AddVariable("a");
var b = problem.AddVariable("b");
var c = problem.AddVariable("c");
var d = problem.AddVariable("d");

Add constraints as clauses (disjunctions of literals where at least one must be true). The ! operator negates a literal:

problem.AddClause(a, b);   // a OR b
problem.AddClause(!a, c);  // NOT a OR c
problem.AddClause(!c, d);  // NOT c OR d
problem.AddClause(a);      // a must be true (unit clause / unconditional fact)

You can also use | and & operator expressions with += to add multiple clauses at once in a compact form:

problem += (a | b) & (!a | c) & (!c | d) & a;

For common implication patterns there is a dedicated helper:

problem.Implies(a, c);  // a implies c  (equivalent to: NOT a OR c)
problem.Implies(c, d);  // c implies d

You can also add an at-most-one constraint to express that no two literals in a group may be true simultaneously:

problem.AtMostOne(a, b, c);

Call Solve() to find a satisfying assignment. It returns null when the problem is unsatisfiable:

Model<string>? model = problem.Solve();
if (model != null)
{
    // Satisfiable: inspect which variables were assigned true
    foreach (var value in model.SelectedValues)
        Console.WriteLine($"{value} = true");

    // Or query a specific variable
    bool? aValue = model["a"];
}

When the solver needs to choose which unassigned variable to branch on next, it picks one arbitrarily. You can supply a decider callback with domain-specific branching logic for better performance:

Model<string>? model = problem.Solve(decider: () =>
{
    // Return a preferred literal to branch on, or null to let the solver decide
    return somePreferredLiteral;
});

Building

The source code is in src/, config for building the API documentation is in doc/ and generated build artifacts are placed in artifacts/. The source code does not contain version numbers. Instead the version is determined during CI using GitVersion.

To build run .\build.ps1 or ./build.sh (.NET SDK is automatically downloaded if missing using 0install).

Contributing

We welcome contributions to this project such as bug reports, recommendations and pull requests.

This repository contains an EditorConfig file. Please make sure to use an editor that supports it to ensure consistent code style, file encoding, etc.. For full tooling support for all style and naming conventions consider using JetBrains' ReSharper or Rider products.

About

DPLL boolean satisfiability solver for .NET

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors