32 bool useCostFunctionsJacobian)
33 : epsfcn_(epsfcn), xtol_(xtol), gtol_(gtol),
34 useCostFunctionsJacobian_(useCostFunctionsJacobian) {}
49 std::unique_ptr<Real[]> fvec(
new Real[m]);
50 std::unique_ptr<Real[]> diag(
new Real[
n]);
60 std::unique_ptr<Real[]> fjac(
new Real[m*
n]);
62 std::unique_ptr<int[]> ipvt(
new int[
n]);
63 std::unique_ptr<Real[]> qtf(
new Real[
n]);
64 std::unique_ptr<Real[]> wa1(
new Real[
n]);
65 std::unique_ptr<Real[]> wa2(
new Real[
n]);
66 std::unique_ptr<Real[]> wa3(
new Real[
n]);
67 std::unique_ptr<Real[]> wa4(
new Real[m]);
71 "less functions (" << m <<
72 ") than available variables (" <<
n <<
")");
74 "negative f tolerance");
77 QL_REQUIRE(maxfev > 0,
"null number of evaluations");
82 [
this](
const auto m,
const auto n,
const auto x,
const auto fvec,
const auto iflag) {
83 this->
fcn(m,
n, x, fvec);
87 ? [
this](
const auto m,
const auto n,
const auto x,
const auto fjac,
const auto iflag) {
97 diag.get(), mode, factor,
98 nprint, &info, &nfev, fjac.get(),
99 ldfjac, ipvt.get(), qtf.get(),
100 wa1.get(), wa2.get(), wa3.get(), wa4.get(),
106 QL_REQUIRE(info != 0,
"MINPACK: improper input parameters");
107 QL_REQUIRE(info != 7,
"MINPACK: xtol is too small. no further "
108 "improvement in the approximate "
109 "solution x is possible.");
110 QL_REQUIRE(info != 8,
"MINPACK: gtol is too small. fvec is "
111 "orthogonal to the columns of the "
112 "jacobian to machine precision.");
131 QL_FAIL(
"unknown MINPACK result: " << info);
142 std::copy(x, x+
n, xt.
begin());
147 std::copy(tmp.
begin(), tmp.
end(), fvec);
155 std::copy(x, x+
n, xt.
begin());
162 std::copy(tmpT.
begin(), tmpT.
end(), fjac);
165 std::copy(tmpT.
begin(), tmpT.
end(), fjac);
1-D array used in linear algebra.
const_iterator end() const
Size size() const
dimension of the array
const_iterator begin() const
bool test(const Array &p) const
virtual Array values(const Array &x) const =0
method to overload to compute the cost function values in x
virtual Real value(const Array &x) const
method to overload to compute the cost function value in x
virtual void jacobian(Matrix &jac, const Array &x) const
method to overload to compute J_f, the jacobian of
Criteria to end optimization process:
@ StationaryFunctionValue
@ FunctionEpsilonTooSmall
Real functionEpsilon() const
Size maxIterations() const
const bool useCostFunctionsJacobian_
void fcn(int m, int n, Real *x, Real *fvec, int *)
LevenbergMarquardt(Real epsfcn=1.0e-8, Real xtol=1.0e-8, Real gtol=1.0e-8, bool useCostFunctionsJacobian=false)
Problem * currentProblem_
void jacFcn(int m, int n, Real *x, Real *fjac, int *)
EndCriteria::Type minimize(Problem &P, const EndCriteria &endCriteria) override
minimize the optimization problem P
Matrix used in linear algebra.
const_iterator begin() const
const_iterator end() const
Constrained optimization problem.
const Array & currentValue() const
current value of the local minimum
void setCurrentValue(Array currentValue)
Constraint & constraint() const
Constraint.
Array values(const Array &x)
call cost values computation and increment evaluation counter
void setFunctionValue(Real functionValue)
CostFunction & costFunction() const
Cost function.
Abstract constraint class.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Maps function, bind and cref to either the boost or std implementation.
Levenberg-Marquardt optimization method.
wrapper for MINPACK minimization routine
void lmdif(int m, int n, Real *x, Real *fvec, Real ftol, Real xtol, Real gtol, int maxfev, Real epsfcn, Real *diag, int mode, Real factor, int nprint, int *info, int *nfev, Real *fjac, int ldfjac, int *ipvt, Real *qtf, Real *wa1, Real *wa2, Real *wa3, Real *wa4, const QuantLib::MINPACK::LmdifCostFunction &fcn, const QuantLib::MINPACK::LmdifCostFunction &jacFcn)
std::function< void(int, int, Real *, Real *, int *)> LmdifCostFunction
Matrix transpose(const Matrix &m)