forum.alglib.net

ALGLIB forum
It is currently Sun Dec 22, 2024 11:36 pm

All times are UTC


Forum rules


1. This forum can be used for discussion of both ALGLIB-related and general numerical analysis questions
2. This forum is English-only - postings in other languages will be removed.



Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: segfault in minlmresults
PostPosted: Sat Dec 31, 2011 1:59 pm 
Offline

Joined: Sat Dec 31, 2011 1:26 pm
Posts: 4
Hi!

I made this simple code from http://www.alglib.net/translator/man/manual.cpp.html#example_minlm_d_v
It segfaults at "minlmresults(state, x, rep);"
Code:
using namespace alglib;
...
    ae_int_t maxits = 20;
    minlmstate state;
    minlmreport rep;

    real_1d_array x = "[1,0,0,0,0,1,0,0,0,0,1,0]";

    minlmcreatev(12, x, 0.0001, state);
    minlmsetcond(state, 0.0, 0.0, 0.0, maxits);
    alglib::minlmoptimize(state, fvec,NULL,this);
    qDebug() <<"minlm5";
    minlmresults(state, x, rep);
   ...
}

static void fvec(const real_1d_array &x, real_1d_array &fi, void *ptr){
...
}

What is that I make wrong?


Top
 Profile  
 
 Post subject: Re: segfault in minlmresults
PostPosted: Sun Jan 01, 2012 9:18 am 
Offline

Joined: Sat Dec 31, 2011 1:26 pm
Posts: 4
QtCreator say this (in Debug mode):
Code:
Level Function                   File       Line   Address
0   __GI___libc_free             malloc.c   3709   0x7ffff4d53d29   
1   alglib_impl::aligned_free    ap.cpp      183   0x58c16e   
2   alglib_impl::ae_free         ap.cpp      212   0x58c1ef   
3   alglib_impl::ae_db_free      ap.cpp      486   0x58c781   
4   alglib_impl::ae_vector_clear ap.cpp      641   0x58cac2   
5   alglib_impl::minlmresults    optimization.cpp   14195   0x49be19   
6   alglib::minlmresults         optimization.cpp   5019   0x484465   
7   Process::run                 process.cpp 108   0x4133d8   
8   ??            /usr/lib/x86_64-linux-gnu/libQtCore.so.4   0   0x7ffff6190d05   
9   start_thread                 pthread_create.c   304   0x7ffff5a2cefc   
10  clone                        clone.S     112   0x7ffff4db889d   
11  ??                                         0      

My code is in level 7


Top
 Profile  
 
 Post subject: Re: segfault in minlmresults
PostPosted: Sun Jan 01, 2012 10:00 am 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
The most probable reason is that you've accidentally corrupted memory somewhere in the fvec(). It can be array bounds violation, incorrect operation with pointers or something else. The corruption does not reveal itself until first operation with dynamic memory, which is done in the minlmresults().

I've checked code similar to yours on my machine, it works fine. So I suppose that you should check fvec() for errors, especially array-related errors.


Top
 Profile  
 
 Post subject: Re: segfault in minlmresults
PostPosted: Sun Jan 01, 2012 10:29 am 
Offline

Joined: Sat Dec 31, 2011 1:26 pm
Posts: 4
Here is my fvec(). I've cut some line from it (//...) , they are very similar, so I think they are irrelevant.
Indexes of fi are continuous.
Do you see mistake?

Mat is opencv's Mat.

