From 38c0f38d724c13804ea7b9c75c28ba90a3140e07 Mon Sep 17 00:00:00 2001 From: Yi Zhang Date: Tue, 22 Oct 2019 10:52:04 +0800 Subject: [PATCH] Update sample3.cpp --- src/sample/sample3.cpp | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/sample/sample3.cpp b/src/sample/sample3.cpp index dbf97be..9d964c2 100644 --- a/src/sample/sample3.cpp +++ b/src/sample/sample3.cpp @@ -39,7 +39,7 @@ TEST_FUNC::TEST_FUNC() { m_x = NULL; m_x = lbfgs_malloc(3); - m_x[0] = m_x[1] = m_x[2] = 1.0; + m_x[0] = m_x[1] = m_x[2] = 0.0; } TEST_FUNC::~TEST_FUNC() @@ -55,9 +55,9 @@ lbfgsfloatval_t TEST_FUNC::Func(const lbfgsfloatval_t *x, lbfgsfloatval_t *g, const int n, const lbfgsfloatval_t step) { double f0,f1,f2,temp; - f0 = 3*x[0] + x[1] + 2*x[2]*x[2] - 3; - f1 = -3*x[0] + 5*x[1]*x[1] + 2*x[0]*x[2] - 1; - f2 = 25*x[0]*x[1] + 20*x[2] + 12; + f0 = 3*x[0] + x[1] + 2*x[2]*x[2] - 3.012; //这里添加一点噪声 + f1 = -3*x[0] + 5*x[1]*x[1] + 2*x[0]*x[2] - 1.0521; + f2 = 25*x[0]*x[1] + 20*x[2] + 12.10231; temp = sqrt(f0*f0+f1*f1+f2*f2); g[0] = 0.5*(6*f0+2*f1*(2*x[2]-3)+50*f2*x[1])/temp; @@ -70,19 +70,28 @@ int TEST_FUNC::Progress(const lbfgsfloatval_t *x, const lbfgsfloatval_t *g, cons const lbfgsfloatval_t xnorm, const lbfgsfloatval_t gnorm, const lbfgsfloatval_t step, int n, int k, int ls) { - clog << "iteration times: " << k << endl; + clog << "iteration times: " << k << " fx = " << fx << " gnorm/xnorm = " << gnorm/xnorm << endl; clog << x[0] << " " << x[1] << " " << x[2] << endl; + + if (fx < 1e-10) return 1; //这里我们设置一个方程组的整体目标函数值作为终止条件 因为此方程组在0值处梯度不为0 无法使用梯度条件 return 0; } int TEST_FUNC::Routine() { lbfgsfloatval_t fx; - int ret = lbfgs(3, m_x, &fx, _Func, _Progress, this, NULL); + + lbfgs_parameter_t self_para; + lbfgs_parameter_init(&self_para); + //self_para.min_step = 1e-30; + //self_para.max_linesearch = 40; + //self_para.linesearch = LBFGS_LINESEARCH_BACKTRACKING_WOLFE; + + int ret = lbfgs(3, m_x, &fx, _Func, _Progress, this, &self_para); clog << "L-BFGS optimization terminated with status: " << endl << lbfgs_strerror(ret) << endl; clog << m_x[0] << " " << m_x[1] << " " << m_x[2] << endl; - return 0; + return ret; } int main(int argc, char const *argv[])