package solver
import "github.com/irfansharif/solver"
Package solver is a Go wrapper library for the CP-SAT Solver included as part Google's Operations Research tools.
Index ¶
- type Constraint
- func NewAllDifferentConstraint(vars ...IntVar) Constraint
- func NewAllSameConstraint(vars ...IntVar) Constraint
- func NewAllowedAssignmentsConstraint(vars []IntVar, assignments [][]int64) Constraint
- func NewAllowedLiteralAssignmentsConstraint(literals []Literal, assignments [][]bool) Constraint
- func NewAtLeastKConstraint(k int, literals ...Literal) Constraint
- func NewAtMostKConstraint(k int, literals ...Literal) Constraint
- func NewBooleanAndConstraint(literals ...Literal) Constraint
- func NewBooleanOrConstraint(literals ...Literal) Constraint
- func NewBooleanXorConstraint(literals ...Literal) Constraint
- func NewCumulativeConstraint(capacity IntVar, intervals []Interval, demands []IntVar) Constraint
- func NewDivisionConstraint(target, numerator, denominator IntVar) Constraint
- func NewElementConstraint(target, index IntVar, vars ...IntVar) Constraint
- func NewExactlyKConstraint(k int, literals ...Literal) Constraint
- func NewForbiddenAssignmentsConstraint(vars []IntVar, assignments [][]int64) Constraint
- func NewForbiddenLiteralAssignmentsConstraint(literals []Literal, assignments [][]bool) Constraint
- func NewImplicationConstraint(a, b Literal) Constraint
- func NewLinearConstraint(e LinearExpr, d Domain) Constraint
- func NewLinearMaximumConstraint(target LinearExpr, exprs ...LinearExpr) Constraint
- func NewLinearMinimumConstraint(target LinearExpr, exprs ...LinearExpr) Constraint
- func NewMaximumConstraint(target IntVar, vars ...IntVar) Constraint
- func NewMinimumConstraint(target IntVar, vars ...IntVar) Constraint
- func NewModuloConstraint(target, dividend, divisor IntVar) Constraint
- func NewNonOverlapping2DConstraint( xintervals []Interval, yintervals []Interval, boxesWithNoAreaCanOverlap bool, ) Constraint
- func NewNonOverlappingConstraint(intervals ...Interval) Constraint
- func NewProductConstraint(target IntVar, multiplicands ...IntVar) Constraint
- type Domain
- type IntVar
- type Interval
- type LinearExpr
- func NewLinearExpr(vars []IntVar, coeffs []int64, offset int64) LinearExpr
- func Sum(vars ...IntVar) LinearExpr
- type Literal
- type Model
- func NewModel(name string) *Model
- func (m *Model) AddConstraints(cs ...Constraint)
- func (m *Model) Maximize(e LinearExpr)
- func (m *Model) Minimize(e LinearExpr)
- func (m *Model) NewConstant(c int64, name string) IntVar
- func (m *Model) NewIntVar(lb int64, ub int64, name string) IntVar
- func (m *Model) NewIntVarFromDomain(d Domain, name string) IntVar
- func (m *Model) NewInterval(start, end, size IntVar, name string) Interval
- func (m *Model) NewLiteral(name string) Literal
- func (m *Model) Solve(os ...Option) Result
- func (m *Model) String() string
- func (m *Model) Validate() (ok bool, _ error)
- type Option
- func WithEnumeration(f func(Result)) Option
- func WithLogger(w io.Writer, prefix string) Option
- func WithParallelism(parallelism int) Option
- func WithTimeout(d time.Duration) Option
- type Result
Types ¶
type Constraint ¶
type Constraint interface { // OnlyEnforceIf enforces the constraint iff all literals listed are true. If // not explicitly called, of if the list is empty, then the constraint will // always be enforced. // // NB: Only a few constraints support enforcement: // - NewBooleanOrConstraint // - NewBooleanAndConstraint // - NewLinearConstraint // // Intervals support enforcement too, but only with a single literal. OnlyEnforceIf(literals ...Literal) Constraint // Stringer provides a printable format representation for the constraint. fmt.Stringer // WithName sets a name for the constraint; used for debugging/logging // purposes. // // TODO(irfansharif): We don't use it in our pretty printing, yet. That // aside it would be nice to have a unified way to name things. For intvars, // literal, intervals -- it's provided as part of initializer. We use // separate things for models and constraints. WithName(name string) Constraint // contains filtered or unexported methods }
Constraint is what a model attempts to satisfy when deciding on a solution.
func NewAllDifferentConstraint ¶
func NewAllDifferentConstraint(vars ...IntVar) Constraint
NewAllDifferentConstraint forces all variables to take different values.
func NewAllSameConstraint ¶
func NewAllSameConstraint(vars ...IntVar) Constraint
NewAllSameConstraint forces all variables to take the same values.
func NewAllowedAssignmentsConstraint ¶
func NewAllowedAssignmentsConstraint(vars []IntVar, assignments [][]int64) Constraint
NewAllowedAssignmentsConstraint ensures that the values of the n-tuple formed by the given variables is one of the listed n-tuple assignments.
func NewAllowedLiteralAssignmentsConstraint ¶
func NewAllowedLiteralAssignmentsConstraint(literals []Literal, assignments [][]bool) Constraint
NewAllowedLiteralAssignmentsConstraint ensures that the values of the n-tuple formed by the given literals is one of the listed n-tuple assignments.
func NewAtLeastKConstraint ¶
func NewAtLeastKConstraint(k int, literals ...Literal) Constraint
NewAtLeastKConstraint ensures that at least k literals are true.
func NewAtMostKConstraint ¶
func NewAtMostKConstraint(k int, literals ...Literal) Constraint
NewAtMostKConstraint ensures that no more than k literals are true.
func NewBooleanAndConstraint ¶
func NewBooleanAndConstraint(literals ...Literal) Constraint
NewBooleanAndConstraint ensures that all literals are true.
func NewBooleanOrConstraint ¶
func NewBooleanOrConstraint(literals ...Literal) Constraint
NewBooleanOrConstraint ensures that at least one literal is true. It can be thought of as a special case of NewAtLeastKConstraint, but one that uses a more efficient internal encoding.
func NewBooleanXorConstraint ¶
func NewBooleanXorConstraint(literals ...Literal) Constraint
NewBooleanXorConstraint ensures that an odd number of the literals are true.
func NewCumulativeConstraint ¶
func NewCumulativeConstraint(capacity IntVar, intervals []Interval, demands []IntVar) Constraint
NewCumulativeConstraint ensures that the sum of the demands of the intervals (intervals[i]'s demand is specified in demands[i]) at each interval point cannot exceed a max capacity. The intervals are interpreted as [start, end). Intervals of size zero are ignored.
func NewDivisionConstraint ¶
func NewDivisionConstraint(target, numerator, denominator IntVar) Constraint
NewDivisionConstraint ensures that the target is to equal to numerator/denominator. It also ensures that the denominator is non-zero.
func NewElementConstraint ¶
func NewElementConstraint(target, index IntVar, vars ...IntVar) Constraint
NewElementConstraint ensures that the target is equal to vars[index]. Implicitly index takes on one of the values in [0, len(vars)).
func NewExactlyKConstraint ¶
func NewExactlyKConstraint(k int, literals ...Literal) Constraint
NewExactlyKConstraint ensures that exactly k literals are true.
func NewForbiddenAssignmentsConstraint ¶
func NewForbiddenAssignmentsConstraint(vars []IntVar, assignments [][]int64) Constraint
NewForbiddenAssignmentsConstraint ensures that the values of the n-tuple formed by the given variables is not one of the listed n-tuple assignments.
func NewForbiddenLiteralAssignmentsConstraint ¶
func NewForbiddenLiteralAssignmentsConstraint(literals []Literal, assignments [][]bool) Constraint
NewForbiddenLiteralAssignmentsConstraint ensures that the values of the n-tuple formed by the given literals is not one of the listed n-tuple assignments.
func NewImplicationConstraint ¶
func NewImplicationConstraint(a, b Literal) Constraint
NewImplicationConstraint ensures that the first literal implies the second.
func NewLinearConstraint ¶
func NewLinearConstraint(e LinearExpr, d Domain) Constraint
NewLinearConstraint ensures that the linear expression lies in the given domain. It can be used to express linear equalities of the form:
0 <= x + 2y <= 10
func NewLinearMaximumConstraint ¶
func NewLinearMaximumConstraint(target LinearExpr, exprs ...LinearExpr) Constraint
NewLinearMaximumConstraint ensures that the target is equal to the maximum of all linear expressions.
func NewLinearMinimumConstraint ¶
func NewLinearMinimumConstraint(target LinearExpr, exprs ...LinearExpr) Constraint
NewLinearMinimumConstraint ensures that the target is equal to the minimum of all linear expressions.
func NewMaximumConstraint ¶
func NewMaximumConstraint(target IntVar, vars ...IntVar) Constraint
NewMaximumConstraint ensures that the target is equal to the maximum of all variables.
func NewMinimumConstraint ¶
func NewMinimumConstraint(target IntVar, vars ...IntVar) Constraint
NewMinimumConstraint ensures that the target is equal to the minimum of all variables.
func NewModuloConstraint ¶
func NewModuloConstraint(target, dividend, divisor IntVar) Constraint
NewModuloConstraint ensures that the target to equal to dividend%divisor. The domain of the divisor must be strictly positive.
func NewNonOverlapping2DConstraint ¶
func NewNonOverlapping2DConstraint( xintervals []Interval, yintervals []Interval, boxesWithNoAreaCanOverlap bool, ) Constraint
NewNonOverlapping2DConstraint ensures that the boxes defined by the following don't overlap:
[xintervals[i].start, xintervals[i].end) [yintervals[i].start, yintervals[i].end)
Intervals/boxes of size zero are considered for overlap if the last argument is true.
func NewNonOverlappingConstraint ¶
func NewNonOverlappingConstraint(intervals ...Interval) Constraint
NewNonOverlappingConstraint ensures that all the intervals are disjoint. More formally, there must exist a sequence such that for every pair of consecutive intervals, we have intervals[i].end <= intervals[i+1].start. Intervals of size zero matter for this constraint. This is also known as a disjunctive constraint in scheduling.
func NewProductConstraint ¶
func NewProductConstraint(target IntVar, multiplicands ...IntVar) Constraint
NewProductConstraint ensures that the target to equal to the product of all multiplicands. An empty multiplicands list forces the target to be equal to one.
type Domain ¶
Domain represents n disjoint intervals, each of the form [min, max]:
[min_0, max_0, ..., min_{n-1}, max_{n-1}].
The following constraints hold:
- For all i < n : min_i <= max_i
- For all i < n-1 : max_i + 1 < min_{i+1}.
The most common example being just [min, max]. If min == max, then this is a constant variable.
NB: We check at validation that a variable domain is small enough so that we don't run into integer overflow in our algorithms. Avoid having "unbounded" variables like [0, math.MaxInt64], opting instead for tighter domains.
func NewDomain ¶
NewDomain instantiates a new domain using the given intervals.
type IntVar ¶
type IntVar interface { // Stringer provides a printable format representation for the int var. fmt.Stringer // contains filtered or unexported methods }
IntVar is an integer variable. It's typically constructed using a domain and is used as part of a model's constraints/objectives. When solving a model, the variable's integer value is decided on.
func AsIntVars ¶
AsIntVars is a convenience function to convert a slice of Literals to IntVars.
type Interval ¶
type Interval interface { Constraint // Parameters returns the variables the interval is comprised of. Parameters() (start, end, size IntVar) // Stringer provides a printable format representation for the interval. fmt.Stringer // contains filtered or unexported methods }
Interval represents an interval parameterized by a start, end, and size. When added to a model, it automatically enforces the following properties:
start + size == end size >= 0
It can be used to define interval-based constraints. Constraints differ in how they interpret zero-sized intervals and whether the end is considered exclusive.
type LinearExpr ¶
type LinearExpr interface { // Parameters returns the variables, coefficients, and offset the linear // expression is comprised of. Parameters() (vars []IntVar, coeffs []int64, offset int64) fmt.Stringer // contains filtered or unexported methods }
LinearExpr represents a linear expression of the form:
5x - 7y + 21z - 42
In the expression above {x, y, z} are variables (IntVars) to be decided on by the model, {5, -7, 21} are coefficients for said variables, and -42 is the offset.
func NewLinearExpr ¶
func NewLinearExpr(vars []IntVar, coeffs []int64, offset int64) LinearExpr
NewLinearExpr instantiates a new linear expression, representing:
sum(coefficients[i] * vars[i]) + offset
func Sum ¶
func Sum(vars ...IntVar) LinearExpr
Sum instantiates a new linear expression representing the sum of the given variables. It's a shorthand for NewLinearExpr with no offset and coefficients equal to one.
type Literal ¶
type Literal interface { IntVar // Not returns the negated form of literal. It uses a more efficient encoding // than two literals with a model constraint xor-ing them together. Not() Literal // contains filtered or unexported methods }
Literal is a boolean variable. It's represented using an IntVar with a fixed domain [0, 1].
type Model ¶
type Model struct {
// contains filtered or unexported fields
}
Model is a constraint programming problem. It's not safe for concurrent use.
func NewModel ¶
NewModel instantiates a new model.
func (*Model) AddConstraints ¶
func (m *Model) AddConstraints(cs ...Constraint)
AddConstraints adds constraints to the model. When deciding on a solution, these constraints will need to be satisfied.
func (*Model) Maximize ¶
func (m *Model) Maximize(e LinearExpr)
Maximize sets a maximization objective for the model.
func (*Model) Minimize ¶
func (m *Model) Minimize(e LinearExpr)
Minimize sets a minimization objective for the model.
func (*Model) NewConstant ¶
NewConstant adds a new constant to the model.
func (*Model) NewIntVar ¶
NewIntVar adds a new integer variable to the model, one that's constrained to the given inclusive upper/lower bound.
func (*Model) NewIntVarFromDomain ¶
NewIntVarFromDomain adds a new integer variable to the model, one that's constrained to the given domain.
func (*Model) NewInterval ¶
NewInterval adds a new interval to the model, one that's defined using the given start, end and size.
func (*Model) NewLiteral ¶
NewLiteral adds a new literal to the model.
func (*Model) Solve ¶
Solve attempts to satisfy the model's constraints, if any, by deciding values for all the variables/literals that were instantiated into it. It returns the optimal result if an objective function is declared. If not, it returns the first found result that satisfies the model.
The solve process itself can be configured with various options.
func (*Model) String ¶
String provides a string representation of the model.
func (*Model) Validate ¶
Validate checks whether the model is valid. If not, a descriptive error message is returned.
TODO(irfansharif): This validation message refers to things using indexes, which is not really usable.
type Option ¶
type Option func(o *options, s internal.SolveWrapper)
func WithEnumeration ¶
WithEnumeration configures the solver to enumerate over all solutions without objective. This option is incompatible with a parallelism greater than 1.
func WithLogger ¶
WithLogger configures the solver to route its internal logging to the given io.Writer, using the given prefix.
func WithParallelism ¶
WithParallelism configures the solver to use the given number of parallel workers during search. If the number provided is <= 1, there will be no parallelism.
func WithTimeout ¶
WithTimeout configures the solver with a hard time limit.
type Result ¶
type Result struct {
// contains filtered or unexported fields
}
Result is what's returned after attempting to solve a model.
func (Result) BooleanValue ¶
BooleanValue returns the decided value of the given Literal. This is only valid to use if the result is optimal or feasible.
func (Result) Feasible ¶
Feasible is true if a feasible solution has been found, and if we're enumerating through all solutions (if asked). See comment for Optimal for more details.
func (Result) Infeasible ¶
Infeasible is true iff the problem has been proven infeasible.
func (Result) Invalid ¶
Invalid is true if the model being solved was invalid.
func (Result) ObjectiveValue ¶
ObjectiveValue is the result of evaluating a model's objective function if the solution found is optimal or feasible. If no solution is found, then for a minimization problem, this will be an upper-bound of the objective of any feasible solution. For a maximization problem, it will be the lower-bound.
func (Result) Optimal ¶
Optimal is true iff a feasible solution has been found.
More generally, this status represents a successful attempt at solving a model (if we've found a solution for a pure feasability problem, or if a gap limit has been specified and we've found solutions within the limit). In the case where we're iterating through all feasible solutions, this status will only be Feasible().
func (Result) String ¶
func (Result) Value ¶
Value returns the decided value of the given IntVar. This is only valid to use if the result is optimal or feasible.
Source Files ¶
constraint.go doc.go domain.go interval.go intvar.go linearexpr.go model.go options.go result.go
Directories ¶
Path | Synopsis |
---|---|
internal | Package internal is where the raw SWIG bindings to the OR-Tools CP-SAT solver live. |
- Version
- v0.0.0-20220713194315-9c33ad307075 (latest)
- Published
- Jul 13, 2022
- Platform
- linux/amd64
- Imports
- 10 packages
- Last checked
- 3 weeks ago –
Tools for package owners.