예제 #1
0
def make_hessian(sol: Solution,
                 state: FrozenState) -> Tuple[Solution, Hessian]:
    sol = Solution(
        fval=sol.fval,
        x=sol.x,
        grad=sol.grad,
        proj=sol.proj,
        shifted_constr=sol.shifted_constr,
        hess_up_to_date=True,
    )
    H = findiff(state.gradient, sol.x, state.constraints)
    if state.opts.shaking == "x.shape[0]":
        max_times = sol.x.shape[0]
    else:
        max_times = state.opts.shaking
    return sol, Hessian(H, max_times=max_times)
예제 #2
0
def gradient_check(analytic: ndarray, x: ndarray, state: FrozenState,
                   check: GradientCheck) -> None:
    iter: Final[int] = check.iter
    gradient_infnorm: Final[float] = check.gradient_infnorm
    initial_gradient_infnorm: Final[float] = check.initial_gradient_infnorm

    opts: Final[Trust_Region_Options] = state.opts
    need_rel_check = (
        opts.check_iter is None or iter <= opts.check_iter
    ) and gradient_infnorm >= initial_gradient_infnorm * opts.check_rel

    if need_rel_check or opts.check_abs is not None:
        findiff_ = findiff(state.objective_np, x, state.constraints)
        assert len(findiff_.shape) == 2 and findiff_.shape[0] == 1
        findiff_.shape = (findiff_.shape[1], )

        if need_rel_check:
            relerr = difference.relative(analytic, findiff_)
            if relerr > opts.check_rel:
                raise Grad_Check_Failed(iter, relerr, analytic, findiff_)
        if opts.check_abs is not None:
            abserr = difference.absolute(analytic, findiff_)
            if abserr > opts.check_abs:
                raise Grad_Check_Failed(iter, abserr, analytic, findiff_)
예제 #3
0
 def test_原点(self) -> None:
     x = numpy.array([0, 0], dtype=numpy.float64)
     hessian = findiff.findiff(grad, x, (A, b, lb, ub))
     compare_hess(hessian, hessian_GT)