gemseo / post / core

hessians module¶

Approximation of the Hessian matrix from an optimization history.

Notations:

• $$f$$: the function of interest for which to approximate the Hessian matrix,

• $$y$$: the output value of $$f$$,

• $$x\in\mathbb{R}^d$$: the $$d$$ input variables of $$f$$,

• $$k$$: the $$k$$-th iteration of the optimization history,

• $$K$$: the iteration of the optimization history at which to approximate the Hessian matrix,

• $$x_k$$: the input value at iteration $$k$$,

• $$\Delta x_k=x_{k+1}-x_k$$: the variation of $$x$$ from iteration $$k$$ to iteration $$k+1$$,

• $$y_k$$: the output value at iteration $$k$$,

• $$\Delta y_k=y_{k+1}-y_k$$: the variation of the function output from iteration $$k$$ to iteration $$k+1$$,

• $$g_k$$: the gradient of $$f$$ at $$x_k$$,

• $$\Delta g_k=g_{k+1}-g_k$$: the variation of the gradient from iteration $$k$$ to iteration $$k+1$$,

• $$B_k$$: the approximation of the Hessian of $$f$$ at $$x_k$$,

• $$H_k$$: the inverse of $$B_k$$.

class gemseo.post.core.hessians.BFGSApprox(history)[source]

Hessian matrix approximation with the BFGS algorithm.

Parameters:

history (Database) – The optimization history containing input values, output values and Jacobian values.

Compute the variations of the input variables and gradient.

The variations from the iteration $$k$$ are defined by:

• $$\Delta x_k = x_{k+1}-x_k$$ for the input variables,

• $$\Delta g_k = g_{k+1} - g_k$$ for the gradient.

Parameters:
• x_hist (ndarray) – The history of the input variables.

Returns:

• The difference between the input variables at iteration iteration and the input variables at iteration iteration+1.

• The difference between the gradient at iteration iteration and the gradient at iteration iteration+1.

Return type:

Generator[tuple[ndarray, ndarray]]

b_mat_history: list[ndarray]

The history $$B_0,B_1,\ldots,B_K$$ of the approximations of the Hessian matrix $$B$$.

f_ref: ndarray | None

The value $$y_K$$ of the output of $$f$$ at $$x_K$$.

The value $$g_K$$ of the gradient function $$g$$ of $$f$$ at $$x_K$$.

h_mat_history: list[ndarray]

The history $$H_0,H_1,\ldots,H_K$$ of the approximations of the inverse Hessian matrix $$H$$.

history: Database

The optimization history containing input values, output values and Jacobian values.

x_ref: ndarray | None

The value $$x_K$$ of the input variables $$x$$ at the iteration $$K$$ of the optimization history; this is the point at which the Hessian matrix and its inverse are approximated.

class gemseo.post.core.hessians.HessianApproximation(history)[source]

Bases: object

Approximation of the Hessian matrix from an optimization history.

Parameters:

history (Database) – The optimization history containing input values, output values and Jacobian values.

build_approximation(funcname, save_diag=False, first_iter=0, last_iter=-1, b_mat0=None, at_most_niter=None, return_x_grad=False, func_index=None, save_matrix=False, scaling=False, normalize_design_space=False, design_space=None)[source]

Compute $$B$$, the approximation of the Hessian matrix.

Parameters:
• funcname (str) – The name of the function for which to approximate the Hessian matrix.

• save_diag (bool) –

Whether to return the approximations of the Hessian’s diagonal.

By default it is set to False.

• first_iter (int) –

The first iteration of the history to be considered.

By default it is set to 0.

• last_iter (int | None) –

The last iteration of the history to be considered. If None, consider all the iterations.

By default it is set to -1.

• b_mat0 (ndarray | None) – The initial approximation of the Hessian matrix.

• at_most_niter (int | None) – The maximum number of iterations to be considered. If None, consider all the iterations.

Whether to return the input variables and gradient at the last iteration.

By default it is set to False.

• func_index (int | None) – The index of the output of interest to be defined if the function has a multidimensional output. If None and if the output is multidimensional, an error is raised.

• save_matrix (bool) –

Whether to store the approximations of the Hessian in HessianApproximation.b_mat_history.

By default it is set to False.

• scaling (bool) –

do scaling step

By default it is set to False.

• normalize_design_space (bool) –

Whether to scale the input values between 0 and 1 to work in a normalized input space.

By default it is set to False.

• design_space (DesignSpace | None) – The input space used to scale the input values if normalize_design_space is True.

