Skip to content

Commit

Permalink
optimize: Remove Local function
Browse files Browse the repository at this point in the history
This change removes the Local function. In order to do so, this changes the previous LocalGlobal wrapper to LocalController to allow Local methods to be used as a Global optimizer. This adds methods to all of the Local methods in order to implement GlobalMethod, and changes the tests accordingly. The next commit will fix all of the names
  • Loading branch information
btracey committed Jun 29, 2018
1 parent e9e5634 commit d1590c8
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 167 deletions.
18 changes: 18 additions & 0 deletions optimize/bfgs.go
Expand Up @@ -21,6 +21,8 @@ type BFGS struct {
// If Linesearcher == nil, an appropriate default is chosen.
Linesearcher Linesearcher

local *LocalController

ls *LinesearchMethod

dim int
Expand All @@ -35,6 +37,22 @@ type BFGS struct {
first bool // Indicator of the first iteration.
}

func (b *BFGS) Status() (Status, error) {
return b.local.Status()
}

func (b *BFGS) InitGlobal(dim, tasks int) int {
if b.local == nil {
b.local = &LocalController{Method: b}
}
return b.local.InitGlobal(dim, tasks)
}

func (b *BFGS) RunGlobal(operation chan<- GlobalTask, result <-chan GlobalTask, tasks []GlobalTask) {
b.local.RunGlobal(operation, result, tasks)
return
}

func (b *BFGS) Init(loc *Location) (Operation, error) {
if b.Linesearcher == nil {
b.Linesearcher = &Bisection{}
Expand Down
18 changes: 18 additions & 0 deletions optimize/cg.go
Expand Up @@ -88,6 +88,8 @@ type CG struct {
// CG will panic if AngleRestartThreshold is not in the interval [-1, 0].
AngleRestartThreshold float64

local *LocalController

ls *LinesearchMethod

restartAfter int
Expand All @@ -98,6 +100,22 @@ type CG struct {
gradPrevNorm float64
}

func (cg *CG) Status() (Status, error) {
return cg.local.Status()
}

func (cg *CG) InitGlobal(dim, tasks int) int {
if cg.local == nil {
cg.local = &LocalController{Method: cg}
}
return cg.local.InitGlobal(dim, tasks)
}

func (cg *CG) RunGlobal(operation chan<- GlobalTask, result <-chan GlobalTask, tasks []GlobalTask) {
cg.local.RunGlobal(operation, result, tasks)
return
}

func (cg *CG) Init(loc *Location) (Operation, error) {
if cg.IterationRestartFactor < 0 {
panic("cg: IterationRestartFactor is negative")
Expand Down
9 changes: 8 additions & 1 deletion optimize/global.go
Expand Up @@ -115,7 +115,7 @@ type GlobalMethod interface {
func Global(p Problem, dim int, settings *Settings, method GlobalMethod) (*Result, error) {
startTime := time.Now()
if method == nil {
method = &GuessAndCheck{}
method = getDefaultMethod(&p)
}
if settings == nil {
settings = DefaultSettingsGlobal()
Expand Down Expand Up @@ -161,6 +161,13 @@ func Global(p Problem, dim int, settings *Settings, method GlobalMethod) (*Resul
}, err
}

func getDefaultMethod(p *Problem) GlobalMethod {
if p.Grad != nil {
return &BFGS{}
}
return &NelderMead{}
}

// minimizeGlobal performs a Global optimization. minimizeGlobal updates the
// settings and optLoc, and returns the final Status and error.
func minimizeGlobal(prob *Problem, method GlobalMethod, settings *Settings, stats *Stats, initOp Operation, initLoc, optLoc *Location, startTime time.Time) (Status, error) {
Expand Down
18 changes: 18 additions & 0 deletions optimize/gradientdescent.go
Expand Up @@ -17,6 +17,24 @@ type GradientDescent struct {
StepSizer StepSizer

ls *LinesearchMethod

local *LocalController
}

func (g *GradientDescent) Status() (Status, error) {
return g.local.Status()
}

func (g *GradientDescent) InitGlobal(dim, tasks int) int {
if g.local == nil {
g.local = &LocalController{Method: g}
}
return g.local.InitGlobal(dim, tasks)
}

func (g *GradientDescent) RunGlobal(operation chan<- GlobalTask, result <-chan GlobalTask, tasks []GlobalTask) {
g.local.RunGlobal(operation, result, tasks)
return
}

func (g *GradientDescent) Init(loc *Location) (Operation, error) {
Expand Down
18 changes: 18 additions & 0 deletions optimize/lbfgs.go
Expand Up @@ -27,6 +27,8 @@ type LBFGS struct {
// If Store is 0, it will be defaulted to 15.
Store int

local *LocalController

ls *LinesearchMethod

dim int // Dimension of the problem
Expand All @@ -41,6 +43,22 @@ type LBFGS struct {
a []float64 // Cache of Hessian updates
}

func (l *LBFGS) Status() (Status, error) {
return l.local.Status()
}

func (l *LBFGS) InitGlobal(dim, tasks int) int {
if l.local == nil {
l.local = &LocalController{Method: l}
}
return l.local.InitGlobal(dim, tasks)
}

func (l *LBFGS) RunGlobal(operation chan<- GlobalTask, result <-chan GlobalTask, tasks []GlobalTask) {
l.local.RunGlobal(operation, result, tasks)
return
}

func (l *LBFGS) Init(loc *Location) (Operation, error) {
if l.Linesearcher == nil {
l.Linesearcher = &Bisection{}
Expand Down

0 comments on commit d1590c8

Please sign in to comment.