Skip to content

Commit

Permalink
Refactor solver to remove variables
Browse files Browse the repository at this point in the history
Signed-off-by: perdasilva <[email protected]>
  • Loading branch information
perdasilva committed Oct 11, 2022
1 parent 90318e1 commit a9c6824
Show file tree
Hide file tree
Showing 10 changed files with 480 additions and 422 deletions.
34 changes: 15 additions & 19 deletions internal/solver/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"testing"
)

var BenchmarkInput = func() []Variable {
var BenchmarkConstraints = func() []Constraint {
const (
length = 256
seed = 9
Expand All @@ -22,10 +22,11 @@ var BenchmarkInput = func() []Variable {
return Identifier(strconv.Itoa(i))
}

variable := func(i int) TestVariable {
input := func(i int) []Constraint {
var c []Constraint
subject := id(i)
if rand.Float64() < pMandatory {
c = append(c, Mandatory())
c = append(c, Mandatory(subject))
}
if rand.Float64() < pDependency {
n := rand.Intn(nDependency-1) + 1
Expand All @@ -37,7 +38,7 @@ var BenchmarkInput = func() []Variable {
}
d = append(d, id(y))
}
c = append(c, Dependency(d...))
c = append(c, Dependency(subject, d...))
}
if rand.Float64() < pConflict {
n := rand.Intn(nConflict-1) + 1
Expand All @@ -46,39 +47,34 @@ var BenchmarkInput = func() []Variable {
for y == i {
y = rand.Intn(length)
}
c = append(c, Conflict(id(y)))
c = append(c, Conflict(subject, id(y)))
}
}
return TestVariable{
identifier: id(i),
constraints: c,
}
return c
}

rand.Seed(seed)
result := make([]Variable, length)
for i := range result {
result[i] = variable(i)
constraints := make([]Constraint, 0)
for i := 0; i < length; i++ {
constrs := input(i)
constraints = append(constraints, constrs...)
}
return result
return constraints
}()

func BenchmarkSolve(b *testing.B) {
for i := 0; i < b.N; i++ {
s, err := New(WithInput(BenchmarkInput))
if err != nil {
b.Fatalf("failed to initialize solver: %s", err)
}
_, err = s.Solve(context.Background())
s, err := New(WithInput(BenchmarkConstraints))
if err != nil {
b.Fatalf("failed to initialize solver: %s", err)
}
s.Solve(context.Background())
}
}

func BenchmarkNewInput(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := New(WithInput(BenchmarkInput))
_, err := New(WithInput(BenchmarkConstraints))
if err != nil {
b.Fatalf("failed to initialize solver: %s", err)
}
Expand Down
Loading

0 comments on commit a9c6824

Please sign in to comment.