Returns:

• $$B$$, the approximation of the Hessian matrix.

• The diagonal of $$B$$.

• The history of the input variables if return_x_grad is True.

• The history of the gradient if return_x_grad is True.

Return type:

tuple[ndarray, ndarray, ndarray | None, ndarray | None]

build_inverse_approximation(funcname, save_diag=False, first_iter=0, last_iter=-1, h_mat0=None, at_most_niter=None, return_x_grad=False, func_index=None, save_matrix=False, factorize=False, scaling=False, angle_tol=1e-05, step_tol=10000000000.0, normalize_design_space=False, design_space=None)[source]

Compute $$H$$, the approximation of the inverse of the Hessian matrix.

Parameters:
• funcname (str) – The name of the function for which to approximate the inverse of the Hessian matrix.

• save_diag (int) –

Whether to return the list of diagonal approximations.

By default it is set to False.

• first_iter (int) –

The first iteration of the history to be considered.

By default it is set to 0.

• last_iter (int | None) –

The last iteration of the history to be considered. If None, consider all the iterations.

By default it is set to -1.

• h_mat0 (ndarray | None) – The initial approximation of the inverse of the Hessian matrix. If None, use $$H_0=\frac{\Delta g_k^T\Delta x_k} {\Delta g_k^T\Delta g_k}I_d$$.

• at_most_niter (int | None) – The maximum number of iterations to be considered. If None, consider all the iterations.

Whether to return the input variables and gradient at the last iteration.

By default it is set to False.

• func_index (int | None) – The output index of the function to be provided if the function output is a vector.

• save_matrix (bool) –

Whether to store the approximations of the inverse Hessian in HessianApproximation.h_mat_history.

By default it is set to False.

• factorize (bool) –

Whether to factorize the approximations of the Hessian matrix and its inverse, as $$A=A_{1/2}A_{1/2}^T$$ for a matrix $$A$$.

By default it is set to False.

• scaling (bool) –

do scaling step

By default it is set to False.

• angle_tol (float) –

The significativity level for $$\Delta g_k^T\Delta x_k$$.

By default it is set to 1e-05.

• step_tol (float) –

The significativity level for $$\|\Delta g_k\|_{\infty}$$.

By default it is set to 10000000000.0.

• normalize_design_space (bool) –

Whether to scale the input values between 0 and 1 to work in a normalized input space.

By default it is set to False.

• design_space (DesignSpace | None) – The input space used to scale the input values if normalize_design_space is True.

Returns:

• $$H$$, the approximation of the inverse of the Hessian matrix.

• The diagonal of $$H$$.

• The history of the input variables if return_x_grad is True.

• The history of the gradient if return_x_grad is True.

• The matrix $$H_{1/2}$$ such that $$H=H_{1/2}H_{1/2}^T$$ if factorize is True.

• $$B$$, the approximation of the Hessian matrix.

• A matrix $$B_{1/2}$$ such that $$B=B_{1/2}B_{1/2}^T$$ if factorize is True.

Raises:

LinAlgError – When either the inversion of $$H$$ fails or the Cholesky decomposition of $$H$$ or $$B$$ fails.

Return type:

tuple[ndarray, ndarray, ndarray | None, ndarray | None]

Compute the successive variations of both input variables and gradient.

These variations are called corrections.

Parameters:
• x_hist (ndarray) – The history of the input variables.

Returns:

• The successive variations of the input variables.

• The successive variations of the gradient.

Return type:
static compute_scaling(hessk, hessk_dsk, dskt_hessk_dsk, dyk, dyt_dsk)[source]

Compute the scaling coefficients $$c_1$$ and $$c_2$$.

• $$c_1=\frac{d-1}{\mathrm{Tr}(B_k)-\frac{\|B_k\Delta x_k\|_2^2} {\Delta x_k^T B_k\Delta x_k}}$$,

• $$c_2=\frac{\Delta g_k^T\Delta x_k}{\|\Delta g_k\|_2^2}$$.

Parameters:
• hessk (ndarray) – The approximation $$B_k$$ of the Hessian matrix at iteration $$k$$.

• hessk_dsk (ndarray) – The product $$B_k\Delta x_k$$.

• dskt_hessk_dsk (ndarray) – The product $$\Delta x_k^T B_k\Delta x_k$$.

• dyk (ndarray) – The variation of the gradient $$\Delta g_k$$.

• dyt_dsk (ndarray) – The product $$\Delta g_k^T\Delta x_k$$.

