pub trait Optimizer:
Send
+ Sync
+ Display {
// Required method
fn optimize(
&self,
function: &dyn Fn(&Tensor) -> Tensor,
x0: &Tensor,
) -> Result<Tensor>;
}Expand description
Core trait defining the interface for all optimization algorithms.
This trait abstracts over different optimization strategies (Newton, BFGS, CG, Halley),
allowing them to be used interchangeably with implicit ODE solvers. The trait is designed
for thread-safe usage with Send + Sync bounds.
§Function Signature
The optimize method accepts:
function: A closure taking a 1D tensorxand returning a scalar tensor representing the objective value to minimize. The function should use torch operations compatible with autodifferentiation.x0: Initial guess as a 1D tensor, which determines both the problem dimension and computation device (CPU/GPU).
§Return Value
Returns Ok(optimal_x) containing the optimized parameter vector, or Err(e) if optimization
fails due to convergence issues, numerical instability, or invalid inputs.
§Implementation Notes
- The function is evaluated multiple times during optimization
- Gradients/Hessians are computed automatically via torch.autograd
- Tolerance parameters control convergence criteria
Required Methods§
Sourcefn optimize(
&self,
function: &dyn Fn(&Tensor) -> Tensor,
x0: &Tensor,
) -> Result<Tensor>
fn optimize( &self, function: &dyn Fn(&Tensor) -> Tensor, x0: &Tensor, ) -> Result<Tensor>
Minimizes an objective function starting from an initial guess.
§Arguments
function- A closure that takes a 1D tensorxand returns a scalar tensor representing the objective value to minimize. Must support automatic differentiation.x0- Initial guess, 1D tensor accepted byfunction. Determines computation device and floating-point precision.
§Returns
Optimal x that minimizes function, or error if optimization fails.
§Errors
Returns an error if:
- Input validation fails (non-scalar output, wrong rank, non-finite values)
- Hessian allocation fails due to insufficient memory
- Line search fails
- Maximum iterations reached without convergence
§Panics
May panic if libtorch tensor operations fail unexpectedly (should be rare).
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".