78 #define min2(a, b) ((a) <= (b) ? (a) : (b))
79 #define max2(a, b) ((a) >= (b) ? (a) : (b))
80 #define max3(a, b, c) max2(max2((a), (b)), (c));
102 1e-20, 1e20, 1e-4, 0.9, 0.9, 1.0e-16,
205 memcpy(param, &_defparam,
sizeof(*param));
220 int32_t i, j, k, ls, end, bound;
225 const int32_t m = param.
m;
228 float64_t *g = NULL, *gp = NULL, *pg = NULL;
229 float64_t *d = NULL, *w = NULL, *pf = NULL;
252 if (param.
past < 0) {
255 if (param.
delta < 0.) {
264 if (param.
ftol < 0.) {
273 if (param.
gtol < 0.) {
276 if (param.
xtol < 0.) {
324 if (xp == NULL || g == NULL || gp == NULL || d == NULL || w == NULL) {
346 for (i = 0;i < m;++i) {
352 if (it->s == NULL || it->y == NULL) {
359 if (0 < param.
past) {
388 std::copy(pg,pg+n,d);
401 if (xnorm < 1.0) xnorm = 1.0;
402 if (gnorm / xnorm <= param.
epsilon) {
421 ls = linesearch(n, x, &fx, g, d, &step, xp, gp, w, &cd, ¶m);
423 ls = linesearch(n, x, &fx, g, d, &step, xp, pg, w, &cd, ¶m);
431 std::copy(xp,xp+n,x);
432 std::copy(gp,gp+n,g);
462 if (xnorm < 1.0) xnorm = 1.0;
463 if (gnorm / xnorm <= param.
epsilon) {
476 if (param.
past <= k) {
478 rate = (pf[k % param.
past] - fx) / fx;
481 if (rate < param.
delta) {
488 pf[k % param.
past] = fx;
524 bound = (m <= k) ? m : k;
531 std::copy(g, g+n, d);
534 std::copy(pg, pg+n, d);
539 for (i = 0;i < bound;++i) {
551 for (i = 0;i < bound;++i) {
566 if (d[i] * pg[i] >= 0) {
580 if (ptr_fx != NULL) {
588 for (i = 0;i < m;++i) {
640 dgtest = param->
ftol * dginit;
644 std::copy(xp,xp+n,x);
674 if (*f > finit + *stp * dgtest) {
685 if (dg < param->wolfe * dginit) {
694 if(dg > -param->
wolfe * dginit) {
703 if (*stp < param->min_step) {
736 int32_t i, count = 0;
746 for (i = 0;i < n;++i) {
747 wp[i] = (xp[i] == 0.) ? -gp[i] : xp[i];
752 std::copy(xp,xp+n,x);
770 for (i = 0;i < n;++i) {
771 dgtest += (x[i] - xp[i]) * gp[i];
774 if (*f <= finit + param->ftol * dgtest) {
779 if (*stp < param->min_step) {
813 int32_t brackt, stage1, uinfo = 0;
839 dgtest = param->
ftol * dginit;
841 prev_width = 2.0 * width;
862 stmin =
min2(stx, sty);
863 stmax =
max2(stx, sty);
866 stmax = *stp + 4.0 * (*stp - stx);
870 if (*stp < param->min_step) *stp = param->
min_step;
877 if ((brackt && ((*stp <= stmin || stmax <= *stp) || param->
max_linesearch <= count + 1 || uinfo != 0)) || (brackt && (stmax - stmin <= param->
xtol * stmax))) {
885 std::copy(xp,xp+n,x);
896 ftest1 = finit + *stp * dgtest;
900 if (brackt && ((*stp <= stmin || stmax <= *stp) || uinfo != 0)) {
904 if (*stp == param->
max_step && *f <= ftest1 && dg <= dgtest) {
908 if (*stp == param->
min_step && (ftest1 < *f || dgtest <= dg)) {
912 if (brackt && (stmax - stmin) <= param->
xtol * stmax) {
920 if (*f <= ftest1 && fabs(dg) <= param->
gtol * (-dginit)) {
929 if (stage1 && *f <= ftest1 &&
min2(param->
ftol, param->
gtol) * dginit <= dg) {
940 if (stage1 && ftest1 < *f && *f <= fx) {
942 fm = *f - *stp * dgtest;
943 fxm = fx - stx * dgtest;
944 fym = fy - sty * dgtest;
957 stmin, stmax, &brackt
961 fx = fxm + stx * dgtest;
962 fy = fym + sty * dgtest;
974 stmin, stmax, &brackt
982 if (0.66 * prev_width <= fabs(sty - stx)) {
983 *stp = stx + 0.5 * (sty - stx);
986 width = fabs(sty - stx);
998 #define USES_MINIMIZER \
999 float64_t a, d, gamma, theta, p, q, r, s;
1011 #define CUBIC_MINIMIZER(cm, u, fu, du, v, fv, dv) \
1013 theta = ((fu) - (fv)) * 3 / d + (du) + (dv); \
1017 s = max3(p, q, r); \
1020 gamma = s * sqrt(a * a - ((du) / s) * ((dv) / s)); \
1021 if ((v) < (u)) gamma = -gamma; \
1022 p = gamma - (du) + theta; \
1023 q = gamma - (du) + gamma + (dv); \
1039 #define CUBIC_MINIMIZER2(cm, u, fu, du, v, fv, dv, xmin, xmax) \
1041 theta = ((fu) - (fv)) * 3 / d + (du) + (dv); \
1045 s = max3(p, q, r); \
1048 gamma = s * sqrt(max2(0, a * a - ((du) / s) * ((dv) / s))); \
1049 if ((u) < (v)) gamma = -gamma; \
1050 p = gamma - (dv) + theta; \
1051 q = gamma - (dv) + gamma + (du); \
1053 if (r < 0. && gamma != 0.) { \
1054 (cm) = (v) - r * d; \
1055 } else if (a < 0) { \
1070 #define QUARD_MINIMIZER(qm, u, fu, du, v, fv) \
1072 (qm) = (u) + (du) / (((fu) - (fv)) / a + (du)) / 2 * a;
1082 #define QUARD_MINIMIZER2(qm, u, du, v, dv) \
1084 (qm) = (v) + (dv) / ((dv) - (du)) * a;
1086 #define fsigndiff(x, y) (*(x) * (*(y) / fabs(*(y))) < 0.)
1141 if (*t <=
min2(*x, *y) ||
max2(*x, *y) <= *t) {
1145 if (0. <= *dx * (*t - *x)) {
1169 if (fabs(mc - *x) < fabs(mq - *x)) {
1172 newt = mc + 0.5 * (mq - mc);
1185 if (fabs(mc - *t) > fabs(mq - *t)) {
1190 }
else if (fabs(*dt) < fabs(*dx)) {
1206 if (fabs(*t - mc) < fabs(*t - mq)) {
1212 if (fabs(*t - mc) > fabs(*t - mq)) {
1228 }
else if (*x < *t) {
1265 if (tmax < newt) newt = tmax;
1266 if (newt < tmin) newt = tmin;
1272 if (*brackt && bound) {
1273 mq = *x + 0.66 * (*y - *x);
1275 if (mq < newt) newt = mq;
1277 if (newt < mq) newt = mq;
1292 const int32_t start,
1299 for (i = start;i < n;++i) {
1312 const int32_t start,
1319 for (i = 0;i < start;++i) {
1324 for (i = start;i < end;++i) {
1328 }
else if (0. < x[i]) {
1335 }
else if (c < g[i]) {
1344 for (i = end;i < n;++i) {
1352 const int32_t start,
1358 for (i = start;i < end;++i) {
1359 if (d[i] * sign[i] <= 0) {
static T twonorm(const T *x, int32_t len)
|| x ||_2
int32_t lbfgs(int32_t n, float64_t *x, float64_t *ptr_fx, lbfgs_evaluate_t proc_evaluate, lbfgs_progress_t proc_progress, void *instance, lbfgs_parameter_t *_param, lbfgs_adjust_step_t proc_adjust_step)
static int32_t line_search_morethuente(int32_t n, float64_t *x, float64_t *f, float64_t *g, float64_t *s, float64_t *stp, const float64_t *xp, const float64_t *gp, float64_t *wa, callback_data_t *cd, const lbfgs_parameter_t *param)
static int32_t line_search_backtracking_owlqn(int32_t n, float64_t *x, float64_t *f, float64_t *g, float64_t *s, float64_t *stp, const float64_t *xp, const float64_t *gp, float64_t *wp, callback_data_t *cd, const lbfgs_parameter_t *param)
static void owlqn_project(float64_t *d, const float64_t *sign, const int32_t start, const int32_t end)
#define QUARD_MINIMIZER(qm, u, fu, du, v, fv)
static const lbfgs_parameter_t _defparam
#define CUBIC_MINIMIZER2(cm, u, fu, du, v, fv, dv, xmin, xmax)
lbfgs_progress_t proc_progress
#define QUARD_MINIMIZER2(qm, u, du, v, dv)
static int32_t line_search_backtracking(int32_t n, float64_t *x, float64_t *f, float64_t *g, float64_t *s, float64_t *stp, const float64_t *xp, const float64_t *gp, float64_t *wa, callback_data_t *cd, const lbfgs_parameter_t *param)
lbfgs_adjust_step_t proc_adjust_step
static void scale_vector(T alpha, T *vec, int32_t len)
Scale vector inplace.
static int32_t update_trial_interval(float64_t *x, float64_t *fx, float64_t *dx, float64_t *y, float64_t *fy, float64_t *dy, float64_t *t, float64_t *ft, float64_t *dt, const float64_t tmin, const float64_t tmax, int32_t *brackt)
static void owlqn_pseudo_gradient(float64_t *pg, const float64_t *x, const float64_t *g, const int32_t n, const float64_t c, const int32_t start, const int32_t end)
static float64_t owlqn_x1norm(const float64_t *x, const int32_t start, const int32_t n)
static float64_t dot(const bool *v1, const bool *v2, int32_t n)
Compute dot product between v1 and v2 (blas optimized)
#define CUBIC_MINIMIZER(cm, u, fu, du, v, fv, dv)
all of classes and functions are contained in the shogun namespace
static int is_infinity(double f)
checks whether a float is infinity
float64_t(* lbfgs_adjust_step_t)(void *instance, const float64_t *x, const float64_t *d, const int n, const float64_t step)
static int is_nan(double f)
checks whether a float is nan
lbfgs_evaluate_t proc_evaluate
float64_t(* lbfgs_evaluate_t)(void *instance, const float64_t *x, float64_t *g, const int n, const float64_t step)
int32_t(* line_search_proc)(int32_t n, float64_t *x, float64_t *f, float64_t *g, float64_t *s, float64_t *stp, const float64_t *xp, const float64_t *gp, float64_t *wa, callback_data_t *cd, const lbfgs_parameter_t *param)
int(* lbfgs_progress_t)(void *instance, const float64_t *x, const float64_t *g, const float64_t fx, const float64_t xnorm, const float64_t gnorm, const float64_t step, int n, int k, int ls)
void add(const SGVector< T > x)
void lbfgs_parameter_init(lbfgs_parameter_t *param)