Returns:

TODO * coeff2: TODO

Return type:

• coeff1

Compute the variation of the input variables and gradient from an iteration.

The variations from the iteration $$k$$ are defined by:

• $$\Delta x_k = x_{k+1}-x_k$$ for the input variables,

• $$\Delta g_k = g_{k+1} - g_k$$ for the gradient.

Parameters:
• x_hist (ndarray) – The history of the input variables.

• iteration (int) – The optimization iteration at which to compute the variations.

Returns:

• The difference between the input variables at iteration iteration+1 and the input variables at iteration iteration.

• The difference between the gradient at iteration iteration+1 and the gradient at iteration iteration.

Raises:

ValueError – When the iteration is not stored in the database.

Return type:
get_x_grad_history(funcname, first_iter=0, last_iter=None, at_most_niter=None, func_index=None, normalize_design_space=False, design_space=None)[source]

Return the histories of the inputs and gradient.

Parameters:
• funcname (str) – The name of the function for which to get the gradient.

• first_iter (int) –

The first iteration of the history to be considered.

By default it is set to 0.

• last_iter (int | None) – The last iteration of the history to be considered. If None, consider all the iterations.

• at_most_niter (int | None) – The maximum number of iterations to be considered. If None, consider all the iterations.

• func_index (int | None) – The index of the output of interest to be defined if the function has a multidimensional output. If None and if the output is multidimensional, an error is raised.

• normalize_design_space (bool) –

Whether to scale the input values between 0 and 1 to work in a normalized input space.

By default it is set to False.

• design_space (DesignSpace | None) – The input space used to scale the input values if normalize_design_space is True.

Returns:

• The history of the input variables.

• The history of the gradient.

• The length of the history.

• The dimension of the input space.

Raises:

ValueError – When either the gradient history contains a single element, func_index is None while the function output is a vector, func_index is not an output index, the shape of the history of the input variables is not consistent with the shape of the history of the gradient or the optimization history size is insufficient.

Return type:

tuple[ndarray, ndarray, int, int]

static iterate_approximation(hessk, dsk, dyk, scaling=False)[source]

Update $$B$$ from iteration $$k$$ to iteration $$k+1$$.

Based on an iteration of the BFGS algorithm:

$$B_{k+1} = B_k - c_1\frac{B_k\Delta x_k\Delta x_k^TB_k}{\Delta x_k^TB_k\Delta x_k} + c_2\frac{\Delta g_k\Delta g_k^T}{\Delta g_k^T\Delta x_k}$$

where $$c_1=c_2=1$$ if scaling is False, otherwise:

• $$c_1=\frac{d-1}{\mathrm{Tr}(B_k)-\frac{\|B_k\Delta x_k\|_2^2} {\Delta x_k^T B_k\Delta x_k}}$$,

• $$c_2=\frac{\Delta g_k^T\Delta x_k}{\|\Delta g_k\|_2^2}$$.

Note

hessk represents $$B_k$$ initially before to be overwritten by $$B_{k+1}$$ when passed to this method.

BFGS algorithm.

Parameters:
• hessk (ndarray) – The approximation $$B_k$$ of the Hessian matrix at iteration $$k$$.

• dsk (ndarray) – The variation $$\Delta x_k$$ of the input variables.

• dyk (ndarray) – The variation $$\Delta g_k$$ of the gradient.

• scaling (bool) –

Whether to use a scaling stage.

By default it is set to False.

Return type:

None

static iterate_inverse_approximation(h_mat, s_k, y_k, h_factor=None, b_mat=None, b_factor=None, factorize=False, scaling=False)[source]

Update $$H$$ and $$B$$ from step $$k$$ to step $$k+1$$.

Use an iteration of the BFGS algorithm:

$$B_{k+1} = B_k - c_1\frac{B_k\Delta x_k\Delta x_k^TB_k}{\Delta x_k^TB_k\Delta x_k} + c_2\frac{\Delta g_k\Delta g_k^T}{\Delta g_k^T\Delta x_k}$$

and

$$H_{k+1}=c_1^{-1}\Pi_{k+1}H_k\Pi_{k+1}^T +c_2^{-1}\frac{\Delta x_k\Delta x_k^T}{\Delta g_k^T\Delta x_k}$$

where:

$$\Pi_{k+1}=I_d-\frac{\Delta x_k\Delta g_k^T} {\Delta g_k^T\Delta x_k}$$

and where $$c_1=c_2=1$$ if scaling is False, otherwise:

