satsolver

package module
v0.0.0-...-dd489e1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2022 License: MIT Imports: 8 Imported by: 0

README

DPLL SAT SOLVER

This is a simple SAT solver I implemented to explore the boolean satisfiability problem and understand why it's so difficult. I followed this blog fairly closely: https://homes.cs.washington.edu/~emina/blog/2017-06-23-a-primer-on-sat.html I ran into some issues with the bcp/unit propogation. I had gotten that for each unit literal you could remove any clause that contained it however. I missed that for each clause with the negative of the unit literal you could remove the negative. And if the clause no longer contained any literals you had a contradiction. This turned out to be a key part of the algorithm that I missed and so I was very confused about the speed of my solver for a long time.

Also included in this repo is a verifier and a simple 3SAT generator.

I used participle (a grammar engine) to parse CNF formulas.

Testing

make test
make bench

Building

make build

Running

Generate 3SAT instance

./generator 100 50 > mycnf.cnf

100 is the number of clauses, 50 is the number of variables to choose from

Solve the CNF

./solver mycnf.cnf
./solver samples/4x3.cnf
./solver samples/80x50.cnf

Instead, you can run the solver in a REPL mode by running it with no arguments

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CleanFormula

func CleanFormula(f *Formula)

func VarsMap

func VarsMap(f Formula) map[string]*Variable

VarsMap returns all variables in the formula by their name

func Verify

func Verify(f Formula, I Interpretation) (bool, error)

Verify returns true if the CNF formula is satisfied under the given interpretation

Types

type Clause

type Clause struct {
	L []Literal `@@ | "(" @@ ("v" @@ )* ")"`
}

func (Clause) Equal

func (a Clause) Equal(b Clause) bool

strict equal including order

func (Clause) String

func (c Clause) String() string

type Formula

type Formula struct {
	C []Clause `@@ ("^" @@)*`
	V map[string]*Variable
}

func CloneFormula

func CloneFormula(f Formula) Formula

func GenerateUniform3SAT

func GenerateUniform3SAT(m int, n int, seed *int64) (Formula, error)

GenerateUniform3SAT generates a random 3SAT problem m is the number of clauses n is the number of variables

func MustParse

func MustParse(s string) Formula

MustParse panics if Parse returns an error

func Parse

func Parse(r io.Reader) (Formula, error)

Parse parses a CNF formula and returns an AST representing it example: a ^ (~b v c) ^ ~d

func (Formula) String

func (f Formula) String() string

type Interpretation

type Interpretation map[string]bool

func FromVariables

func FromVariables(vars []*Variable) Interpretation

func FromVariablesMap

func FromVariablesMap(vars map[string]*Variable) Interpretation

func Solve

func Solve(f Formula, heuristic func(Formula) *Variable) (Interpretation, error)

Solve returns an Interpretation under which the formula is satisfiable or an error if the formula is unsatisfiable

func (Interpretation) String

func (i Interpretation) String() string

type Literal

type Literal struct {
	Negated bool      `@"~"?`
	V       *Variable `@@`
}

func (Literal) String

func (l Literal) String() string

type Variable

type Variable struct {
	Name     string `@Ident`
	Value    bool
	Assigned bool
}

func FirstUnassigned

func FirstUnassigned(f Formula) *Variable

func JeroslowWang

func JeroslowWang(f Formula) *Variable

func MaximumOccurrences

func MaximumOccurrences(f Formula) *Variable

func RandomUnassigned

func RandomUnassigned(f Formula) *Variable

func Vars

func Vars(f Formula) []*Variable

Vars returns all variables in the formula

func (*Variable) Assign

func (v *Variable) Assign(value bool)

func (Variable) String

func (v Variable) String() string

Directories

Path Synopsis
cmd
generator command
solver command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL