Skip to content

Commit

Permalink
Export internal solver
Browse files Browse the repository at this point in the history
Signed-off-by: Mikalai Radchuk <mradchuk@redhat.com>
  • Loading branch information
m1kola committed Dec 4, 2023
1 parent fc57b6f commit eae1c32
Show file tree
Hide file tree
Showing 14 changed files with 20 additions and 7 deletions.
16 changes: 13 additions & 3 deletions cmd/dimacs/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/spf13/cobra"

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

Expand Down Expand Up @@ -52,20 +53,29 @@ func solve(path string) error {
}

// build solver
so := solver.NewDeppySolver()
so, err := solver.New()
if err != nil {
return err
}

// get solution
vars, err := GenerateVariables(dimacs)
if err != nil {
return fmt.Errorf("error generating variables: %s", err)
}
solution, err := so.Solve(vars)
selection, err := so.Solve(vars)
if err != nil {
fmt.Printf("no solution found: %s\n", err)
} else {
selected := map[deppy.Identifier]struct{}{}
for _, variable := range selection {
selected[variable.Identifier()] = struct{}{}
}

fmt.Println("solution found:")
for _, variable := range vars {
fmt.Printf("%s = %t\n", variable.Identifier(), solution.IsSelected(variable.Identifier()))
_, ok := selected[variable.Identifier()]
fmt.Printf("%s = %t\n", variable.Identifier(), ok)
}
}

Expand Down
9 changes: 6 additions & 3 deletions cmd/sudoku/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,22 @@ func NewSudokuCommand() *cobra.Command {

func solve() error {
// build solver
so := solver.NewDeppySolver()
so, err := solver.New()
if err != nil {
return err
}

// get solution
vars, err := GenerateVariables()
if err != nil {
return err
}
solution, err := so.Solve(vars)
selection, err := so.Solve(vars)
if err != nil {
fmt.Println("no solution found")
} else {
selected := map[deppy.Identifier]struct{}{}
for _, variable := range solution.SelectedVariables() {
for _, variable := range selection {
selected[variable.Identifier()] = struct{}{}
}
for row := 0; row < 9; row++ {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var BenchmarkInput = func() []deppy.Variable {
nConflict = 3
)

random := rand.New(rand.NewSource(seed))
random := rand.New(rand.NewSource(seed)) //nolint:gosec // G404: Use of weak random number generator (math/rand instead of crypto/rand) is ignored as this is not security-sensitive.

id := func(i int) deppy.Identifier {
return deppy.Identifier(strconv.Itoa(i))
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit eae1c32

Please sign in to comment.