Skip to content

add max parallel goroutines option #12

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

Open
wants to merge 4 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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# go-bayesopt [![Build Status](https://travis-ci.org/d4l3k/go-bayesopt.svg?branch=master)](https://travis-ci.org/d4l3k/go-bayesopt) [![GoDoc](https://godoc.org/github.com/d4l3k/go-bayesopt?status.svg)](https://godoc.org/github.com/d4l3k/go-bayesopt)
# go-bayesopt [![Build Status](https://travis-ci.org/d4l3k/go-bayesopt.svg?branch=master)](https://travis-ci.org/d4l3k/go-bayesopt) [![GoDoc](https://godoc.org/github.com/anyongjin/go-bayesopt?status.svg)](https://godoc.org/github.com/anyongjin/go-bayesopt)

A library for doing Bayesian Optimization using Gaussian Processes (blackbox
optimizer) in Go/Golang.
Expand All @@ -15,7 +15,7 @@ import (
"log"
"math"

"github.com/d4l3k/go-bayesopt"
"github.com/anyongjin/go-bayesopt"
)

func main() {
Expand Down
55 changes: 38 additions & 17 deletions bayesopt.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package bayesopt

import (
"fmt"
"sync"

"github.com/pkg/errors"

"gonum.org/v1/gonum/optimize"

"github.com/d4l3k/go-bayesopt/gp"
"github.com/anyongjin/go-bayesopt/gp"
)

const (
Expand All @@ -17,6 +18,8 @@ const (
DefaultRandomRounds = 5
// DefaultMinimize is the default value of minimize.
DefaultMinimize = true
// DefaultParallel is the default value of max number goroutines run parallelly.
DefaultParallel = 5

NumRandPoints = 100000
NumGradPoints = 256
Expand All @@ -39,6 +42,7 @@ type Optimizer struct {
exploration Exploration
minimize bool
barrierFunc BarrierFunc
parallel int

running bool
explorationErr error
Expand All @@ -50,11 +54,11 @@ type OptimizerOption func(*Optimizer)

// WithOutputName sets the outputs name. Only really matters if you're planning
// on using gp/plot.
func WithOutputName(name string) OptimizerOption {
return func(o *Optimizer) {
o.updateNames(name)
}
}
// func WithOutputName(name string) OptimizerOption {
// return func(o *Optimizer) {
// o.updateNames(name)
// }
// }

// WithRandomRounds sets the number of random rounds to run.
func WithRandomRounds(rounds int) OptimizerOption {
Expand Down Expand Up @@ -92,6 +96,13 @@ func WithBarrierFunc(bf BarrierFunc) OptimizerOption {
}
}

// WithParallel sets max number of running goroutines parallelly.
func WithParallel(num int) OptimizerOption {
return func(o *Optimizer) {
o.mu.parallel = num
}
}

// New creates a new optimizer with the specified optimizable parameters and
// options.
func New(params []Param, opts ...OptimizerOption) *Optimizer {
Expand All @@ -104,28 +115,33 @@ func New(params []Param, opts ...OptimizerOption) *Optimizer {
o.mu.rounds = DefaultRounds
o.mu.exploration = DefaultExploration
o.mu.minimize = DefaultMinimize
o.mu.parallel = DefaultParallel
o.mu.barrierFunc = DefaultBarrierFunc

o.updateNames("")
//o.updateNames("")

for _, opt := range opts {
opt(o)
}

if o.mu.parallel <= 0 {
panic(fmt.Sprintf("parallel must >= 1, current: %v", o.mu.parallel))
}

return o
}

// updateNames sets the gaussian process names.
func (o *Optimizer) updateNames(outputName string) {
o.mu.Lock()
defer o.mu.Unlock()
// func (o *Optimizer) updateNames(outputName string) {
// o.mu.Lock()
// defer o.mu.Unlock()

var inputNames []string
for _, p := range o.mu.params {
inputNames = append(inputNames, p.GetName())
}
o.mu.gp.SetNames(inputNames, outputName)
}
// var inputNames []string
// for _, p := range o.mu.params {
// inputNames = append(inputNames, p.GetName())
// }
// o.mu.gp.SetNames(inputNames, outputName)
// }

// GP returns the underlying gaussian process. Primary for use with plotting
// behavior.
Expand Down Expand Up @@ -323,6 +339,7 @@ func (o *Optimizer) Optimize(f func(map[Param]float64) float64) (x map[Param]flo
o.mu.running = true
o.mu.Unlock()

guard := make(chan struct{}, o.mu.parallel)
var wg sync.WaitGroup
for {
if !o.Running() {
Expand All @@ -337,9 +354,13 @@ func (o *Optimizer) Optimize(f func(map[Param]float64) float64) (x map[Param]flo
break
}
if parallel {
guard <- struct{}{}
wg.Add(1)
go func() {
defer wg.Done()
defer func() {
<-guard
wg.Done()
}()

o.Log(x, f(x))
}()
Expand Down
19 changes: 9 additions & 10 deletions bayesopt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"math"
"testing"

"github.com/d4l3k/go-bayesopt/gp/plot"
"gonum.org/v1/gonum/floats"
)

Expand All @@ -31,9 +30,9 @@ func TestOptimizer(t *testing.T) {
x, y := o.GP().RawData()
t.Logf("x %+v\ny %+v", x, y)
}
if _, err := plot.SaveAll(o.GP()); err != nil {
t.Errorf("plot error: %+v", err)
}
// if _, err := plot.SaveAll(o.GP()); err != nil {
// t.Errorf("plot error: %+v", err)
// }

{
got := x[X]
Expand Down Expand Up @@ -78,9 +77,9 @@ func TestOptimizerMax(t *testing.T) {
x, y := o.GP().RawData()
t.Logf("x %+v\ny %+v", x, y)
}
if _, err := plot.SaveAll(o.GP()); err != nil {
t.Errorf("plot error: %+v", err)
}
// if _, err := plot.SaveAll(o.GP()); err != nil {
// t.Errorf("plot error: %+v", err)
// }

{
got := x[X]
Expand Down Expand Up @@ -125,9 +124,9 @@ func TestOptimizerBounds(t *testing.T) {
x, y := o.GP().RawData()
t.Logf("x %+v\ny %+v", x, y)
}
if _, err := plot.SaveAll(o.GP()); err != nil {
t.Errorf("plot error: %+v", err)
}
// if _, err := plot.SaveAll(o.GP()); err != nil {
// t.Errorf("plot error: %+v", err)
// }

{
got := x[X]
Expand Down
2 changes: 1 addition & 1 deletion exploration.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package bayesopt
import (
"math"

"github.com/d4l3k/go-bayesopt/gp"
"github.com/anyongjin/go-bayesopt/gp"
)

// Exploration is the strategy to use for exploring the Gaussian process.
Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
module github.com/d4l3k/go-bayesopt
module github.com/anyongjin/go-bayesopt

go 1.12

require (
github.com/blend/go-sdk v2.0.0+incompatible // indirect
github.com/pkg/errors v0.8.1
github.com/wcharczuk/go-chart v2.0.1+incompatible
gonum.org/v1/gonum v0.6.0
)
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
github.com/blend/go-sdk v2.0.0+incompatible h1:FL9X/of4ZYO5D2JJNI4vHrbXPfuSDbUa7h8JP9+E92w=
github.com/blend/go-sdk v2.0.0+incompatible/go.mod h1:3GUb0YsHFNTJ6hsJTpzdmCUl05o8HisKjx5OAlzYKdw=
github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/wcharczuk/go-chart v2.0.1+incompatible h1:0pz39ZAycJFF7ju/1mepnk26RLVLBCWz1STcD3doU0A=
github.com/wcharczuk/go-chart v2.0.1+incompatible/go.mod h1:PF5tmL4EIx/7Wf+hEkpCqYi5He4u90sw+0+6FhrryuE=
golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20180807140117-3d87b88a115f/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190125153040-c74c464bbbf2 h1:y102fOLFqhV41b+4GPiJoa0k/x+pJcEi2/HB1Y5T6fU=
golang.org/x/exp v0.0.0-20190125153040-c74c464bbbf2/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81 h1:00VmoueYNlNz/aHIilyyQz/MHSqGoWJzpFv/HW8xpzI=
golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs=
golang.org/x/tools v0.0.0-20180525024113-a5b4c53f6e8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190206041539-40960b6deb8e h1:Io7mpb+aUAGF0MKxbyQ7HQl1VgB+cL6ZJZUFaFNqVV4=
Expand Down
6 changes: 1 addition & 5 deletions gp/gp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import (

"gonum.org/v1/gonum/floats"

"github.com/d4l3k/go-bayesopt/gp"
"github.com/d4l3k/go-bayesopt/gp/plot"
"github.com/anyongjin/go-bayesopt/gp"
)

func f(x, y float64) float64 {
Expand All @@ -28,9 +27,6 @@ func TestKnown(t *testing.T) {
gpAdd(gp, rand.Float64()*2*math.Pi-math.Pi, rand.Float64()*2*math.Pi-math.Pi)
}

if _, err := plot.SaveAll(gp); err != nil {
t.Fatal(err)
}
mean, variance, err := gp.Estimate([]float64{0.25, 0.75})
if err != nil {
t.Fatal(err)
Expand Down
Loading