pub trait Problem {
// Required methods
fn objective(&self, x: &Array1<f64>) -> Result<f64, EvaluationError>;
fn variable_bounds(&self) -> Array2<f64>;
// Provided methods
fn gradient(&self, _x: &Array1<f64>) -> Result<Array1<f64>, EvaluationError> { ... }
fn hessian(&self, _x: &Array1<f64>) -> Result<Array2<f64>, EvaluationError> { ... }
fn constraints(&self) -> Vec<fn(&[f64], &mut ()) -> f64> { ... }
}Expand description
§Trait for optimization problems
This trait defines the methods that an optimization problem must implement, including the objective function, gradient, hessian and variable bounds.
The objective function is the function to minimize, evaluated at a given point x (Array1<f64>).
The gradient is the derivative of the objective function, evaluated at a given point x (Array1<f64>).
The hessian is the square matrix of the second order partial derivatives of the objective function, evaluated at a given point x (Array1<f64>).
The variable bounds are the lower and upper bounds for the optimization problem.
Constraint functions for constrained optimization problems can also be defined using the constraints method.
The default implementation of the gradient and hessian returns an error indicating the gradient and hessian are not implemented. Some local solvers require the gradient and hessian to be implemented, while for others it isn’t needed. You should check the documentation of the local solver you are using to know if the gradient and hessian are needed.
Required Methods§
Sourcefn objective(&self, x: &Array1<f64>) -> Result<f64, EvaluationError>
fn objective(&self, x: &Array1<f64>) -> Result<f64, EvaluationError>
Objective function to minimize, given at point x (Array1<f64>)
Returns a Result<f64, EvaluationError> of the value of the objective function at x
Sourcefn variable_bounds(&self) -> Array2<f64>
fn variable_bounds(&self) -> Array2<f64>
Variable bounds for the optimization problem
Returns a Result<Array2<f64>> of the variable bounds for the optimization problem.
This bounds are only used in the scatter search phase of the algorithm. The local solver is unconstrained (See argmin issue #137) and therefor can return solutions out of the bounds. You may be able to guide your solutions to your desired bounds/constraints by using a penalty method.
Provided Methods§
Sourcefn gradient(&self, _x: &Array1<f64>) -> Result<Array1<f64>, EvaluationError>
fn gradient(&self, _x: &Array1<f64>) -> Result<Array1<f64>, EvaluationError>
Gradient of the objective function at point x (Array1<f64>)
Returns a Result<Array1<f64>, EvaluationError> of the gradient of the objective function at x
The default implementation returns an error indicating the gradient is not implemented in case it is needed
Sourcefn hessian(&self, _x: &Array1<f64>) -> Result<Array2<f64>, EvaluationError>
fn hessian(&self, _x: &Array1<f64>) -> Result<Array2<f64>, EvaluationError>
Returns the Hessian at point x (Array1<f64>).
Returns a Result<Array2<f64>, EvaluationError> of the hessian of the objective function at x
The default implementation returns an error indicating the hessian is not implemented in case it is needed
Sourcefn constraints(&self) -> Vec<fn(&[f64], &mut ()) -> f64>
fn constraints(&self) -> Vec<fn(&[f64], &mut ()) -> f64>
Constraint functions for constrained optimization
Returns constraint functions in the format expected by optimization solvers. Function pointers that take (&f64, &mut ()) and return f64.
Sign Convention:
- Positive or zero: constraint satisfied
- Negative: constraint violated
The default implementation returns an empty vector (no constraints).
§Examples
use globalsearch::problem::Problem;
use globalsearch::types::EvaluationError;
use ndarray::Array1;
struct MyProblem;
impl Problem for MyProblem {
fn objective(&self, x: &Array1<f64>) -> Result<f64, EvaluationError> {
Ok(x[0].powi(2) + x[1].powi(2))
}
fn variable_bounds(&self) -> ndarray::Array2<f64> {
ndarray::array![[-1.0, 1.0], [-1.0, 1.0]]
}
fn constraints(&self) -> Vec<fn(&[f64], &mut ()) -> f64> {
vec![
|x: &[f64], _: &mut ()| 1.0 - x[0] - x[1], // x[0] + x[1] <= 1.0 -> 1.0 - x[0] - x[1] >= 0
]
}
}Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".