sat

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2022 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package solver implements a general-purpose solver for boolean constraint satisfiability problems.

Index

Constants

This section is empty.

Variables

View Source
var ErrIncomplete = errors.New("cancelled before a solution could be found")

Functions

This section is empty.

Types

type AppliedConstraint

type AppliedConstraint struct {
	Variable   Variable
	Constraint Constraint
}

AppliedConstraint values compose a single Constraint with the Variable it applies to.

func (AppliedConstraint) String

func (a AppliedConstraint) String() string

String implements fmt.Stringer and returns a human-readable message representing the receiver.

type Constraint

type Constraint interface {
	String(subject Identifier) string
	Apply(c *logic.C, lm *LitMapping, subject Identifier) z.Lit
	Order() []Identifier
	Anchor() bool
}

Constraint implementations limit the circumstances under which a particular Variable can appear in a solution.

func AtMost

func AtMost(n int, ids ...Identifier) Constraint

AtMost returns a Constraint that forbids solutions that contain more than n of the Variables identified by the given Identifiers.

func Conflict

func Conflict(id Identifier) Constraint

Conflict returns a Constraint that will permit solutions containing either the constrained Variable, the Variable identified by the given Identifier, or neither, but not both.

func Dependency

func Dependency(ids ...Identifier) Constraint

Dependency returns a Constraint that will only permit solutions containing a given Variable on the condition that at least one of the Variables identified by the given Identifiers also appears in the solution. Identifiers appearing earlier in the argument list have higher preference than those appearing later.

func Mandatory

func Mandatory() Constraint

Mandatory returns a Constraint that will permit only solutions that contain a particular Variable.

func Prohibited

func Prohibited() Constraint

Prohibited returns a Constraint that will reject any solution that contains a particular Variable. Callers may also decide to omit an Variable from input to Solve rather than Apply such a Constraint.

type DefaultTracer

type DefaultTracer struct{}

func (DefaultTracer) Trace

func (DefaultTracer) Trace(_ SearchPosition)

type DuplicateIdentifier

type DuplicateIdentifier Identifier

func (DuplicateIdentifier) Error

func (e DuplicateIdentifier) Error() string

type Identifier

type Identifier string

Identifier values uniquely identify particular Variables within the input to a single call to Solve.

func IdentifierFromString

func IdentifierFromString(s string) Identifier

IdentifierFromString returns an Identifier based on a provided string.

func (Identifier) String

func (id Identifier) String() string

type LitMapping

type LitMapping struct {
	// contains filtered or unexported fields
}

LitMapping performs translation between the input and output types of Solve (Constraints, Variables, etc.) and the variables that appear in the SAT formula.

func (*LitMapping) AddConstraints

func (d *LitMapping) AddConstraints(g inter.S)

AddConstraints adds the current constraints encoded in the embedded circuit to the solver g

func (*LitMapping) AnchorIdentifiers

func (d *LitMapping) AnchorIdentifiers() []Identifier

AnchorIdentifiers returns a slice containing the Identifiers of every Variable with at least one "Anchor" constraint, in the Order they appear in the input.

func (*LitMapping) AssumeConstraints

func (d *LitMapping) AssumeConstraints(s inter.S)

func (*LitMapping) CardinalityConstrainer

func (d *LitMapping) CardinalityConstrainer(g inter.Adder, ms []z.Lit) *logic.CardSort

CardinalityConstrainer constructs a sorting network to provide cardinality constraints over the provided slice of literals. Any new clauses and variables are translated to CNF and taught to the given inter.Adder, so this function will panic if it is in a test context.

func (*LitMapping) Conflicts

func (d *LitMapping) Conflicts(g inter.Assumable) []AppliedConstraint

func (*LitMapping) ConstraintOf

func (d *LitMapping) ConstraintOf(m z.Lit) AppliedConstraint

ConstraintOf returns the constraint application corresponding to the provided literal, or a zeroConstraint if no such constraint exists.

func (*LitMapping) Error

func (d *LitMapping) Error() error

Error returns a single error value that is an aggregation of all errors encountered during a LitMapping's lifetime, or nil if there have been no errors. A non-nil return value likely indicates a problem with the solver or constraint implementations.

func (*LitMapping) LitOf

func (d *LitMapping) LitOf(id Identifier) z.Lit

LitOf returns the positive literal corresponding to the Variable with the given Identifier.

func (*LitMapping) Lits

func (d *LitMapping) Lits(dst []z.Lit) []z.Lit

func (*LitMapping) VariableOf

func (d *LitMapping) VariableOf(m z.Lit) Variable

VariableOf returns the Variable corresponding to the provided literal, or a zeroVariable if no such Variable exists.

func (*LitMapping) Variables

func (d *LitMapping) Variables(g inter.S) []Variable

type LoggingTracer

type LoggingTracer struct {
	Writer io.Writer
}

func (LoggingTracer) Trace

func (t LoggingTracer) Trace(p SearchPosition)

type NotSatisfiable

type NotSatisfiable []AppliedConstraint

NotSatisfiable is an error composed of a minimal set of applied constraints that is sufficient to make a solution impossible.

func (NotSatisfiable) Error

func (e NotSatisfiable) Error() string

type Option

type Option func(s *solver) error

func WithInput

func WithInput(input []Variable) Option

func WithTracer

func WithTracer(t Tracer) Option

type SearchPosition

type SearchPosition interface {
	Variables() []Variable
	Conflicts() []AppliedConstraint
}

type Solver

type Solver interface {
	Solve(context.Context) ([]Variable, error)
}

func NewSolver

func NewSolver(options ...Option) (Solver, error)

type Tracer

type Tracer interface {
	Trace(p SearchPosition)
}

type Variable

type Variable interface {
	// Identifier returns the Identifier that uniquely identifies
	// this Variable among all other Variables in a given
	// problem.
	Identifier() Identifier
	// Constraints returns the set of constraints that Apply to
	// this Variable.
	Constraints() []Constraint
}

Variable values are the basic unit of problems and solutions understood by this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