Skip to content

Commit

Permalink
Removed parameters from minimization API.
Browse files Browse the repository at this point in the history
* The parameters being part of the minimization function was a poor API
  design choice as there does not seem to be a good use case and it
  complicated the code, not to mention that arbitrary parameters objects
  are messy.
* The API without the parameters will be more suitable for an initial
  release.
  • Loading branch information
afbarnard committed Jul 3, 2013
1 parent 8da3861 commit 28714f8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 69 deletions.
7 changes: 4 additions & 3 deletions example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import (
// allows this example program to use this package directly in its
// workspace without installing anything.
lbfgsb "github.com/afbarnard/go-lbfgsb"
//lbfgsb ".."
)

func main() {
Expand All @@ -70,7 +71,7 @@ func main() {
// Minimize sphere function
fmt.Printf("----- Sphere Function -----\n")
x0_5d := []float64{10.0, -9.0, 8.0, -7.0, 6.0}
minimum, exitStatus := sphereOptimizer.Minimize(sphereObjective, x0_5d, nil)
minimum, exitStatus := sphereOptimizer.Minimize(sphereObjective, x0_5d)
stats := sphereOptimizer.OptimizationStatistics()
PrintResults(sphereMin, minimum, exitStatus, stats)

Expand All @@ -97,7 +98,7 @@ func main() {
// Minimize Rosenbrock
fmt.Printf("----- Rosenbrock Function -----\n")
x0_2d := []float64{10.0, 11.0}
minimum, exitStatus = rosenOptimizer.Minimize(rosenObjective, x0_2d, nil)
minimum, exitStatus = rosenOptimizer.Minimize(rosenObjective, x0_2d)
stats = rosenOptimizer.OptimizationStatistics()
PrintResults(rosenMin, minimum, exitStatus, stats)

Expand All @@ -115,7 +116,7 @@ func main() {
func(info *lbfgsb.OptimizationIterationInformation) {
LogOptimizationIteration(logger, info)
})
minimum, exitStatus = sphereOptimizer.Minimize(sphereObjective, x0_5d, nil)
minimum, exitStatus = sphereOptimizer.Minimize(sphereObjective, x0_5d)
stats = sphereOptimizer.OptimizationStatistics()
PrintResults(sphereMin, minimum, exitStatus, stats)

Expand Down
59 changes: 5 additions & 54 deletions lbfgsb.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,16 +251,13 @@ func (lbfgsb *Lbfgsb) SetLogger(
// Implements OptimizationFunctionMinimizer.Minimize.
func (lbfgsb *Lbfgsb) Minimize(
objective FunctionWithGradient,
initialPoint []float64,
parameters map[string]interface{}) (
initialPoint []float64) (
minimum PointValueGradient,
exitStatus ExitStatus) {

// Make sure object has been initialized
lbfgsb.Init(len(initialPoint))

// TODO OMG! split this out into some helper functions

// Check dimensionality
dim := len(initialPoint)
dim_c := C.int(dim)
Expand All @@ -270,52 +267,6 @@ func (lbfgsb *Lbfgsb) Minimize(
return
}

// Process the parameters. Argument 'parameters' overrides but does
// not replace class-level parameters. The bounds are not part of
// the parameters, they are part of the problem specification.
var paramVal interface{}
var ok bool
// Approximation size
approximationSize := lbfgsb.approximationSize
if paramVal, ok = parameters["approximationSize"]; ok {
approximationSize, ok = paramVal.(int)
if !ok || approximationSize <= 0 {
exitStatus.Code = USAGE_ERROR
exitStatus.Message = fmt.Sprintf("Bad parameter value: approximationSize: %v. Expected integer >= 1.", paramVal)
return
}
}
// F tolerance
fTolerance := lbfgsb.fTolerance
if paramVal, ok = parameters["fTolerance"]; ok {
fTolerance, ok = paramVal.(float64)
if !ok || fTolerance <= 0.0 {
exitStatus.Code = USAGE_ERROR
exitStatus.Message = fmt.Sprintf("Bad parameter value: fTolerance: %v. Expected float >= 0.", paramVal)
return
}
}
// G tolerance
gTolerance := lbfgsb.gTolerance
if paramVal, ok = parameters["gTolerance"]; ok {
gTolerance, ok = paramVal.(float64)
if !ok || gTolerance <= 0.0 {
exitStatus.Code = USAGE_ERROR
exitStatus.Message = fmt.Sprintf("Bad parameter value: gTolerance: %v. Expected float >= 0.", paramVal)
return
}
}
// Debug level
printControl := lbfgsb.printControl
if paramVal, ok = parameters["printControl"]; ok {
printControl, ok = paramVal.(int)
if !ok || printControl < 0 {
exitStatus.Code = USAGE_ERROR
exitStatus.Message = fmt.Sprintf("Bad parameter value: printControl: %v. Expected integer >= 0.", paramVal)
return
}
}

// Set up bounds control. Use a C-compatible type.
boundsControl := make([]C.int, dim)
if lbfgsb.lowerBounds != nil {
Expand Down Expand Up @@ -356,10 +307,10 @@ func (lbfgsb *Lbfgsb) Minimize(
minimum.G = make([]float64, dim)

// Convert parameters for C
approximationSize_c := C.int(approximationSize)
fTolerance_c := C.double(fTolerance)
gTolerance_c := C.double(gTolerance)
printControl_c := C.int(printControl)
approximationSize_c := C.int(lbfgsb.approximationSize)
fTolerance_c := C.double(lbfgsb.fTolerance)
gTolerance_c := C.double(lbfgsb.gTolerance)
printControl_c := C.int(lbfgsb.printControl)

// Prepare buffers and arrays for C. Avoid allocation in C land by
// allocating compatible things in Go and passing their addresses.
Expand Down
14 changes: 2 additions & 12 deletions optim.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,8 @@ type ObjectiveFunctionMinimizer interface {
// objective function starting from the given point. Returns the
// minimum (or the best point found) and the status of the algorithm
// at exit.
//
// 'parameters' is a map containing parameters for the optimization
// algorithm. The parameters are optional and may be nil. If
// specified, the parameters override any previously-set parameters,
// but only for the duration of this invocation. Parameters whose
// names appear exactly as keys in the map will be interpreted. The
// interpretation may fail if the given value cannot be converted to
// a valid parameter value. Any other contents of the parameter map
// will be ignored.
Minimize(objective FunctionWithGradient, initialPoint []float64,
parameters map[string]interface{}) (
minimum PointValueGradient, exitStatus ExitStatus)
Minimize(objective FunctionWithGradient, initialPoint []float64) (
minimum PointValueGradient, exitStatus ExitStatus)
}

////////////////////////////////////////
Expand Down

0 comments on commit 28714f8

Please sign in to comment.