void Lbfgs::minimize(int maxiter) { int n = objToMinimize.ProblemSize(); std::cout << "number of free variables for the minimizer: " << n << std::endl; std::vector<double> l(n); std::vector<double> u(n); Vint nbd(n); x.resize(n); g.resize(n); for (int i=0;i<n; i++) { l[i]=0; u[i]=0; nbd[i]=0; x[i] = 0.0; g[i] = 0.0; } //unconstrained problem int rc; int m = 5; m_opt = lbfgsb_create(n, m, &l[0], &u[0], &nbd[0]); assert(m_opt); m_opt->iprint=-1; double f = DBL_MAX; m_opt->max_iter = maxiter; int last_iter = 0; /* opt->iprint = 0;*/ while (1) { rc = lbfgsb_run(m_opt, &x[0], &f, &g[0]); if (rc == 0) { break; } else if (rc < 0) { printf("lbfgsb stop with an error"); break; } else if (rc == 1) { /* //check analytical derivatives with surreal: { std::vector<dbl> vdblx; tocplx(x,vdblx); std::vector<dbl> vdblg; tocplx(g,vdblg); f = objToMinimize.Function(vdblx); objToMinimize.Derivatives(vdblx,vdblg); g=todbl(vdblg); for (uint i=0; i<x.size(); i++) { std::vector<surreal> svdblx = vdblx ; svdblx[i]+=surreal(0,1); std::cout <<"check: " << svdblx[i] << std::endl; std::cout << g[i] << "==" << imag(objToMinimize.Function(svdblx)) << " " ; } std::cout << std::endl; } //end check derivatives */ if (last_iter < m_opt->niter) { //this is a new iteration last_iter = m_opt->niter; //saves the minimizer variables for each iteration (can be useful for generating animations) m_vars_over_time.push_back(x); } std::vector<dbl> vdblx; tocplx(x,vdblx); std::vector<dbl> vdblg; tocplx(g,vdblg); f = objToMinimize.Function(vdblx); objToMinimize.Derivatives(vdblx,vdblg); g=todbl(vdblg); // std::cout << "analytical derivatives: \n"; // for(uint i=0; i<g.size(); i++) // { // std::cout << "deriv[" << i << "]: " << g[i] << std::endl; // } // objToMinimize.NumDerivatives(x,g,true); } else { assert(!"can not reach here"); } } m_opt->task[59]='\0'; //add a null terminating character to task[] std::cout << m_opt->task << " | " << m_opt->niter << " iterations\n"; }
void pariplot(GEN a, GEN b, GEN code, GEN ysmlu,GEN ybigu, long prec) { const char BLANK = ' ', YY = '|', XX_UPPER = '\'', XX_LOWER = '.'; long jz, j, i, sig; pari_sp av = avma; int jnew, jpre = 0; /* for lint */ GEN x, dx; double diff, dyj, ysml, ybig, y[ISCR+1]; screen scr; char buf[80], z; sig=gcmp(b,a); if (!sig) return; if (sig<0) { x=a; a=b; b=x; } x = gtofp(a, prec); push_lex(x, code); dx = divru(gtofp(gsub(b,a),prec), ISCR-1); for (j=1; j<=JSCR; j++) scr[1][j]=scr[ISCR][j]=YY; for (i=2; i<ISCR; i++) { scr[i][1] = XX_LOWER; scr[i][JSCR]= XX_UPPER; for (j=2; j<JSCR; j++) scr[i][j] = BLANK; } ysml = ybig = 0.; /* -Wall */ for (i=1; i<=ISCR; i++) { pari_sp av2 = avma; y[i] = gtodouble( READ_EXPR(code,x) ); avma = av2; if (i == 1) ysml = ybig = y[1]; else { if (y[i] < ysml) ysml = y[i]; if (y[i] > ybig) ybig = y[i]; } x = addrr(x,dx); } avma = av; if (ysmlu) ysml = gtodouble(ysmlu); if (ybigu) ybig = gtodouble(ybigu); diff = ybig - ysml; if (!diff) { ybig += 1; diff= 1.; } dyj = ((JSCR-1)*3+2) / diff; /* work around bug in gcc-4.8 (32bit): plot(x=-5,5,sin(x)))) */ jz = 3 - (long)(ysml*dyj + 0.5); /* 3 - DTOL(ysml*dyj) */ z = PICTZERO(jz); jz /= 3; for (i=1; i<=ISCR; i++) { if (0<=jz && jz<=JSCR) scr[i][jz]=z; j = 3 + DTOL((y[i]-ysml)*dyj); jnew = j/3; if (i > 1) fill_gap(scr, i, jnew, jpre); if (0<=jnew && jnew<=JSCR) scr[i][jnew] = PICT(j); jpre = jnew; } pari_putc('\n'); pari_printf("%s ", dsprintf9(ybig, buf)); for (i=1; i<=ISCR; i++) pari_putc(scr[i][JSCR]); pari_putc('\n'); for (j=(JSCR-1); j>=2; j--) { pari_puts(" "); for (i=1; i<=ISCR; i++) pari_putc(scr[i][j]); pari_putc('\n'); } pari_printf("%s ", dsprintf9(ysml, buf)); for (i=1; i<=ISCR; i++) pari_putc(scr[i][1]); pari_putc('\n'); { char line[10 + 32 + 32 + ISCR - 9]; sprintf(line, "%10s%-9.7g%*.7g\n"," ",todbl(a),ISCR-9,todbl(b)); pari_printf(line); } pop_lex(1); }