mirror of
https://github.com/gonum/gonum.git
synced 2025-10-05 15:16:59 +08:00
optimize: Change initialization, remove Needser, and update Problem f… (#779)
* optimize: Change initialization, remove Needser, and update Problem function calls We need a better way to express the Hessian function call so that sparse Hessians can be provided. This change updates the Problem function definitions to allow an arbitrary Symmetric matrix. With this change, we need to change how Location is used, so that we do not allocate a SymDense. Once this location is changed, we no longer need Needser to allocate the appropriate memory, and can shift that to initialization, further simplifying the interfaces. A 'fake' Problem is passed to Method to continue to make it impossible for the Method to call the functions directly. Fixes #727, #593.
This commit is contained in:
@@ -1162,9 +1162,21 @@ func testLocal(t *testing.T, tests []unconstrainedTest, method Method) {
|
||||
|
||||
settings := &Settings{}
|
||||
settings.Converger = defaultFunctionConverge()
|
||||
if method != nil && method.Needs().Gradient {
|
||||
var uses Available
|
||||
if method != nil {
|
||||
var err error
|
||||
has := availFromProblem(test.p)
|
||||
uses, err = method.Uses(has)
|
||||
if err != nil {
|
||||
t.Errorf("problem and method mismatch: %v", err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
if method != nil {
|
||||
// Turn off function convergence checks for gradient-based methods.
|
||||
settings.Converger = NeverTerminate{}
|
||||
if uses.Grad {
|
||||
settings.Converger = NeverTerminate{}
|
||||
}
|
||||
} else {
|
||||
if test.fIter == 0 {
|
||||
test.fIter = 20
|
||||
@@ -1222,7 +1234,7 @@ func testLocal(t *testing.T, tests []unconstrainedTest, method Method) {
|
||||
continue
|
||||
}
|
||||
|
||||
if !method.Needs().Gradient && !method.Needs().Hessian {
|
||||
if !uses.Grad && !uses.Hess {
|
||||
// Gradient-free tests can correctly terminate only with
|
||||
// FunctionConvergence status.
|
||||
if result.Status != FunctionConvergence {
|
||||
@@ -1234,11 +1246,11 @@ func testLocal(t *testing.T, tests []unconstrainedTest, method Method) {
|
||||
// evaluate them.
|
||||
settings.InitValues = &Location{}
|
||||
settings.InitValues.F = test.p.Func(test.x)
|
||||
if method.Needs().Gradient {
|
||||
if uses.Grad {
|
||||
settings.InitValues.Gradient = resize(settings.InitValues.Gradient, len(test.x))
|
||||
test.p.Grad(settings.InitValues.Gradient, test.x)
|
||||
}
|
||||
if method.Needs().Hessian {
|
||||
if uses.Hess {
|
||||
settings.InitValues.Hessian = mat.NewSymDense(len(test.x), nil)
|
||||
test.p.Hess(settings.InitValues.Hessian, test.x)
|
||||
}
|
||||
@@ -1266,13 +1278,13 @@ func testLocal(t *testing.T, tests []unconstrainedTest, method Method) {
|
||||
t.Errorf("Providing initial data does not reduce the number of Func calls for:\n%v", test)
|
||||
continue
|
||||
}
|
||||
if method.Needs().Gradient {
|
||||
if uses.Grad {
|
||||
if result.GradEvaluations != result2.GradEvaluations+1 {
|
||||
t.Errorf("Providing initial data does not reduce the number of Grad calls for:\n%v", test)
|
||||
continue
|
||||
}
|
||||
}
|
||||
if method.Needs().Hessian {
|
||||
if uses.Hess {
|
||||
if result.HessEvaluations != result2.HessEvaluations+1 {
|
||||
t.Errorf("Providing initial data does not reduce the number of Hess calls for:\n%v", test)
|
||||
continue
|
||||
|
Reference in New Issue
Block a user