Beispiel #1
0
func (m *gradOptimizer) Init() error {
	initLoc := m.loc.Initial
	initObj := m.obj.Initial
	initGrad := m.grad.Initial

	// The initial values need to both be NaN or both not nan
	if math.IsNaN(initObj) {
		if len(initGrad) != 0 {
			return errors.New("initial function value and gradient must either both be set or neither set")
		}
		// Both nan, so compute the initial fuction value and gradient
		initObj, initGrad, err := m.fun.ObjGrad(initLoc)
		if err != nil {
			return errors.New("error calling function during optimization: \n" + err.Error())
		}
		m.obj.Initial = initObj
		m.grad.Initial = initGrad
	} else {
		if len(initGrad) == 0 {
			return errors.New("initial function value and gradient must either both be set or neither set")
		}
	}

	err := common.Initialize(m.loc, m.obj, m.grad)
	if err != nil {
		return err
	}
	err = m.method.Init(m.loc, m.obj, m.grad)
	if err != nil {
		return err
	}
	return nil
}
Beispiel #2
0
func (u *gradOptimizer) Init() error {
	initLoc := u.loc.Initial
	initObj := u.obj.Initial
	initGrad := u.grad.Initial

	// The initial values need to both be NaN or both not nan
	if math.IsNaN(initObj) {
		if !math.IsNaN(initGrad) {
			return errors.New("gofunopter: cubic: initial function value and gradient must either both be set or neither set")
		}
		// Both nan, so compute the initial fuction value and gradient
		initObj, initGrad, err := u.fun.ObjGrad(initLoc)
		if err != nil {
			return errors.New("gofunopter: cubic: error calling function during optimization")
		}
		u.obj.Initial = initObj
		u.grad.Initial = initGrad
	} else {
		if math.IsNaN(initGrad) {
			return errors.New("gofunopter: cubic: initial function value and gradient must either both be set or neither set")
		}
	}

	err := common.Initialize(u.loc, u.obj, u.grad)
	if err != nil {
		return err
	}

	// Must call u.loc, etc. initialize first because the optimizer may need the initial values and locations
	// at the start
	err = u.method.Init(u.loc, u.obj, u.grad)
	if err != nil {
		return err
	}
	return nil
}