Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust subsolver module to components interface #448

Merged
merged 5 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/estimagic/optimization/pounders_auxiliary.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,11 @@ def solve_subproblem(
"gtol_abs_conjugate_gradient": gtol_abs_conjugate_gradient,
"gtol_rel_conjugate_gradient": gtol_rel_conjugate_gradient,
}
result = bntr(main_model, lower_bounds, upper_bounds, **options)
result = bntr(main_model, lower_bounds, upper_bounds, x_candidate=x0, **options)
elif solver == "gqtpar":
result = gqtpar(
main_model,
x_candidate=x0,
k_easy=k_easy,
k_hard=k_hard,
maxiter=maxiter,
Expand Down
4 changes: 2 additions & 2 deletions src/estimagic/optimization/subsolvers/bntr.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def bntr(
model,
lower_bounds,
upper_bounds,
x_candidate,
*,
conjugate_gradient_method,
maxiter,
Expand Down Expand Up @@ -61,6 +62,7 @@ def bntr(
for the parameter vector x.
upper_bounds (np.ndarray): 1d array of shape (n,) with upper bounds
for the parameter vector x.
x_candidate (np.ndarray): Initial guess for the solution of the subproblem.
conjugate_gradient_method (str): Method for computing the conjugate gradient
step. Available conjugate gradient methods are:
- "cg"
Expand Down Expand Up @@ -105,8 +107,6 @@ def bntr(
"default_radius": 100.00,
}

x_candidate = np.zeros_like(model.linear_terms)

(
x_candidate,
f_candidate,
Expand Down
10 changes: 8 additions & 2 deletions src/estimagic/optimization/subsolvers/bntr_fast.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def bntr_fast(
model,
lower_bounds,
upper_bounds,
x_candidate,
*,
conjugate_gradient_method,
maxiter,
Expand Down Expand Up @@ -56,6 +57,7 @@ def bntr_fast(
for the parameter vector x.
upper_bounds (np.ndarray): 1d array of shape (n,) with upper bounds
for the parameter vector x.
x_candidate (np.ndarray): Initial guess for the solution of the subproblem.
conjugate_gradient_method (str): Method for computing the conjugate gradient
step. Available conjugate gradient methods are:
- "cg"
Expand Down Expand Up @@ -99,6 +101,7 @@ def bntr_fast(
model_hessian=model_hessian,
lower_bounds=lower_bounds,
upper_bounds=upper_bounds,
x_candidate=x_candidate,
conjugate_gradient_method=conjugate_gradient_method,
maxiter=maxiter,
maxiter_gradient_descent=maxiter_gradient_descent,
Expand Down Expand Up @@ -126,6 +129,7 @@ def _bntr_fast_jitted(
model_hessian,
lower_bounds,
upper_bounds,
x_candidate,
conjugate_gradient_method,
maxiter,
maxiter_gradient_descent,
Expand Down Expand Up @@ -163,6 +167,7 @@ def _bntr_fast_jitted(
for the parameter vector x.
upper_bounds (np.ndarray): 1d array of shape (n,) with upper bounds
for the parameter vector x.
x_candidate (np.ndarray): Initial guess for the solution of the subproblem.
conjugate_gradient_method (str): Method for computing the conjugate gradient
step. Available conjugate gradient methods are:
- "cg"
Expand Down Expand Up @@ -209,6 +214,7 @@ def _bntr_fast_jitted(
model_hessian,
lower_bounds,
upper_bounds,
x_candidate,
maxiter_gradient_descent,
gtol_abs,
gtol_rel,
Expand Down Expand Up @@ -341,6 +347,7 @@ def _take_preliminary_gradient_descent_step_and_check_for_solution(
model_hessian,
lower_bounds,
upper_bounds,
x_candidate,
maxiter_gradient_descent,
gtol_abs,
gtol_rel,
Expand All @@ -357,6 +364,7 @@ def _take_preliminary_gradient_descent_step_and_check_for_solution(
for the parameter vector x.
upper_bounds (np.ndarray): 1d array of shape (n,) with upper bounds
for the parameter vector x.
x_candidate (np.ndarray): Initial guess for the solution of the subproblem.
maxiter_gradient_descent (int): Maximum number of iterations in performing
gradient descent step
gtol_abs (float): Convergence tolerance for the absolute gradient norm.
Expand Down Expand Up @@ -384,8 +392,6 @@ def _take_preliminary_gradient_descent_step_and_check_for_solution(
converged = False
convergence_reason = 0

x_candidate = np.zeros(len(model_gradient))

criterion_candidate = _evaluate_model_criterion(
x_candidate, model_gradient, model_hessian
)
Expand Down
9 changes: 3 additions & 6 deletions src/estimagic/optimization/subsolvers/gqtpar.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class DampingFactors(NamedTuple):
upper_bound: Union[float, None] = None


def gqtpar(model, *, k_easy=0.1, k_hard=0.2, maxiter=200):
def gqtpar(model, x_candidate, *, k_easy=0.1, k_hard=0.2, maxiter=200):
"""Solve the quadratic trust-region subproblem via nearly exact iterative method.

This subproblem solver is mainly based on Conn et al. (2000) "Trust region methods"
Expand Down Expand Up @@ -50,11 +50,10 @@ def gqtpar(model, *, k_easy=0.1, k_hard=0.2, maxiter=200):
See pp. 194-197 in :cite:`Conn2000` for a more detailed description.

Args:
main_model (NamedTuple): NamedTuple containing the parameters of the
main model, i.e.:
model (NamedTuple): NamedTuple containing the parameters of the main model, i.e.
- ``linear_terms``, a np.ndarray of shape (n,) and
- ``square_terms``, a np.ndarray of shape (n,n).
trustregion_radius (float): Trustregion radius, often referred to as delta.
x_candidate (np.ndarray): Initial guess for the solution of the subproblem.
k_easy (float): topping criterion for the "easy" case.
k_hard (float): Stopping criterion for the "hard" case.
maxiter (int): Maximum number of iterations to perform. If reached,
Expand All @@ -69,8 +68,6 @@ def gqtpar(model, *, k_easy=0.1, k_hard=0.2, maxiter=200):
"""
hessian_info = HessianInfo()

x_candidate = np.zeros_like(model.linear_terms)

# Small floating point number signaling that for vectors smaller
# than that backward substituition is not reliable.
# See Golub, G. H., Van Loan, C. F. (2013), "Matrix computations", p.165.
Expand Down
8 changes: 3 additions & 5 deletions src/estimagic/optimization/subsolvers/gqtpar_fast.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from scipy.linalg.lapack import dpotrf as compute_cholesky_factorization


def gqtpar_fast(model, *, k_easy=0.1, k_hard=0.2, maxiter=200):
def gqtpar_fast(model, x_candidate, *, k_easy=0.1, k_hard=0.2, maxiter=200):
"""Solve the quadratic trust-region subproblem via nearly exact iterative method.

This subproblem solver is mainly based on Conn et al. (2000) "Trust region methods"
Expand Down Expand Up @@ -36,11 +36,10 @@ def gqtpar_fast(model, *, k_easy=0.1, k_hard=0.2, maxiter=200):
See pp. 194-197 in :cite:`Conn2000` for a more detailed description.

Args:
main_model (NamedTuple): NamedTuple containing the parameters of the
main model, i.e.:
model (NamedTuple): NamedTuple containing the parameters of the main model, i.e.
- ``linear_terms``, a np.ndarray of shape (n,) and
- ``square_terms``, a np.ndarray of shape (n,n).
trustregion_radius (float): Trustregion radius, often referred to as delta.
x_candidate (np.ndarray): Initial guess for the solution of the subproblem.
k_easy (float): topping criterion for the "easy" case.
k_hard (float): Stopping criterion for the "hard" case.
maxiter (int): Maximum number of iterations to perform. If reached,
Expand All @@ -56,7 +55,6 @@ def gqtpar_fast(model, *, k_easy=0.1, k_hard=0.2, maxiter=200):
hessian_already_factorized = False
model_gradient = model.linear_terms
model_hessian = model.square_terms
x_candidate = np.zeros(len(model_gradient))

# Small floating point number signaling that for vectors smaller
# than that backward substituition is not reliable.
Expand Down
9 changes: 2 additions & 7 deletions src/estimagic/optimization/tranquilo/process_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
get_default_radius_options,
get_default_sample_size,
get_default_search_radius_factor,
get_default_subsolver,
update_option_bundle,
)
from estimagic.optimization.tranquilo.region import Region
Expand Down Expand Up @@ -156,13 +155,9 @@ def process_arguments(
sample_points = get_sampler(sampler, sampler_options)

solve_subproblem = get_subsolver(
solver=get_default_subsolver(
bounds=_bounds,
cube_subsolver=cube_subsolver,
sphere_subsolver=sphere_subsolver,
),
cube_solver=cube_subsolver,
sphere_solver=sphere_subsolver,
user_options=subsolver_options,
bounds=_bounds,
)

filter_points = get_sample_filter(
Expand Down
Loading