• $$c_1=\frac{d-1}{\mathrm{Tr}(B_k)-\frac{\|B_k\Delta x_k\|_2^2} {\Delta x_k^T B_k\Delta x_k}}$$,

• $$c_2=\frac{\Delta g_k^T\Delta x_k}{\|\Delta g_k\|_2^2}$$.

Note

h_mat and b_mat represent $$H_k$$ and $$B_k$$ initially before to be overwritten by $$H_{k+1}$$ and $$B_{k+1}$$ when passed to this method.

BFGS algorithm.

Parameters:
• h_mat (ndarray) – The approximation $$H_k$$ of the inverse of the Hessian matrix at iteration $$k$$.

• s_k (ndarray) – The variation $$\Delta x_k$$ of the input variables.

• y_k (ndarray) – The variation $$\Delta g_k$$ of the gradient.

• h_factor (ndarray | None) – The square root of the $$H_k$$ at iteration $$k$$.

• b_mat (ndarray | None) – The approximation $$B_k$$ of the Hessian matrix at iteration $$k$$ if factorize is True.

• b_factor (ndarray | None) – The square root of the $$B_k$$ at iteration $$k$$ if factorize is True.

• factorize (bool) –

Whether to update the approximations of the Hessian matrix and its inverse, as $$A=A_{1/2}A_{1/2}^T$$ for a matrix $$A$$.

By default it is set to False.

• scaling (bool) –

do scaling step

By default it is set to False.

Return type:

None

Compute the variations of the input variables and gradient.

The variations from the iteration $$k$$ are defined by:

• $$\Delta x_k = x_{k+1}-x_k$$ for the input variables,

• $$\Delta g_k = g_{k+1} - g_k$$ for the gradient.

Parameters:
• x_hist (ndarray) – The history of the input variables.

Returns:

• The difference between the input variables at iteration iteration and the input variables at iteration iteration+1.

• The difference between the gradient at iteration iteration and the gradient at iteration iteration+1.

Return type:

Generator[tuple[ndarray, ndarray]]

Compute the history from the corrections of input variables and gradient.

A correction is the variation of a quantity between two successive iterations.

Parameters:
• x_corr (ndarray) – The corrections of the input variables.

• x_0 (ndarray) – The initial values of the input variables.

• g_0 (ndarray) – The initial value of the gradient.

Returns:

• The history of the input variables.

• The history of the gradient.

Return type:
b_mat_history: list[ndarray]

The history $$B_0,B_1,\ldots,B_K$$ of the approximations of the Hessian matrix $$B$$.

f_ref: ndarray | None

The value $$y_K$$ of the output of $$f$$ at $$x_K$$.

The value $$g_K$$ of the gradient function $$g$$ of $$f$$ at $$x_K$$.

h_mat_history: list[ndarray]

The history $$H_0,H_1,\ldots,H_K$$ of the approximations of the inverse Hessian matrix $$H$$.

history: Database

The optimization history containing input values, output values and Jacobian values.

x_ref: ndarray | None

The value $$x_K$$ of the input variables $$x$$ at the iteration $$K$$ of the optimization history; this is the point at which the Hessian matrix and its inverse are approximated.

class gemseo.post.core.hessians.LSTSQApprox(history)[source]

Least squares approximation of a Hessian matrix from an optimization history.

Parameters:

history (Database) – The optimization history containing input values, output values and Jacobian values.

build_approximation(funcname, save_diag=False, first_iter=0, last_iter=-1, b_mat0=None, at_most_niter=None, return_x_grad=False, scaling=False, func_index=-1, normalize_design_space=False, design_space=None)[source]

Compute $$B$$, the approximation of the Hessian matrix.

Parameters:
• funcname (str) – The name of the function for which to approximate the Hessian matrix.

• save_diag (bool) –

Whether to return the approximations of the Hessian’s diagonal.

By default it is set to False.

• first_iter (int) –

The first iteration of the history to be considered.

By default it is set to 0.

• last_iter (int | None) –

The last iteration of the history to be considered. If None, consider all the iterations.

By default it is set to -1.

• b_mat0 (ndarray | None) – The initial approximation of the Hessian matrix.

• at_most_niter (int | None) – The maximum number of iterations to be considered. If None, consider all the iterations.

Whether to return the input variables and gradient at the last iteration.

By default it is set to False.

• scaling (bool) –

do scaling step

By default it is set to False.

• func_index (int) –

