Hi,

    When I am using the following code to to do an optimization (the
objective function takes a vector of vectors as data argument), I can
compile the program (Not MLE estimation, actually). However, I get a
Segmentation fault (core dumped)
 error every time I run it. Could you please give me some suggestions on
how to correct the code so that it will work?
Thanks a lot for your time and best regards,

Chong


double myvfunc(const std::vector<double> &x, std::vector<double> &grad,
void * my_func_data)
{
    std::vector<std::vector<double> > *d =
reinterpret_cast<std::vector<std::vector<double> > *>(my_func_data);
    std::vector<double> meanret = rowWiseMean(*d);
    if (!grad.empty()) {
        for (unsigned int i=0; i != meanret.size(); i++)
        grad[i] = meanret[i];
    }
    return dotProduct(x, meanret);
}


double myvconstraint(const std::vector<double> &x, std::vector<double>
&grad, void * my_ineconstr_data)
{
    std::vector<std::vector<double> > *d =
reinterpret_cast<std::vector<std::vector<double> > *>(my_ineconstr_data);
    unsigned int dim = (*d)[0].size();
    unsigned int T = (*d).size();
    std::vector<std::vector<double> > vc(dim, std::vector<double>(dim));
    for (unsigned int i = 0; i != T; i++)
        vc = matSum(vc, vectorProduct((*d)[i], (*d)[i]));
    vc = scalarDivideMat(double(T), vc);

    if (!grad.empty()) {
        for (unsigned int i =0; i != dim; i++)
        grad[i] = 2 * (matVecProd(vc, x)[i]);
    }
    return dotProduct(x, matVecProd(vc, x) ) - 0.03;
}



int main()

{

nlopt::opt opt(nlopt::LD_MMA, 5);

std::vector<double> lb(5);
lb[0] = 0; lb[1] = 0;  lb[2] = 0; lb[3] = 0; lb[4] = 0;
opt.set_lower_bounds(lb);

std::vector<double> ub(5);
ub[0] =1; ub[1] =1; ub[2] =1; ub[3] =1; ub[4] =1;
opt.set_upper_bounds(ub);


//genSimData is a function generate a 10 by 5 matrix of random numbers.
std::vector<std::vector<double> > dd = genSimData(10, 5, 3);

void *pd = &dd;


opt.set_min_objective(myvfunc, pd);
opt.add_inequality_constraint(myvconstraint, pd, 1e-8);


opt.set_xtol_rel(1e-4);

std::vector<double> x(5);
x[0] = 0.2; x[1] = 0.2; x[2] = 0.2; x[3]= 0.2; x[4]= 0.2;

double minf;
nlopt::result result = opt.optimize(x, minf);
std::cout << result << std::endl;
std::cout << minf << std::endl;
std::cout << x[0] << std::endl;
std::cout << x[1] << std::endl;
return 0;

}




-- 
Chong Li (李 崇)

Department of Economics,
Maxwell School of Citizenship and Public Affairs,
Syracuse University
Syracuse, NY 13244-1020

Email:[email protected]

Mobile: 315-744-2709
_______________________________________________
NLopt-discuss mailing list
[email protected]
http://ab-initio.mit.edu/cgi-bin/mailman/listinfo/nlopt-discuss

Reply via email to