On 5/30/2014 01:16, Chong Li wrote:
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,
Hi!

I could take a look at the example, but this is not a Short, Self Contained, Correct Example (SSCCE).

In particular, you haven't provided the following functions (so no one else can compile the code, either):

segfault.cpp(8) : error C3861: 'rowWiseMean': identifier not found
segfault.cpp(13) : error C3861: 'dotProduct': identifier not found
segfault.cpp(23) : error C3861: 'matSum': identifier not found
segfault.cpp(23) : error C3861: 'vectorProduct': identifier not found
segfault.cpp(24) : error C3861: 'scalarDivideMat': identifier not found
segfault.cpp(28) : error C3861: 'matVecProd': identifier not found
segfault.cpp(30) : error C3861: 'dotProduct': identifier not found
segfault.cpp(30) : error C3861: 'matVecProd': identifier not found
segfault.cpp(48) : error C3861: 'genSimData': identifier not found

In the meantime / alternatively, I'd suggest running the code in a debugger to find out the offending line.
Chances are it's something trivial, like array index out of bounds...

Best,

Matt


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] <mailto:email%[email protected]>

Mobile: 315-744-2709






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

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

Reply via email to