Conjugate Gradient Method
Classical
Conjugate Gradient Method (CG) optimization algorithm.
Algorithm Overview
This module implements the Conjugate Gradient optimization algorithm. The Conjugate Gradient method is an algorithm for the numerical solution of systems of linear equations whose matrix is positive-definite. For general optimization, it's used as an iterative method for solving unconstrained optimization problems.
The method works by:
- Computing the gradient at the current point
- Determining a conjugate direction (orthogonal in a specific sense)
- Performing a line search along this direction
- Updating the position and computing a new conjugate direction
The conjugate gradient method has the property that it converges in at most n steps for a quadratic function in n dimensions, making it particularly effective for quadratic and near-quadratic problems.
This implementation uses scipy's CG optimizer with multiple random restarts to improve global optimization performance.
Usage
from opt.classical.conjugate_gradient import ConjugateGradient
from opt.benchmark.functions import sphere
optimizer = ConjugateGradient(
func=sphere,
lower_bound=-5.12,
upper_bound=5.12,
dim=10,
max_iter=500,
)
best_solution, best_fitness = optimizer.search()
print(f"Best solution: {best_solution}")
print(f"Best fitness: {best_fitness:.6e}")Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
func | Callable | Required | Objective function to minimize. |
lower_bound | float | Required | Lower bound of search space. |
upper_bound | float | Required | Upper bound of search space. |
dim | int | Required | Problem dimensionality. |
max_iter | int | 1000 | Maximum iterations per restart. |
num_restarts | int | 10 | Number of random restarts for multistart strategy. |
seed | int | None | None | Random seed for reproducibility. |
Algorithm Metadata
| Property | Value |
|---|---|
| Algorithm Name | Conjugate Gradient Method |
| Acronym | CG |
| Year Introduced | 1952 |
| Authors | Hestenes, Magnus; Stiefel, Eduard |
| Algorithm Class | Classical |
| Complexity | O(n²) per iteration |
| Properties | Gradient-based, Deterministic |
| Implementation | Python 3.10+ |
| COCO Compatible | Yes |
Mathematical Formulation
Core update equation:
where:
is the position at iteration is the step size from line search is the conjugate search direction
Direction update:
where
Constraint handling:
- Boundary conditions: Penalty-based (large value for out-of-bounds)
- Feasibility enforcement: Post-optimization clamping to bounds
Hyperparameters
| Parameter | Default | BBOB Recommended | Description |
|---|---|---|---|
| max_iter | 1000 | 10000 | Maximum iterations |
| num_restarts | 10 | 5-20 | Number of random restarts |
Sensitivity Analysis:
num_restarts: Medium impact on global optimization quality- Recommended tuning ranges:
COCO/BBOB Benchmark Settings
Search Space:
- Dimensions tested:
2, 3, 5, 10, 20, 40 - Bounds: Function-specific (typically
[-5, 5]or[-100, 100]) - Instances: 15 per function (BBOB standard)
Evaluation Budget:
- Budget:
function evaluations - Independent runs: 15 (for statistical significance)
- Seeds:
0-14(reproducibility requirement)
Performance Metrics:
- Target precision:
1e-8(BBOB default) - Success rate at precision thresholds:
[1e-8, 1e-6, 1e-4, 1e-2] - Expected Running Time (ERT) tracking
Raises
ValueError: If search space is invalid or function evaluation fails.
Notes
- Modifies self.history if track_history=True
- Uses self.seed for all random number generation
- BBOB: Returns final best solution after max_iter or convergence
Computational Complexity:
- Time per iteration:
for direction updates and line search - Space complexity:
for storing vectors - BBOB budget usage: Typically uses 15-40% of
budget for smooth functions
BBOB Performance Characteristics:
- Best function classes: Unimodal, Smooth, Well-conditioned quadratic
- Weak function classes: Non-smooth, Highly multimodal, Ill-conditioned
- Typical success rate at 1e-8 precision: 60-80% (dim=5, quadratic functions)
- Expected Running Time (ERT): Excellent on quadratic, good on smooth unimodal
Convergence Properties:
- Convergence rate: Superlinear on quadratic functions
- Local vs Global: Tends to find local optima, multistart improves global search
- Premature convergence risk: Medium (depends on
formula used)
Reproducibility:
- Deterministic: Yes (given same seed) - Same seed guarantees same restart points
- BBOB compliance: seed parameter required for 15 independent runs
- Initialization: Uniform random sampling in
[lower_bound, upper_bound] - RNG usage:
numpy.random.default_rng(self.seed)for restart initialization
Implementation Details:
- Parallelization: Not supported (sequential restarts)
- Constraint handling: Penalty-based during optimization, clamping post-optimization
- Numerical stability: Relies on SciPy's numerically stable CG implementation
Known Limitations:
- Requires gradient computation (finite differences if not provided)
- Performance degrades on ill-conditioned problems
- May cycle or stall on non-quadratic functions
- Multistart strategy increases total function evaluations
Version History:
- v0.1.0: Initial implementation with multistart strategy
- v0.1.2: Added COCO/BBOB compliance documentation
References
[1] Hestenes, M. R., & Stiefel, E. (1952). "Methods of conjugate gradients for solving linear systems." Journal of Research of the National Bureau of Standards, 49(6), 409-436. https://doi.org/10.6028/jres.049.044
[2] Fletcher, R., & Reeves, C. M. (1964). "Function minimization by conjugate gradients." The Computer Journal, 7(2), 149-154. https://doi.org/10.1093/comjnl/7.2.149
[3] Polak, E., & Ribière, G. (1969). "Note sur la convergence de méthodes de directions conjuguées." Revue française d'informatique et de recherche opérationnelle, 3(16), 35-43.
[4] Hansen, N., Auger, A., Ros, R., Mersmann, O., Tušar, T., Brockhoff, D. (2021). "COCO: A platform for comparing continuous optimizers in a black-box setting." Optimization Methods and Software, 36(1), 114-144. https://doi.org/10.1080/10556788.2020.1808977
COCO Data Archive:
- Benchmark results: https://coco-platform.org/testsuites/bbob/data-archive.html
- Algorithm data: SciPy implementation widely benchmarked
- Code repository: https://github.com/Anselmoo/useful-optimizer
Implementation:
- Original paper code: Multiple independent implementations
- This implementation: Based on SciPy's CG with multistart for BBOB compliance
See Also
BFGS: Quasi-Newton method with similar convergence properties BBOB Comparison: Faster per iteration for low-medium dimensions
LBFGS: Limited-memory variant suitable for high dimensions BBOB Comparison: Better memory scaling, similar convergence rate
AbstractOptimizer: Base class for all optimizers opt.benchmark.functions: BBOB-compatible test functions
Related BBOB Algorithm Classes:
- Classical: BFGS, NelderMead, Powell
- Gradient: AdamW, SGDMomentum
Benchmark Performance
Results of the benchmark suite for this optimizer, on the same evaluation budget as every other optimizer and compared with a random search baseline. See the methodology for the protocol and statistics.
Related Pages
Source Code
View the implementation: conjugate_gradient.py