### Install stabbb via pip Source: https://github.com/mesquita-daniel/stabbb/blob/master/README.md The installation command for the stabbb package using the Python package manager. ```console pip install stabbb ``` -------------------------------- ### Full Parameter Reference for stab_BB Source: https://context7.com/mesquita-daniel/stabbb/llms.txt This example showcases the `stab_BB` function with all its available parameters, including recommended values from the research paper. It optimizes a simple sphere function and prints the optimal point and its objective value. ```python import numpy as np from stabbb import stab_BB def objective(x): return np.sum(x**2) # Simple sphere function def gradient(x): return 2 * x x0 = np.array([5.0, -3.0, 2.0, -1.0]) bestX, (xHistory, alphaHistory, normGradHistory) = stab_BB( x0, # Initial point (required) objective, # Objective function f(x) -> float (required) gradient, # Gradient function (required) bb=1, # BB method: 1 or 2 (default: 1) deltaUpdateStrategy='adaptative', # 'adaptative' or 'constant' (default: 'adaptative') deltaInput=1e6, # Initial/constant delta (default: 1e6) c=0.2, # Adaptive delta parameter (default: 0.2) # Paper suggests: 0.1-0.3 for quadratic, 0.1-1.0 for non-quadratic maxIt=10000, # Maximum iterations (default: 10000) tol=1e-7, # Convergence tolerance (default: 1e-7) verbose=False # Print progress (default: False) ) # Return values: # bestX: optimal point found (np.ndarray) # xHistory: list of all evaluated points # alphaHistory: list of all step sizes used # normGradHistory: list of gradient norms at each iteration print(f"Optimal: {bestX}") print(f"Objective value: {objective(bestX):.2e}") ``` -------------------------------- ### Optimize functions using stab_BB Source: https://github.com/mesquita-daniel/stabbb/blob/master/README.md A basic usage example demonstrating how to minimize the Rosenbrock function using the stab_BB optimizer. It requires an initial guess, the objective function, and its derivative. ```python from scipy.optimize import rosen, rosen_der from stabbb import stab_BB x0 = [1.3, 0.7, 0.8, 1.9, 1.2] bestX, _ = stab_BB(x0, rosen, rosen_der) print(bestX) ``` -------------------------------- ### Compare BB1 and BB2 Methods Source: https://context7.com/mesquita-daniel/stabbb/llms.txt Shows how to toggle between the BB1 and BB2 stepsize formulas within the stab_BB function to determine which performs better for a specific problem. ```python import numpy as np from scipy.optimize import rosen, rosen_der from stabbb import stab_BB x0 = np.array([1.3, 0.7, 0.8, 1.9, 1.2]) # Optimize using BB1 method bestX_bb1, (_, alpha_bb1, grad_bb1) = stab_BB( x0, rosen, rosen_der, bb=1, tol=1e-7 ) ``` -------------------------------- ### Optimize using BB2 Method and Compare with BB1 Source: https://context7.com/mesquita-daniel/stabbb/llms.txt This snippet demonstrates the usage of the stab_BB function with the BB2 method for optimization. It compares the number of iterations and the final gradient norm achieved by both BB1 and BB2 methods, and prints the optimal points found by each. ```python bestX_bb2, (_, alpha_bb2, grad_bb2) = stab_BB( x0, rosen, rosen_der, bb=2, tol=1e-7 ) print(f"BB1 method: {len(alpha_bb1)} iterations, final grad norm: {grad_bb1[-1]:.2e}") print(f"BB2 method: {len(alpha_bb2)} iterations, final grad norm: {grad_bb2[-1]:.2e}") print(f"BB1 optimal: {bestX_bb1}") print(f"BB2 optimal: {bestX_bb2}") ``` -------------------------------- ### Handle Non-Convex Functions with Stabilized BB Source: https://context7.com/mesquita-daniel/stabbb/llms.txt Demonstrates optimizing a non-convex function (Rastrigin) using the stabilized BB method. It highlights the use of `deltaUpdateStrategy='constant'` and a smaller `deltaInput` for non-convex problems, showing the found minimum and function value. ```python import numpy as np from stabbb import stab_BB # Rastrigin function - a non-convex function with many local minima def rastrigin(x): A = 10 n = len(x) return A * n + sum(xi**2 - A * np.cos(2 * np.pi * xi) for xi in x) def rastrigin_grad(x): A = 10 return np.array([2 * xi + 2 * np.pi * A * np.sin(2 * np.pi * xi) for xi in x]) # Start from a point near the global minimum x0 = np.array([0.5, 0.5]) bestX, (xHistory, alphaHistory, normGradHistory) = stab_BB( x0, rastrigin, rastrigin_grad, deltaUpdateStrategy='constant', deltaInput=0.1, # Smaller delta for non-convex problems maxIt=10000, tol=1e-6 ) print(f"Found minimum at: {bestX}") print(f"Function value: {rastrigin(bestX)}") print(f"Iterations: {len(alphaHistory)}") ``` -------------------------------- ### Optimize with Adaptive Delta and Verbose Output Source: https://context7.com/mesquita-daniel/stabbb/llms.txt Demonstrates the adaptive delta strategy which dynamically adjusts the stabilization parameter. Includes verbose mode to track gradient norm progress during execution. ```python import numpy as np from stabbb import stab_BB def quadratic_cost(x): A = np.array([[4, 1], [1, 3]]) b = np.array([1, -1]) return 0.5 * x @ A @ x + b @ x def quadratic_grad(x): A = np.array([[4, 1], [1, 3]]) b = np.array([1, -1]) return A @ x + b x0 = np.array([10.0, 10.0]) bestX, history = stab_BB( x0, quadratic_cost, quadratic_grad, bb=1, deltaUpdateStrategy='adaptative', deltaInput=1e6, c=0.2, maxIt=5000, tol=1e-8, verbose=True ) xHistory, alphaHistory, normGradHistory = history print(f"\nOptimal point: {bestX}") print(f"Minimum value: {quadratic_cost(bestX)}") print(f"Final gradient norm: {normGradHistory[-1]}") ``` -------------------------------- ### Configure stab_BB with BB2 and Constant Delta Source: https://context7.com/mesquita-daniel/stabbb/llms.txt Configures the optimizer to use the BB2 stepsize formula with a fixed, constant delta parameter. This is useful for maintaining specific bounds on iterate distances throughout the optimization process. ```python import numpy as np from scipy.optimize import rosen, rosen_der from stabbb import stab_BB # Using BB2 method with constant delta strategy x0 = [1.3, 0.7, 0.8, 1.9, 1.2] bestX, (xHistory, alphaHistory, normGradHistory) = stab_BB( x0, rosen, rosen_der, bb=2, deltaUpdateStrategy='constant', deltaInput=2, maxIt=10000, tol=1e-7 ) print(f"Optimal point: {bestX}") print(f"Converged in {len(alphaHistory)} iterations") ``` -------------------------------- ### Perform Optimization with stab_BB Source: https://context7.com/mesquita-daniel/stabbb/llms.txt Demonstrates basic usage of the stab_BB function to minimize the Rosenbrock function. It returns the optimal point and a history tuple containing iterate paths, step sizes, and gradient norms. ```python import numpy as np from scipy.optimize import rosen, rosen_der from stabbb import stab_BB # Basic usage with Rosenbrock function x0 = [1.3, 0.7, 0.8, 1.9, 1.2] bestX, history = stab_BB(x0, rosen, rosen_der) print(f"Optimal point: {bestX}") # Access optimization history xHistory, alphaHistory, normGradHistory = history print(f"Final gradient norm: {normGradHistory[-1]}") print(f"Number of iterations: {len(alphaHistory)}") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.