GithubHelp home page GithubHelp logo

cxxsat's Introduction

cxxsat

cxxsat is an enhanced C++ interface for SAT solvers that makes prototyping easier, encodes commonly required functionality natively and reduces formula bloat, leading to better SAT solver performance.

cxxsat comes pre-packaged with Cadical and uses it through the ipasir C interface used in all competitive incremental solvers. Replacing Cadical with another solver is therefore as easy as changing the CMakeLists.txt file to specify the new solver as an external project in some git sub-module.

Example Code

Here is an example of using only the basic features of cxxsat, that shows the workflow of creating a solver, constraints, solving the problem and getting the satisfiable solution:

#include "Solver.h"
using Solver = cxxsat::Solver;
using var_t = cxxsat::var_t;
using state_t = cxxsat::Solver::state_t;
int main()
{
  Solver solver;
  var_t a = solver.new_var();
  var_t b = solver.new_var();
  var_t c = solver.new_var();
  solver.add_clause(-a, b, -c);
  solver.add_clause(a, -b);
  state_t state = solver.check();
  if (state == state_t::STATE_SAT)
  {
    std::cout << "a = " << solver.value(a);
    std::cout << "b = " << solver.value(b);
    std::cout << "c = " << solver.value(c);
  }
  return 0;
}

Here is abother example that focuses on the usability aspect, because usually, you do not want to implement all the logic operator encoding, simplification and hashing yourself:

#include "Solver.h"
using var_t = cxxsat::var_t;
using state_t = cxxsat::Solver::state_t;

int main()
{
  cxxsat::solver = new cxxsat::Solver();
  var_t a = solver->new_var();
  var_t b = solver->new_var();
  var_t c = solver->new_var();
  var_t d = a & b;
  var_t e = -a | -b;
  var_t f = cxxsat::mux(c, d, var_t::ZERO);

  std::vector<var_t> free_vars = {a, b, c};
  var_t le_2 = cxxsat::solver->make_at_most(free_vars, 2);
  cxxsat::solver->add_clause(le_2);
  cxxsat::solver->assume(a);

  state_t state = cxxsat::solver->check();
  if (state == state_t::STATE_SAT)
  {
    std::cout << "a (" << as_int(a) << ") = " << cxxsat::solver->value(a);
    std::cout << "b (" << as_int(b) << ") = " << cxxsat::solver->value(b);
    std::cout << "c (" << as_int(c) << ") = " << cxxsat::solver->value(c);
    std::cout << "d (" << as_int(d) << ") = " << cxxsat::solver->value(d);
    std::cout << "e (" << as_int(e) << ") = " << cxxsat::solver->value(e);
    std::cout << "f (" << as_int(f) << ") = " << cxxsat::solver->value(f);
  }
  delete cxxsat::solver;
  return 0;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.