Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor solver and move to internal #82

Merged
merged 3 commits into from
Jan 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/dimacs/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/spf13/cobra"

"github.com/operator-framework/deppy/pkg/solver"
"github.com/operator-framework/deppy/pkg/deppy/solver"
)

func NewDimacsCommand() *cobra.Command {
Expand Down
30 changes: 15 additions & 15 deletions cmd/dimacs/dimacs_constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"context"
"strings"

"github.com/operator-framework/deppy/pkg/entitysource"
"github.com/operator-framework/deppy/pkg/sat"
"github.com/operator-framework/deppy/pkg/variablesource"
"github.com/operator-framework/deppy/pkg/deppy"
"github.com/operator-framework/deppy/pkg/deppy/constraint"
"github.com/operator-framework/deppy/pkg/deppy/input"
)

var _ variablesource.VariableSource = &ConstraintGenerator{}
var _ input.VariableSource = &ConstraintGenerator{}

type ConstraintGenerator struct {
dimacs *Dimacs
Expand All @@ -21,13 +21,13 @@ func NewDimacsVariableSource(dimacs *Dimacs) *ConstraintGenerator {
}
}

func (d *ConstraintGenerator) GetVariables(ctx context.Context, entitySource entitysource.EntitySource) ([]sat.Variable, error) {
varMap := make(map[entitysource.EntityID]*variablesource.Variable, len(d.dimacs.variables))
variables := make([]sat.Variable, 0, len(d.dimacs.variables))
if err := entitySource.Iterate(ctx, func(entity *entitysource.Entity) error {
variable := variablesource.NewVariable(sat.Identifier(entity.ID()))
func (d *ConstraintGenerator) GetVariables(ctx context.Context, entitySource input.EntitySource) ([]deppy.Variable, error) {
varMap := make(map[deppy.Identifier]*input.SimpleVariable, len(d.dimacs.variables))
variables := make([]deppy.Variable, 0, len(d.dimacs.variables))
if err := entitySource.Iterate(ctx, func(entity *input.Entity) error {
variable := input.NewSimpleVariable(entity.Identifier())
variables = append(variables, variable)
varMap[entity.ID()] = variable
varMap[entity.Identifier()] = variable
return nil
}); err != nil {
return nil, err
Expand All @@ -43,23 +43,23 @@ func (d *ConstraintGenerator) GetVariables(ctx context.Context, entitySource ent

if len(terms) == 1 {
// TODO: check constraints haven't already been added to the variable
variable := varMap[entitysource.EntityID(strings.TrimPrefix(first, "-"))]
variable := varMap[deppy.Identifier(strings.TrimPrefix(first, "-"))]
if strings.HasPrefix(first, "-") {
variable.AddConstraint(sat.Not())
variable.AddConstraint(constraint.Not())
} else {
// TODO: is this the right constraint here? (given that its an achoring constraint?)
variable.AddConstraint(sat.Mandatory())
variable.AddConstraint(constraint.Mandatory())
}
continue
}
for i := 1; i < len(terms); i++ {
variable := varMap[entitysource.EntityID(strings.TrimPrefix(first, "-"))]
variable := varMap[deppy.Identifier(strings.TrimPrefix(first, "-"))]
second := terms[i]
negSubject := strings.HasPrefix(first, "-")
negOperand := strings.HasPrefix(second, "-")

// TODO: this Or constraint is hacky as hell
variable.AddConstraint(sat.Or(sat.Identifier(strings.TrimPrefix(second, "-")), negSubject, negOperand))
variable.AddConstraint(constraint.Or(deppy.Identifier(strings.TrimPrefix(second, "-")), negSubject, negOperand))
first = second
}
}
Expand Down
15 changes: 8 additions & 7 deletions cmd/dimacs/dimacs_source.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
package dimacs

import (
"github.com/operator-framework/deppy/pkg/entitysource"
"github.com/operator-framework/deppy/pkg/deppy"
"github.com/operator-framework/deppy/pkg/deppy/input"
)

var _ entitysource.EntitySource = &EntitySource{}
var _ input.EntitySource = &EntitySource{}

type EntitySource struct {
*entitysource.CacheEntitySource
*input.CacheEntitySource
}

func NewDimacsEntitySource(dimacs *Dimacs) *EntitySource {
entities := make(map[entitysource.EntityID]entitysource.Entity, len(dimacs.Variables()))
entities := make(map[deppy.Identifier]input.Entity, len(dimacs.Variables()))
for _, variable := range dimacs.Variables() {
id := entitysource.EntityID(variable)
entities[id] = *entitysource.NewEntity(entitysource.EntityID(variable), nil)
id := deppy.Identifier(variable)
entities[id] = *input.NewEntity(id, nil)
}

return &EntitySource{
CacheEntitySource: entitysource.NewCacheQuerier(entities),
CacheEntitySource: input.NewCacheQuerier(entities),
}
}
2 changes: 1 addition & 1 deletion cmd/sudoku/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/spf13/cobra"

"github.com/operator-framework/deppy/pkg/solver"
"github.com/operator-framework/deppy/pkg/deppy/solver"
)

func NewSudokuCommand() *cobra.Command {
Expand Down
52 changes: 26 additions & 26 deletions cmd/sudoku/sudoku.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,32 @@ import (
"strconv"
"time"

"github.com/operator-framework/deppy/pkg/entitysource"
"github.com/operator-framework/deppy/pkg/sat"
"github.com/operator-framework/deppy/pkg/variablesource"
"github.com/operator-framework/deppy/pkg/deppy"
"github.com/operator-framework/deppy/pkg/deppy/constraint"
"github.com/operator-framework/deppy/pkg/deppy/input"
)

var _ entitysource.EntitySource = &Sudoku{}
var _ variablesource.VariableSource = &Sudoku{}
var _ input.EntitySource = &Sudoku{}
var _ input.VariableSource = &Sudoku{}

type Sudoku struct {
*entitysource.CacheEntitySource
*input.CacheEntitySource
}

func GetID(row int, col int, num int) entitysource.EntityID {
func GetID(row int, col int, num int) deppy.Identifier {
n := num
n += col * 9
n += row * 81
return entitysource.EntityID(fmt.Sprintf("%03d", n))
return deppy.Identifier(fmt.Sprintf("%03d", n))
}

func NewSudoku() *Sudoku {
var entities = make(map[entitysource.EntityID]entitysource.Entity, 9*9*9)
var entities = make(map[deppy.Identifier]input.Entity, 9*9*9)
for row := 0; row < 9; row++ {
for col := 0; col < 9; col++ {
for num := 0; num < 9; num++ {
id := GetID(row, col, num)
entities[id] = *entitysource.NewEntity(id, map[string]string{
entities[id] = *input.NewEntity(id, map[string]string{
"row": strconv.Itoa(row),
"col": strconv.Itoa(col),
"num": strconv.Itoa(num),
Expand All @@ -41,21 +41,21 @@ func NewSudoku() *Sudoku {
}
}
return &Sudoku{
CacheEntitySource: entitysource.NewCacheQuerier(entities),
CacheEntitySource: input.NewCacheQuerier(entities),
}
}

func (s Sudoku) GetVariables(ctx context.Context, _ entitysource.EntitySource) ([]sat.Variable, error) {
func (s Sudoku) GetVariables(ctx context.Context, _ input.EntitySource) ([]deppy.Variable, error) {
// adapted from: https://github.com/go-air/gini/blob/871d828a26852598db2b88f436549634ba9533ff/sudoku_test.go#L10
variables := make(map[sat.Identifier]*variablesource.Variable, 0)
inorder := make([]sat.Variable, 0)
variables := make(map[deppy.Identifier]*input.SimpleVariable, 0)
inorder := make([]deppy.Variable, 0)
rand.Seed(time.Now().UnixNano())

// create variables for all number in all positions of the board
for row := 0; row < 9; row++ {
for col := 0; col < 9; col++ {
for n := 0; n < 9; n++ {
variable := variablesource.NewVariable(sat.Identifier(GetID(row, col, n)))
variable := input.NewSimpleVariable(GetID(row, col, n))
variables[variable.Identifier()] = variable
inorder = append(inorder, variable)
}
Expand All @@ -65,16 +65,16 @@ func (s Sudoku) GetVariables(ctx context.Context, _ entitysource.EntitySource) (
// add a clause stating that every position on the board has a number
for row := 0; row < 9; row++ {
for col := 0; col < 9; col++ {
ids := make([]sat.Identifier, 9)
ids := make([]deppy.Identifier, 9)
for n := 0; n < 9; n++ {
ids[n] = sat.Identifier(GetID(row, col, n))
ids[n] = GetID(row, col, n)
}
// randomize order to create new sudoku boards every run
rand.Shuffle(len(ids), func(i, j int) { ids[i], ids[j] = ids[j], ids[i] })

// create clause that the particular position has a number
varID := sat.Identifier(fmt.Sprintf("%d-%d has a number", row, col))
variable := variablesource.NewVariable(varID, sat.Mandatory(), sat.Dependency(ids...))
varID := deppy.Identifier(fmt.Sprintf("%d-%d has a number", row, col))
variable := input.NewSimpleVariable(varID, constraint.Mandatory(), constraint.Dependency(ids...))
variables[varID] = variable
inorder = append(inorder, variable)
}
Expand All @@ -84,10 +84,10 @@ func (s Sudoku) GetVariables(ctx context.Context, _ entitysource.EntitySource) (
for n := 0; n < 9; n++ {
for row := 0; row < 9; row++ {
for colA := 0; colA < 9; colA++ {
idA := sat.Identifier(GetID(row, colA, n))
idA := GetID(row, colA, n)
variable := variables[idA]
for colB := colA + 1; colB < 9; colB++ {
variable.AddConstraint(sat.Conflict(sat.Identifier(GetID(row, colB, n))))
variable.AddConstraint(constraint.Conflict(GetID(row, colB, n)))
}
}
}
Expand All @@ -97,10 +97,10 @@ func (s Sudoku) GetVariables(ctx context.Context, _ entitysource.EntitySource) (
for n := 0; n < 9; n++ {
for col := 0; col < 9; col++ {
for rowA := 0; rowA < 9; rowA++ {
idA := sat.Identifier(GetID(rowA, col, n))
idA := GetID(rowA, col, n)
variable := variables[idA]
for rowB := rowA + 1; rowB < 9; rowB++ {
variable.AddConstraint(sat.Conflict(sat.Identifier(GetID(rowB, col, n))))
variable.AddConstraint(constraint.Conflict(GetID(rowB, col, n)))
}
}
}
Expand All @@ -114,12 +114,12 @@ func (s Sudoku) GetVariables(ctx context.Context, _ entitysource.EntitySource) (
// all numbers
for n := 0; n < 9; n++ {
for i, offA := range offs {
idA := sat.Identifier(GetID(x+offA.x, y+offA.y, n))
idA := GetID(x+offA.x, y+offA.y, n)
variable := variables[idA]
for j := i + 1; j < len(offs); j++ {
offB := offs[j]
idB := sat.Identifier(GetID(x+offB.x, y+offB.y, n))
variable.AddConstraint(sat.Conflict(idB))
idB := GetID(x+offB.x, y+offB.y, n)
variable.AddConstraint(constraint.Conflict(idB))
}
}
}
Expand Down
23 changes: 13 additions & 10 deletions pkg/sat/bench_test.go → internal/solver/bench_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package sat
package solver

import (
"context"
"math/rand"
"strconv"
"testing"

"github.com/operator-framework/deppy/pkg/deppy"
"github.com/operator-framework/deppy/pkg/deppy/constraint"
)

var BenchmarkInput = func() []Variable {
var BenchmarkInput = func() []deppy.Variable {
const (
length = 256
seed = 9
Expand All @@ -18,26 +21,26 @@ var BenchmarkInput = func() []Variable {
nConflict = 3
)

id := func(i int) Identifier {
return Identifier(strconv.Itoa(i))
id := func(i int) deppy.Identifier {
return deppy.Identifier(strconv.Itoa(i))
}

variable := func(i int) TestVariable {
var c []Constraint
var c []deppy.Constraint
if rand.Float64() < pMandatory {
c = append(c, Mandatory())
c = append(c, constraint.Mandatory())
}
if rand.Float64() < pDependency {
n := rand.Intn(nDependency-1) + 1
var d []Identifier
var d []deppy.Identifier
for x := 0; x < n; x++ {
y := i
for y == i {
y = rand.Intn(length)
}
d = append(d, id(y))
}
c = append(c, Dependency(d...))
c = append(c, constraint.Dependency(d...))
}
if rand.Float64() < pConflict {
n := rand.Intn(nConflict-1) + 1
Expand All @@ -46,7 +49,7 @@ var BenchmarkInput = func() []Variable {
for y == i {
y = rand.Intn(length)
}
c = append(c, Conflict(id(y)))
c = append(c, constraint.Conflict(id(y)))
}
}
return TestVariable{
Expand All @@ -56,7 +59,7 @@ var BenchmarkInput = func() []Variable {
}

rand.Seed(seed)
result := make([]Variable, length)
result := make([]deppy.Variable, length)
for i := range result {
result[i] = variable(i)
}
Expand Down
28 changes: 28 additions & 0 deletions internal/solver/constraints.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package solver

import (
"github.com/go-air/gini/z"

"github.com/operator-framework/deppy/pkg/deppy"
)

// zeroConstraint is returned by ConstraintOf in error cases.
type zeroConstraint struct{}

var _ deppy.Constraint = zeroConstraint{}

func (zeroConstraint) String(subject deppy.Identifier) string {
return ""
}

func (zeroConstraint) Apply(lm deppy.LitMapping, subject deppy.Identifier) z.Lit {
return z.LitNull
}

func (zeroConstraint) Order() []deppy.Identifier {
return nil
}

func (zeroConstraint) Anchor() bool {
return false
}
43 changes: 43 additions & 0 deletions internal/solver/constraints_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package solver

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/operator-framework/deppy/pkg/deppy/constraint"

"github.com/operator-framework/deppy/pkg/deppy"
)

func TestOrder(t *testing.T) {
type tc struct {
Name string
Constraint deppy.Constraint
Expected []deppy.Identifier
}

for _, tt := range []tc{
{
Name: "mandatory",
Constraint: constraint.Mandatory(),
},
{
Name: "prohibited",
Constraint: constraint.Prohibited(),
},
{
Name: "dependency",
Constraint: constraint.Dependency("a", "b", "c"),
Expected: []deppy.Identifier{"a", "b", "c"},
},
{
Name: "conflict",
Constraint: constraint.Conflict("a"),
},
} {
t.Run(tt.Name, func(t *testing.T) {
assert.Equal(t, tt.Expected, tt.Constraint.Order())
})
}
}
2 changes: 1 addition & 1 deletion pkg/sat/doc.go → internal/solver/doc.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Package solver implements a general-purpose solver for boolean
// constraint satisfiability problems.
package sat
package solver
Loading