Hi all,
I am new to Quantlib and trying to use LevenbergMarquardt to optimize a toy problem.
I’ve derived a function class from CostFunction:
// =====================================================
class ObjFun : public CostFunction
{
public:
virtual Real value(const Array& x) const;
virtual Disposable<Array> values(const Array& x) const;
};
Real ObjFun::value(const Array& x) const
{
Real fval = exp(x[0]) * (4 * x[0] * x[0] + 2 * x[1] * x[1] + 4 * x[0] * x[1] + 2 * x[1] + 1);
return fval;
}
Disposable<Array> ObjFun::values(const Array& x) const
{
Array arr = x;
Disposable<Array> dispArr(arr);
return dispArr;
}
//---------------------------------------------------------
And then I wrote the following codes:
// ================================================
void testLMA()
{
NoConstraint noCon;
LevenbergMarquardt lm(1e-8);
EndCriteria endCrit(1000,400,1e-8,1e-8,1e-8);
ObjFun myfun;
Array init(2);
init[0] = 0.5;
init[1] = -1;
Real testVal = myfun.value(init);
Array grad(2);
myfun.gradient(grad,init);
Real g2 = grad[1];
Problem problem1(myfun,noCon,init);
EndCriteria::Type endType = lm.minimize(problem1,endCrit);
Real x0 = problem1.currentValue()[0];
testVal = problem1.functionValue();
}
//------------------------------------------------------------------------
Despite all possible initial guess I’ve input, the method just converges to the same non-optimized point every time, namely (0,0) in this case.
Can anyone help me out of this problem? Thanks in advance.
Ben
******************************************************************
This communication is intended solely for the addressee and is confidential. If you
are not the intended recipient, any disclosure, copying, distribution or any action taken
or omitted to be taken in reliance on it, is prohibited and may be unlawful. Unless
indicated to the contrary: it does not constitute professional advice or opinions upon
which reliance may be made by the addressee or any other party, and it should be
considered to be a work in progress.
******************************************************************
| Free forum by Nabble | Edit this page |