Yes, there are a few logical flaws in your BFGS implementation. Let's walk through them:
-
Incorrect Assignment for stop_condition:
In the section where you set the stop_condition, there's a mistake with the assignment operator. Specifically, in the elif block:
elif stop == "max_iter":
stop_condition == k < max_iter
Here, == is used instead of =. The == operator checks for equality, whereas = is used for assignment. As a result, stop_condition is not being updated correctly for the "max_iter" stopping criterion. It should be:
elif stop == "max_iter":
stop_condition = k < max_iter
-
Static stop_condition:
The stop_condition is evaluated only once before entering the while loop. This is problematic because:
-
Time-Based Stopping: If stop is set to "time", the condition time.time() - start_time < time_limit needs to be re-evaluated in each iteration to check if the time limit has been exceeded.
-
Iteration-Based Stopping: Similarly, if stop is set to "max_iter", k < max_iter should be checked in every iteration to decide whether to continue.
Currently, the stop_condition isn't updated within the loop, meaning the loop might not terminate as expected based on the chosen stopping criterion.
-
Potential Division by Zero:
While not a direct logical flaw, it's worth noting that in the BFGS update:
if p_q != 0:
H_k = (H_k + np.outer(p_k, p_k) / p_q
- np.outer(H_q, H_q) / (q_k @ H_q))
You're checking for p_q != 0, but in practice, due to floating-point precision, it's safer to check if abs(p_q) > some_small_threshold to avoid numerical instability.
-
Redundant Function Call:
At the end of the function, you have:
return x_k, k, total_armijo, f(x_k), f_list, error_ratios, time.time() - start_time
You already computed f_current = f(x_k) inside the loop. It would be more efficient to return f_current instead of calling f(x_k) again.
-
Initialization of f_prev and error_ratios:
The calculation of error_ratio skips the first iteration by checking if k > 0, which is correct. However, ensure that f_prev is properly initialized before the loop to avoid potential issues.
Corrected Version
Here's a revised version addressing the identified issues:
def BFGS(f, grad_f, x0, tol=1e-6, max_iter=100, time_limit=1, stop="time", H0=None):
import numpy as np
import time
k = 0
n = len(x0)
x_k = x0
f_list = []
f_prev = f(x0)
error_ratios = []
H_k = np.eye(n) if H0 is None else H0
start_time = time.time()
total_armijo = 0
grad_k = grad_f(x0)
while True:
# Update stop_condition based on chosen stopping criterion
if stop == "time":
stop_condition = (time.time() - start_time) < time_limit
elif stop == "max_iter":
stop_condition = k < max_iter
else:
raise ValueError("Unsupported stopping criterion")
# Check stopping conditions
if not stop_condition or np.linalg.norm(grad_k) <= tol:
break
# Compute search direction
d_k = -H_k @ grad_k
# Perform line search
t_k, armijo_iter = armijo_line_search(f, grad_f, x_k, d_k)
total_armijo += armijo_iter
# Update position
x_next = x_k + t_k * d_k
p_k = x_next - x_k
grad_next = grad_f(x_next)
q_k = grad_next - grad_k
p_q = np.dot(p_k, q_k)
H_q = H_k @ q_k
if abs(p_q) > 1e-10: # Use a small threshold instead of exact zero
H_k = (H_k + np.outer(p_k, p_k) / p_q
- np.outer(H_q, H_q) / np.dot(q_k, H_q))
x_k = x_next
grad_k = grad_next
k += 1
f_current = f(x_k)
f_list.append(f_current)
if k > 0:
error_ratio = abs(f_current - f_prev) / abs(f_prev) if abs(f_prev) > 0 else np.nan
error_ratios.append(error_ratio)
f_prev = f_current
total_time = time.time() - start_time
return x_k, k, total_armijo, f_current, f_list, error_ratios, total_time
Summary of Fixes
- Assignment Correction: Changed
stop_condition == k < max_iter to stop_condition = k < max_iter.
- Dynamic Stop Condition: Moved the evaluation of
stop_condition inside the loop to ensure it's checked in every iteration.
- Threshold for
p_q: Introduced a small threshold to avoid numerical issues.
- Avoid Redundant Function Call: Returned
f_current instead of calling f(x_k) again.
- Error Handling: Added an else clause to handle unsupported stopping criteria.
By addressing these issues, the BFGS implementation should function more reliably and as intended.