Code:
static void fvec(const real_1d_array &x, real_1d_array &fi, void *ptr){

    Process *pr=(Process *) ptr;
    double q[4][4] = { { x[0],x[1],x[2],x[3] },{ x[4],x[5],x[6],x[7] },{ x[8],x[9],x[10],x[11] },{0,0,0,1} };
    Mat A = Mat(4,4, CV_64FC1);
    for(int i = 0; i<4; ++i)
        for(int j = 0; j < 4; ++j){
            A.at<double>(i,j) = q[i][j];
        }
    if(determinant(A) < 0.0){
        fi[0]=alglib::maxrealnumber;
        return;
    }

    fi[0] = q[0][0] * pr->q_p->x + q[0][1] * pr->q_p->y + q[0][2] * pr->q_p->z + q[0][3] * pr->q_p->mu - pr->eqr[0][0];

    fi[1] = pow(q[0][0], 2.0) * pr->q_p->x2
        + 2 * q[0][0] * q[0][1] * pr->q_p->xy
        + 2 * q[0][0] * q[0][2] * pr->q_p->xz
        + 2 * q[0][0] * q[0][3] * pr->q_p->x
        + pow(q[0][1], 2.0) * pr->q_p->y2
        + 2 * q[0][1] * q[0][2] * pr->q_p->yz
        + 2 * q[0][1] * q[0][3] * pr->q_p->y
        + pow(q[0][2], 2.0) * pr->q_p->z2
        + 2 * q[0][2] * q[0][3] * pr->q_p->z
        + pow(q[0][3], 2.0) * pr->q_p->mu
        - pr->eqr[1][0];

    fi[2] = 3 * q[0][0] * pow(q[0][2], 2.0) * pr->q_p->xz2
        + 3 * q[0][1] * pow(q[0][2], 2.0) * pr->q_p->yz2
        + 3 * pow(q[0][0], 2.0) * q[0][1] * pr->q_p->x2y
        + 3 * pow(q[0][1], 2.0) * q[0][2] * pr->q_p->y2z
        + 3 * pow(q[0][0], 2.0) * q[0][2] * pr->q_p->x2z
        + 3 * q[0][0] * pow(q[0][1], 2.0) * pr->q_p->xy2
        + 6 * q[0][0] * q[0][1] * q[0][3] * pr->q_p->xy
        + 6 * q[0][0] * q[0][1] * q[0][2] * pr->q_p->xyz
        + 3 * pow(q[0][1], 2.0) * q[0][3] * pr->q_p->y2
        + 3 * q[0][1] * pow(q[0][3], 2.0) * pr->q_p->y
        + 3 * pow(q[0][2], 2.0) * q[0][3] * pr->q_p->z2
        + 3 * q[0][0] * pow(q[0][3], 2.0) * pr->q_p->x
        + 3 * q[0][2] * pow(q[0][3], 2.0) * pr->q_p->z
        + 3 * pow(q[0][0], 2.0) * q[0][3] * pr->q_p->x2
        + pow(q[0][1], 3.0) * pr->q_p->y3
        + pow(q[0][3], 3.0) * pr->q_p->mu
        + 6 * q[0][0] * q[0][2] * q[0][3] * pr->q_p->xz
        + 6 * q[0][1] * q[0][2] * q[0][3] * pr->q_p->yz
        + pow(q[0][0], 3.0) * pr->q_p->x3
        + pow(q[0][2], 3.0) * pr->q_p->z3
        - pr->eqr[2][0];


    fi[3] = q[1][0] * pr->q_p->x + q[1][1] * pr->q_p->y + q[1][2] * pr->q_p->z + q[1][3] * pr->q_p->mu - pr->eqr[0][1];

    //...

    fi[18] = q[0][0] * q[1][0] * q[2][1] * pr->q_p->x2y
        + q[0][1] * q[1][0] * q[2][3] * pr->q_p->xy
        + q[0][1] * q[1][1] * q[2][0] * pr->q_p->xy2
        + q[0][1] * q[1][1] * q[2][2] * pr->q_p->y2z
        + q[0][1] * q[1][2] * q[2][0] * pr->q_p->xyz
        + q[0][1] * q[1][2] * q[2][3] * pr->q_p->yz
        + q[0][1] * q[1][3] * q[2][0] * pr->q_p->xy
        + q[0][1] * q[1][3] * q[2][2] * pr->q_p->yz
        + q[0][0] * q[1][1] * q[2][3] * pr->q_p->xy
        + q[0][1] * q[1][2] * q[2][1] * pr->q_p->y2z
        + q[0][1] * q[1][2] * q[2][2] * pr->q_p->yz2
        + q[0][1] * q[1][0] * q[2][2] * pr->q_p->xyz
        + q[0][0] * q[1][0] * q[2][2] * pr->q_p->x2z
        + q[0][0] * q[1][1] * q[2][0] * pr->q_p->x2y
        + q[0][0] * q[1][1] * q[2][1] * pr->q_p->xy2
        + q[0][0] * q[1][1] * q[2][2] * pr->q_p->xyz
        + q[0][0] * q[1][2] * q[2][0] * pr->q_p->x2z
        + q[0][0] * q[1][2] * q[2][1] * pr->q_p->xyz
        + q[0][0] * q[1][2] * q[2][2] * pr->q_p->xz2
        + q[0][1] * q[1][1] * q[2][3] * pr->q_p->y2
        + q[0][3] * q[1][2] * q[2][3] * pr->q_p->z
        + q[0][1] * q[1][1] * q[2][1] * pr->q_p->y3
        + q[0][0] * q[1][0] * q[2][0] * pr->q_p->x3
        + q[0][0] * q[1][0] * q[2][3] * pr->q_p->x2
        + q[0][0] * q[1][3] * q[2][0] * pr->q_p->x2
        + q[0][0] * q[1][3] * q[2][3] * pr->q_p->x
        + q[0][2] * q[1][2] * q[2][3] * pr->q_p->z2
        + q[0][3] * q[1][0] * q[2][3] * pr->q_p->x
        + q[0][3] * q[1][1] * q[2][1] * pr->q_p->y2
        + q[0][3] * q[1][1] * q[2][3] * pr->q_p->y
        + q[0][2] * q[1][3] * q[2][2] * pr->q_p->z2
        + q[0][1] * q[1][3] * q[2][1] * pr->q_p->y2
        + q[0][1] * q[1][3] * q[2][3] * pr->q_p->y
        + q[0][2] * q[1][2] * q[2][2] * pr->q_p->z3
        + q[0][3] * q[1][3] * q[2][0] * pr->q_p->x
        + q[0][2] * q[1][3] * q[2][3] * pr->q_p->z
        + q[0][3] * q[1][0] * q[2][0] * pr->q_p->x2
        + q[0][3] * q[1][3] * q[2][3] * pr->q_p->mu
        + q[0][3] * q[1][3] * q[2][2] * pr->q_p->z
        + q[0][3] * q[1][3] * q[2][1] * pr->q_p->y
        + q[0][3] * q[1][2] * q[2][2] * pr->q_p->z2
        + q[0][0] * q[1][2] * q[2][3] * pr->q_p->xz
        + q[0][0] * q[1][3] * q[2][1] * pr->q_p->xy
        + q[0][0] * q[1][3] * q[2][2] * pr->q_p->xz
        + q[0][1] * q[1][0] * q[2][0] * pr->q_p->x2y
        + q[0][1] * q[1][0] * q[2][1] * pr->q_p->xy2
        + q[0][2] * q[1][0] * q[2][1] * pr->q_p->xyz
        + q[0][2] * q[1][0] * q[2][2] * pr->q_p->xz2
        + q[0][3] * q[1][0] * q[2][1] * pr->q_p->xy
        + q[0][3] * q[1][0] * q[2][2] * pr->q_p->xz
        + q[0][3] * q[1][1] * q[2][0] * pr->q_p->xy
        + q[0][3] * q[1][1] * q[2][2] * pr->q_p->yz
        + q[0][3] * q[1][2] * q[2][0] * pr->q_p->xz
        + q[0][3] * q[1][2] * q[2][1] * pr->q_p->yz
        + q[0][2] * q[1][0] * q[2][3] * pr->q_p->xz
        + q[0][2] * q[1][1] * q[2][0] * pr->q_p->xyz
        + q[0][2] * q[1][1] * q[2][1] * pr->q_p->y2z
        + q[0][2] * q[1][1] * q[2][2] * pr->q_p->yz2
        + q[0][2] * q[1][1] * q[2][3] * pr->q_p->yz
        + q[0][2] * q[1][2] * q[2][0] * pr->q_p->xz2
        + q[0][2] * q[1][2] * q[2][1] * pr->q_p->yz2
        + q[0][2] * q[1][3] * q[2][0] * pr->q_p->xz
        + q[0][2] * q[1][3] * q[2][1] * pr->q_p->yz
        + q[0][2] * q[1][0] * q[2][0] * pr->q_p->x2z
        - pr->p_p->xyz;



    Mat B = A.inv();
    for(int i = 0; i<4; ++i)
        for(int j = 0; j < 4; ++j){
            q[i][j] = B.at<double>(i,j);
        }



    fi[19] = q[0][0] * pr->p_p->x + q[0][1] * pr->p_p->y + q[0][2] * pr->p_p->z + q[0][3] * pr->p_p->mu - pr->ieqr[0][0];
    fi[20] = pow(q[0][0], 2.0) * pr->p_p->x2 + 2 * q[0][0] * q[0][1] * pr->p_p->xy
        + 2 * q[0][0] * q[0][2] * pr->p_p->xz
        + 2 * q[0][0] * q[0][3] * pr->p_p->x
        + pow(q[0][1], 2.0) * pr->p_p->y2
        + 2 * q[0][1] * q[0][2] * pr->p_p->yz
        + 2 * q[0][1] * q[0][3] * pr->p_p->y
        + pow(q[0][2], 2.0) * pr->p_p->z2
        + 2 * q[0][2] * q[0][3] * pr->p_p->z
        + pow(q[0][3], 2.0) * pr->p_p->mu
        - pr->ieqr[1][0];

    //...

    fi[37] = q[0][0] * q[1][0] * q[2][1] * pr->p_p->x2y
        + q[0][1] * q[1][0] * q[2][3] * pr->p_p->xy
        + q[0][1] * q[1][1] * q[2][0] * pr->p_p->xy2
        + q[0][1] * q[1][1] * q[2][2] * pr->p_p->y2z
        + q[0][1] * q[1][2] * q[2][0] * pr->p_p->xyz
        + q[0][1] * q[1][2] * q[2][3] * pr->p_p->yz
        + q[0][1] * q[1][3] * q[2][0] * pr->p_p->xy
        + q[0][1] * q[1][3] * q[2][2] * pr->p_p->yz
        + q[0][0] * q[1][1] * q[2][3] * pr->p_p->xy
        + q[0][1] * q[1][2] * q[2][1] * pr->p_p->y2z
        + q[0][1] * q[1][2] * q[2][2] * pr->p_p->yz2
        + q[0][1] * q[1][0] * q[2][2] * pr->p_p->xyz
        + q[0][0] * q[1][0] * q[2][2] * pr->p_p->x2z
        + q[0][0] * q[1][1] * q[2][0] * pr->p_p->x2y
        + q[0][0] * q[1][1] * q[2][1] * pr->p_p->xy2
        + q[0][0] * q[1][1] * q[2][2] * pr->p_p->xyz
        + q[0][0] * q[1][2] * q[2][0] * pr->p_p->x2z
        + q[0][0] * q[1][2] * q[2][1] * pr->p_p->xyz
        + q[0][0] * q[1][2] * q[2][2] * pr->p_p->xz2
        + q[0][1] * q[1][1] * q[2][3] * pr->p_p->y2
        + q[0][3] * q[1][2] * q[2][3] * pr->p_p->z
        + q[0][1] * q[1][1] * q[2][1] * pr->p_p->y3
        + q[0][0] * q[1][0] * q[2][0] * pr->p_p->x3
        + q[0][0] * q[1][0] * q[2][3] * pr->p_p->x2
        + q[0][0] * q[1][3] * q[2][0] * pr->p_p->x2
        + q[0][0] * q[1][3] * q[2][3] * pr->p_p->x
        + q[0][2] * q[1][2] * q[2][3] * pr->p_p->z2
        + q[0][3] * q[1][0] * q[2][3] * pr->p_p->x
        + q[0][3] * q[1][1] * q[2][1] * pr->p_p->y2
        + q[0][3] * q[1][1] * q[2][3] * pr->p_p->y
        + q[0][2] * q[1][3] * q[2][2] * pr->p_p->z2
        + q[0][1] * q[1][3] * q[2][1] * pr->p_p->y2
        + q[0][1] * q[1][3] * q[2][3] * pr->p_p->y
        + q[0][2] * q[1][2] * q[2][2] * pr->p_p->z3
        + q[0][3] * q[1][3] * q[2][0] * pr->p_p->x
        + q[0][2] * q[1][3] * q[2][3] * pr->p_p->z
        + q[0][3] * q[1][0] * q[2][0] * pr->p_p->x2
        + q[0][3] * q[1][3] * q[2][3] * pr->p_p->mu
        + q[0][3] * q[1][3] * q[2][2] * pr->p_p->z
        + q[0][3] * q[1][3] * q[2][1] * pr->p_p->y
        + q[0][3] * q[1][2] * q[2][2] * pr->p_p->z2
        + q[0][0] * q[1][2] * q[2][3] * pr->p_p->xz
        + q[0][0] * q[1][3] * q[2][1] * pr->p_p->xy
        + q[0][0] * q[1][3] * q[2][2] * pr->p_p->xz
        + q[0][1] * q[1][0] * q[2][0] * pr->p_p->x2y
        + q[0][1] * q[1][0] * q[2][1] * pr->p_p->xy2
        + q[0][2] * q[1][0] * q[2][1] * pr->p_p->xyz
        + q[0][2] * q[1][0] * q[2][2] * pr->p_p->xz2
        + q[0][3] * q[1][0] * q[2][1] * pr->p_p->xy
        + q[0][3] * q[1][0] * q[2][2] * pr->p_p->xz
        + q[0][3] * q[1][1] * q[2][0] * pr->p_p->xy
        + q[0][3] * q[1][1] * q[2][2] * pr->p_p->yz
        + q[0][3] * q[1][2] * q[2][0] * pr->p_p->xz
        + q[0][3] * q[1][2] * q[2][1] * pr->p_p->yz
        + q[0][2] * q[1][0] * q[2][3] * pr->p_p->xz
        + q[0][2] * q[1][1] * q[2][0] * pr->p_p->xyz
        + q[0][2] * q[1][1] * q[2][1] * pr->p_p->y2z
        + q[0][2] * q[1][1] * q[2][2] * pr->p_p->yz2
        + q[0][2] * q[1][1] * q[2][3] * pr->p_p->yz
        + q[0][2] * q[1][2] * q[2][0] * pr->p_p->xz2
        + q[0][2] * q[1][2] * q[2][1] * pr->p_p->yz2
        + q[0][2] * q[1][3] * q[2][0] * pr->p_p->xz
        + q[0][2] * q[1][3] * q[2][1] * pr->p_p->yz
        + q[0][2] * q[1][0] * q[2][0] * pr->p_p->x2z
        - pr->q_p->xyz;

}

Thanks for your help


Top
 Profile  
 
 Post subject: Re: segfault in minlmresults
PostPosted: Sun Jan 01, 2012 12:34 pm 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
Your code accesses fi[37], but LM optimizer expects that there are only 12 functions (you have passed 12, not 38, when creating optimizer object).


Top
 Profile  
 
 Post subject: Re: segfault in minlmresults
PostPosted: Sun Jan 01, 2012 3:51 pm 
Offline

Joined: Sat Dec 31, 2011 1:26 pm
Posts: 4
I've 12 parameter, but I've 38 function.
So I need "minlmcreatev(38 ...)"

Thank you


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 32 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group