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

feat: support passing a seed in for reproducibility #117

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 10 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var (
Workers int
Nth int
Repeat int
Seed int64
V, VV bool
)

Expand Down Expand Up @@ -73,6 +74,10 @@ func init() {
flag.IntVar(&Workers, "j", 0, "number of parallel workers (default uses all cores)")
flag.IntVar(&Nth, "nth", 1, "save every Nth frame (put \"%d\" in path)")
flag.IntVar(&Repeat, "rep", 0, "add N extra shapes per iteration with reduced search")
flag.Func("seed", "seed to feed the RNG", func(value string) (err error) {
Seed, err = strconv.ParseInt(value, 10, 64)
return
})
flag.BoolVar(&V, "v", false, "verbose")
flag.BoolVar(&VV, "vv", false, "very verbose")
}
Expand Down Expand Up @@ -126,7 +131,10 @@ func main() {
}

// seed random number generator
rand.Seed(time.Now().UTC().UnixNano())
if Seed == 0 {
Seed = time.Now().UTC().UnixNano()
}
rand.Seed(Seed)

// determine worker count
if Workers < 1 {
Expand All @@ -153,7 +161,7 @@ func main() {
}

// run algorithm
model := primitive.NewModel(input, bg, OutputSize, Workers)
model := primitive.NewModel(input, bg, OutputSize, Workers, Seed+1)
primitive.Log(1, "%d: t=%.3f, score=%.6f\n", 0, 0.0, model.Score)
start := time.Now()
frame := 0
Expand Down
4 changes: 2 additions & 2 deletions primitive/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Model struct {
Workers []*Worker
}

func NewModel(target image.Image, background Color, size, numWorkers int) *Model {
func NewModel(target image.Image, background Color, size, numWorkers int, seed int64) *Model {
w := target.Bounds().Size().X
h := target.Bounds().Size().Y
aspect := float64(w) / float64(h)
Expand All @@ -48,7 +48,7 @@ func NewModel(target image.Image, background Color, size, numWorkers int) *Model
model.Score = differenceFull(model.Target, model.Current)
model.Context = model.newContext()
for i := 0; i < numWorkers; i++ {
worker := NewWorker(model.Target)
worker := NewWorker(model.Target, seed)
model.Workers = append(model.Workers, worker)
}
return model
Expand Down
2 changes: 1 addition & 1 deletion primitive/raster.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func fix(x float64) fixed.Int26_6 {
}

func fixp(x, y float64) fixed.Point26_6 {
return fixed.Point26_6{fix(x), fix(y)}
return fixed.Point26_6{X: fix(x), Y: fix(y)}
}

type painter struct {
Expand Down
2 changes: 1 addition & 1 deletion primitive/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func SaveJPG(path string, im image.Image, quality int) error {
return err
}
defer file.Close()
return jpeg.Encode(file, im, &jpeg.Options{quality})
return jpeg.Encode(file, im, &jpeg.Options{Quality: quality})
}

func SaveGIF(path string, frames []image.Image, delay, lastDelay int) error {
Expand Down
5 changes: 2 additions & 3 deletions primitive/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package primitive
import (
"image"
"math/rand"
"time"

"github.com/golang/freetype/raster"
)
Expand All @@ -21,7 +20,7 @@ type Worker struct {
Counter int
}

func NewWorker(target *image.RGBA) *Worker {
func NewWorker(target *image.RGBA, seed int64) *Worker {
w := target.Bounds().Size().X
h := target.Bounds().Size().Y
worker := Worker{}
Expand All @@ -32,7 +31,7 @@ func NewWorker(target *image.RGBA) *Worker {
worker.Rasterizer = raster.NewRasterizer(w, h)
worker.Lines = make([]Scanline, 0, 4096) // TODO: based on height
worker.Heatmap = NewHeatmap(w, h)
worker.Rnd = rand.New(rand.NewSource(time.Now().UnixNano()))
worker.Rnd = rand.New(rand.NewSource(seed))
return &worker
}

Expand Down