Functor wrapper for NLopt with automatic, transparent parameter space mapping (e.g., log-scaling).
More...
#include <optimization.h>
|
| | ObjectiveFunctor (std::function< double(Eigen::Ref< const Eigen::VectorXd >)> fval) |
| |
| | ObjectiveFunctor (std::function< double(Eigen::Ref< const Eigen::VectorXd >, Eigen::Ref< Eigen::VectorXd >)> fval_grad) |
| |
| void | setLogScale (const std::vector< bool > &log_scale) |
| | Set which parameters should be optimized in log-space.
|
| |
| const std::vector< bool > & | getLogScale () const |
| |
| double | operator() (Eigen::Ref< const Eigen::VectorXd > x_opt, Eigen::Ref< Eigen::VectorXd > grad_opt) const |
| |
| bool | usesGradient () const |
| | Checks if the functor utilizes gradient information.
|
| |
| void | addInequalityConstraint (std::function< double(Eigen::Ref< const Eigen::VectorXd >, Eigen::Ref< Eigen::VectorXd >)> g) |
| |
| void | addEqualityConstraint (std::function< double(Eigen::Ref< const Eigen::VectorXd >, Eigen::Ref< Eigen::VectorXd >)> h) |
| |
| std::vector< std::function< double(Eigen::Ref< const Eigen::VectorXd >, Eigen::Ref< Eigen::VectorXd >)> > | getInequalityConstraints () const |
| |
| std::vector< std::function< double(Eigen::Ref< const Eigen::VectorXd >, Eigen::Ref< Eigen::VectorXd >)> > | getEqualityConstraints () const |
| |
|
| static double | NLoptCallback (const std::vector< double > &x, std::vector< double > &grad, void *data) |
| |
| static double | NLoptConstraintWrapper (const std::vector< double > &x, std::vector< double > &grad, void *data) |
| |
|
| Eigen::VectorXd | mapToReal (Eigen::Ref< const Eigen::VectorXd > x_opt) const |
| | Maps parameters from optimization space (potentially log-scaled) to physical space.
|
| |
| void | mapGradientToOpt (Eigen::Ref< const Eigen::VectorXd > x_real, Eigen::Ref< Eigen::VectorXd > grad_real) const |
| | Transforms gradients from real space back to optimization space using the chain rule.
|
| |
| double | evaluateConstraint (size_t index, bool is_ineq, Eigen::Ref< const Eigen::VectorXd > x_opt, Eigen::Ref< Eigen::VectorXd > grad_opt) const |
| | Evaluates a constraint while properly translating inputs and mapping output gradients.
|
| |
|
| std::function< double(Eigen::Ref< const Eigen::VectorXd >)> | fval_only_ |
| | Pointer to a gradient-free objective function.
|
| |
| std::function< double(Eigen::Ref< const Eigen::VectorXd >, Eigen::Ref< Eigen::VectorXd >)> | fval_grad_inplace_ |
| | Pointer to a gradient-based objective function.
|
| |
| std::vector< std::function< double(Eigen::Ref< const Eigen::VectorXd >, Eigen::Ref< Eigen::VectorXd >)> > | ineq_constraints_ |
| | Collection of inequality constraint functions.
|
| |
| std::vector< std::function< double(Eigen::Ref< const Eigen::VectorXd >, Eigen::Ref< Eigen::VectorXd >)> > | eq_constraints_ |
| | Collection of equality constraint functions.
|
| |
| bool | use_gradient_ |
| | Flag indicating whether the objective function uses gradient information.
|
| |
| std::vector< bool > | log_scale_ |
| | Mask indicating which parameter dimensions are optimized in log-scale.
|
| |
Functor wrapper for NLopt with automatic, transparent parameter space mapping (e.g., log-scaling).
Mathematical Formulation When optimization parameters span multiple orders of magnitude, log-scaling maps the optimization coordinate \(y_k\) to the physical parameter \(x_k\):
\[
x_k = \exp(y_k)
\]
By the chain rule, the gradient evaluated by NLopt with respect to \(y_k\) is:
\[
\frac{\partial f}{\partial y_k} = \frac{\partial f}{\partial x_k} \frac{\partial x_k}{\partial y_k} = \frac{\partial f}{\partial x_k} \exp(y_k) = \frac{\partial f}{\partial x_k} x_k
\]
Implementation Algorithm
mapToReal() performs element-wise exponential transformation if log-scaling is enabled for a given parameter index.
mapGradientToOpt() multiplies each computed gradient entry \(\frac{\partial f}{\partial x_k}\) by \(x_k\) if log-scaling is active.
NLoptCallback coordinates the mapping of std::vector to Eigen::Map mapping and triggers evaluations.
◆ ObjectiveFunctor() [1/2]
| cmp::ObjectiveFunctor::ObjectiveFunctor |
( |
std::function< double(Eigen::Ref< const Eigen::VectorXd >)> |
fval | ) |
|
|
inlineexplicit |
◆ ObjectiveFunctor() [2/2]
| cmp::ObjectiveFunctor::ObjectiveFunctor |
( |
std::function< double(Eigen::Ref< const Eigen::VectorXd >, Eigen::Ref< Eigen::VectorXd >)> |
fval_grad | ) |
|
|
inlineexplicit |
◆ addEqualityConstraint()
| void cmp::ObjectiveFunctor::addEqualityConstraint |
( |
std::function< double(Eigen::Ref< const Eigen::VectorXd >, Eigen::Ref< Eigen::VectorXd >)> |
h | ) |
|
|
inline |
◆ addInequalityConstraint()
| void cmp::ObjectiveFunctor::addInequalityConstraint |
( |
std::function< double(Eigen::Ref< const Eigen::VectorXd >, Eigen::Ref< Eigen::VectorXd >)> |
g | ) |
|
|
inline |
◆ evaluateConstraint()
| double cmp::ObjectiveFunctor::evaluateConstraint |
( |
size_t |
index, |
|
|
bool |
is_ineq, |
|
|
Eigen::Ref< const Eigen::VectorXd > |
x_opt, |
|
|
Eigen::Ref< Eigen::VectorXd > |
grad_opt |
|
) |
| const |
|
inlineprivate |
Evaluates a constraint while properly translating inputs and mapping output gradients.
- Parameters
-
| index | Index of the constraint in the vector. |
| is_ineq | True if inequality, false if equality constraint. |
| x_opt | Input vector in optimization space. |
| grad_opt | Output gradient vector in optimization space. |
- Returns
- The evaluated constraint value.
◆ getEqualityConstraints()
| std::vector< std::function< double(Eigen::Ref< const Eigen::VectorXd >, Eigen::Ref< Eigen::VectorXd >)> > cmp::ObjectiveFunctor::getEqualityConstraints |
( |
| ) |
const |
|
inline |
◆ getInequalityConstraints()
| std::vector< std::function< double(Eigen::Ref< const Eigen::VectorXd >, Eigen::Ref< Eigen::VectorXd >)> > cmp::ObjectiveFunctor::getInequalityConstraints |
( |
| ) |
const |
|
inline |
◆ getLogScale()
| const std::vector< bool > & cmp::ObjectiveFunctor::getLogScale |
( |
| ) |
const |
|
inline |
◆ mapGradientToOpt()
| void cmp::ObjectiveFunctor::mapGradientToOpt |
( |
Eigen::Ref< const Eigen::VectorXd > |
x_real, |
|
|
Eigen::Ref< Eigen::VectorXd > |
grad_real |
|
) |
| const |
|
inlineprivate |
Transforms gradients from real space back to optimization space using the chain rule.
Mathematical Formulation By the chain rule, the gradient with respect to the optimization parameter is:
\[
\frac{\partial f}{\partial \theta_{\text{opt}, i}} = \frac{\partial f}{\partial \theta_{\text{real}, i}} \frac{d \theta_{\text{real}, i}}{d \theta_{\text{opt}, i}} = \frac{\partial f}{\partial \theta_{\text{real}, i}} \theta_{\text{real}, i}
\]
- Parameters
-
| x_real | Parameter vector in real space. |
| grad_real | Gradient vector in real space (modified in-place to optimization space gradient). |
◆ mapToReal()
| Eigen::VectorXd cmp::ObjectiveFunctor::mapToReal |
( |
Eigen::Ref< const Eigen::VectorXd > |
x_opt | ) |
const |
|
inlineprivate |
Maps parameters from optimization space (potentially log-scaled) to physical space.
Mathematical Formulation For dimensions flagged as log-scaled:
\[
\theta_{\text{real}, i} = \exp\left( \theta_{\text{opt}, i} \right)
\]
For unscaled dimensions:
\[
\theta_{\text{real}, i} = \theta_{\text{opt}, i}
\]
- Parameters
-
| x_opt | Parameter vector in optimization space. |
- Returns
- Parameter vector in real space.
◆ NLoptCallback()
| static double cmp::ObjectiveFunctor::NLoptCallback |
( |
const std::vector< double > & |
x, |
|
|
std::vector< double > & |
grad, |
|
|
void * |
data |
|
) |
| |
|
inlinestatic |
◆ NLoptConstraintWrapper()
| static double cmp::ObjectiveFunctor::NLoptConstraintWrapper |
( |
const std::vector< double > & |
x, |
|
|
std::vector< double > & |
grad, |
|
|
void * |
data |
|
) |
| |
|
inlinestatic |
◆ operator()()
| double cmp::ObjectiveFunctor::operator() |
( |
Eigen::Ref< const Eigen::VectorXd > |
x_opt, |
|
|
Eigen::Ref< Eigen::VectorXd > |
grad_opt |
|
) |
| const |
|
inline |
◆ setLogScale()
| void cmp::ObjectiveFunctor::setLogScale |
( |
const std::vector< bool > & |
log_scale | ) |
|
|
inline |
Set which parameters should be optimized in log-space.
- Parameters
-
| log_scale | A boolean mask where true means the parameter is log-scaled. |
◆ usesGradient()
| bool cmp::ObjectiveFunctor::usesGradient |
( |
| ) |
const |
|
inline |
Checks if the functor utilizes gradient information.
- Returns
- True if gradients are used, false otherwise.
◆ eq_constraints_
| std::vector<std::function<double(Eigen::Ref<const Eigen::VectorXd>, Eigen::Ref<Eigen::VectorXd>)> > cmp::ObjectiveFunctor::eq_constraints_ |
|
private |
Collection of equality constraint functions.
◆ fval_grad_inplace_
| std::function<double(Eigen::Ref<const Eigen::VectorXd>, Eigen::Ref<Eigen::VectorXd>)> cmp::ObjectiveFunctor::fval_grad_inplace_ |
|
private |
Pointer to a gradient-based objective function.
◆ fval_only_
| std::function<double(Eigen::Ref<const Eigen::VectorXd>)> cmp::ObjectiveFunctor::fval_only_ |
|
private |
Pointer to a gradient-free objective function.
◆ ineq_constraints_
| std::vector<std::function<double(Eigen::Ref<const Eigen::VectorXd>, Eigen::Ref<Eigen::VectorXd>)> > cmp::ObjectiveFunctor::ineq_constraints_ |
|
private |
Collection of inequality constraint functions.
◆ log_scale_
| std::vector<bool> cmp::ObjectiveFunctor::log_scale_ |
|
private |
Mask indicating which parameter dimensions are optimized in log-scale.
◆ use_gradient_
| bool cmp::ObjectiveFunctor::use_gradient_ |
|
private |
Flag indicating whether the objective function uses gradient information.
The documentation for this class was generated from the following file: