Ejemplo n.º 1
0
int main(int argc, char *argv[])
{
	struct lbfgsb* data = lbfgsb_create(1, 5);
	data->iprint = 1;
	data->x[0] = 40;
	data->l[0] = -100;
	data->u[0] = 100;
	data->nbd[0] = 2;
	
//	printf("float size=%u\n", sizeof(float));
//	printf("double size= %u\n", sizeof(double));
//	printf("int size= %u\n", sizeof(int));
//	printf("char size= %u\n", sizeof(char));

	lbfgsb_set_task(data, LBFGSB_START);
	int i = 0;
	int i_max = 100;
	while(1)
	{
		if(i==i_max)
		{
			lbfgsb_set_task(data, LBFGSB_STOP);
			lbfgsb_step(data);
			break;
		}
		lbfgsb_step(data);
		printf("---->%d x: %lf\n", i, data->x[0]);
		enum lbfgsb_task_type type = lbfgsb_get_task(data);
		if(type==LBFGSB_FG)
		{
			printf("FG!\n");
			data->f = fun(data->x[0]);
			data->g[0] = df(data->x[0]);
		}
		else if(type==LBFGSB_NEW_X)
		{
			printf("NEW_X! - new iteration\n");
		}
		else if(type==LBFGSB_CONV)
		{
			printf("Convergence!\n");
			break;
		}
		else if(type==LBFGSB_ABNO)
		{
			printf("Abnormal exit!!\n");
			break;
		}
		else if(type==LBFGSB_ERROR)
		{
			printf("Error!!!\n");
			break;
		}
		i++;
	}
	printf("The end my friend\n");
	lbfgsb_delete(data);
	return 0;
}
Ejemplo n.º 2
0
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";
}