The index of the output of interest to be defined if the function has a multidimensional output. If None and if the output is multidimensional, an error is raised.

By default it is set to -1.

• normalize_design_space (bool) –

Whether to scale the input values between 0 and 1 to work in a normalized input space.

By default it is set to False.

• design_space (DesignSpace | None) – The input space used to scale the input values if normalize_design_space is True.

Returns:

• $$B$$, the approximation of the Hessian matrix.

• The diagonal of $$B$$.

• The history of the input variables if return_x_grad is True.

• The history of the gradient if return_x_grad is True.

Return type:

tuple[ndarray, ndarray, ndarray | None, ndarray | None]

b_mat_history: list[ndarray]

The history $$B_0,B_1,\ldots,B_K$$ of the approximations of the Hessian matrix $$B$$.

f_ref: ndarray | None

The value $$y_K$$ of the output of $$f$$ at $$x_K$$.

The value $$g_K$$ of the gradient function $$g$$ of $$f$$ at $$x_K$$.

h_mat_history: list[ndarray]

The history $$H_0,H_1,\ldots,H_K$$ of the approximations of the inverse Hessian matrix $$H$$.

history: Database

The optimization history containing input values, output values and Jacobian values.

x_ref: ndarray | None

The value $$x_K$$ of the input variables $$x$$ at the iteration $$K$$ of the optimization history; this is the point at which the Hessian matrix and its inverse are approximated.

class gemseo.post.core.hessians.SR1Approx(history)[source]

Hessian matrix approximation with the Symmetric Rank One (SR1) algorithm.

The approximation at iteration $$k+1$$ is:

$B_{k+1}=B_k + \frac{(\Delta g_k-B_k\Delta x_k)(\Delta g_k-B_k\Delta x_k)^T} {(\Delta g_k-B_k\Delta x_k)^T\Delta x_k}$

This update from iteration $$k$$ to iteration $$k+1$$ is applied only if $$|(\Delta g_k-B_k\Delta x_k)^T\Delta x_k| \geq \varepsilon\|\Delta x_k\|\|\Delta g_k\|$$ where $$\varepsilon$$ is a small number, e.g. $$10^{-8}$$.

SR1 algorithm.

Parameters:

history (Database) – The optimization history containing input values, output values and Jacobian values.

static iterate_approximation(b_mat, s_k, y_k, scaling=False)[source]

Update $$B$$ from iteration $$k$$ to iteration $$k+1$$.

Based on an iteration of the BFGS algorithm:

$$B_{k+1} = B_k - c_1\frac{B_k\Delta x_k\Delta x_k^TB_k}{\Delta x_k^TB_k\Delta x_k} + c_2\frac{\Delta g_k\Delta g_k^T}{\Delta g_k^T\Delta x_k}$$

where $$c_1=c_2=1$$ if scaling is False, otherwise:

• $$c_1=\frac{d-1}{\mathrm{Tr}(B_k)-\frac{\|B_k\Delta x_k\|_2^2} {\Delta x_k^T B_k\Delta x_k}}$$,

• $$c_2=\frac{\Delta g_k^T\Delta x_k}{\|\Delta g_k\|_2^2}$$.

Note

hessk represents $$B_k$$ initially before to be overwritten by $$B_{k+1}$$ when passed to this method.

BFGS algorithm.

Parameters:
• hessk – The approximation $$B_k$$ of the Hessian matrix at iteration $$k$$.

• dsk – The variation $$\Delta x_k$$ of the input variables.

• dyk – The variation $$\Delta g_k$$ of the gradient.

• scaling (bool) –

Whether to use a scaling stage.

By default it is set to False.

• b_mat (ndarray) –

• s_k (ndarray) –

• y_k (ndarray) –

Return type:

None

EPSILON = 1e-08
b_mat_history: list[ndarray]

The history $$B_0,B_1,\ldots,B_K$$ of the approximations of the Hessian matrix $$B$$.

f_ref: ndarray | None

The value $$y_K$$ of the output of $$f$$ at $$x_K$$.

The value $$g_K$$ of the gradient function $$g$$ of $$f$$ at $$x_K$$.

h_mat_history: list[ndarray]

The history $$H_0,H_1,\ldots,H_K$$ of the approximations of the inverse Hessian matrix $$H$$.

history: Database

The optimization history containing input values, output values and Jacobian values.

x_ref: ndarray | None

The value $$x_K$$ of the input variables $$x$$ at the iteration $$K$$ of the optimization history; this is the point at which the Hessian matrix and its inverse are approximated.