forum.alglib.net http://forum.alglib.net/ |
|
optimization with alglib http://forum.alglib.net/viewtopic.php?f=2&t=375 |
Page 1 of 1 |
Author: | 157679 [ Fri Jun 03, 2011 2:15 am ] |
Post subject: | optimization with alglib |
Hello, At the beginning I would like to thank you very much for creating such a great library. I’m trying to implement Zhang camera calibration algorithm and I would like to use your IMPROVED LEVENBERG-MARQUARDT METHOD FOR NON-LINEAR LEAST SQUARES OPTIMIZATION. But I don’t know if I’m using it right. I need to minimize the sum of squares of such equation F = m – H*M What gives me 3 equations: x, y, z - are coordinates of projected point m on image X, Y, Z - are coordinates of equivalent point M on the model. a, b, c, d, e, f, g, h, j – are parameters of homography H that have to be optimized to make the difference in function F as small as possible. I’m interested in using just function vector. Should the function1_fvec look like this? (z and Z are always 1 so I made the equations shorter). public static void function1_fvec(double[,] point_m, double[,] H, double[,] point_M, double[] fi, object obj) { fi[0] = point_m[0, 0] - H[0, 0] * point_M[0, 0] - H[0, 1] * point_M[1, 0] - H[0, 2]; fi[1] = point_m[1, 0] - H[1, 0] * point_M[0, 0] - H[1, 1] * point_M[1, 0] - H[1, 2]; fi[2] = point_m[2, 0] - H[2, 0] * point_M[0, 0] - H[2, 1] * point_M[1, 0] - H[2, 2]; } Let say I have 100points m and 100pts M. Can you please show me steps of how can I minimize sumOfSq = (m1 – HM1)^2 + (m2 – HM2)^2 + … + (m100 – HM100) ? PS. let say the startin point is p=(400,300) PS2. I read http://www.alglib.net/translator/man/ma ... _minlm_d_v But it doesn’t work and I don’t know where is a mistake.. |
Author: | Sergey.Bochkanov [ Fri Jun 03, 2011 6:57 am ] |
Post subject: | Re: optimization with alglib |
You have to reformulate your problem in order to fit into Levenberg-Marquardt framework. LM optimizer accepts function1_fvec(double[] x, double[] fi, object obj), where: x is a vector of parameters (it should contain H) fi is a vector of functions - you have 3*100 functions obj is additional parameter passed to the function - you should use it to pass list of points (m and M) to your function Your code should be something like: Code: public static void function1_fvec(double[] x, double[] fi, object obj) { my_special_type my_object = (my_special_type)obj; for(i=0; i<100; i++) { fi[3*i+0] = my_object.point_m[0, i] - x[0] * my_object.point_M[0, i] - x[1] * my_object.point_M[1, i] - x[2] * my_object.point_M[2, i]; fi[3*i+1] = my_object.point_m[1, i] - x[3] * my_object.point_M[0, i] - x[4] * my_object.point_M[1, i] - x[5] * my_object.point_M[2, i]; fi[3*i+2] = my_object.point_m[2, i] - x[6] * my_object.point_M[0, i] - x[7] * my_object.point_M[1, i] - x[8] * my_object.point_M[2, i]; } } where my_special_type is a class used to pass data to function1_fvec. It has only two fields - point_m and point_M, and its only purpose is to be container for these data. This object is passed as one of the parameters to minlmoptimize(). |
Author: | 157679 [ Sun Jun 05, 2011 4:27 pm ] |
Post subject: | Re: optimization with alglib |
It's alive! IT'S ALIVE!! <sinister laugh> :) Thank you so much! It works really good and gives me well optimized parameters. But to be 100% sure that I’m squeezing out the best performance I would like to ask you a few control questions: 1.What is the DiffStep in minlmcreatev()? 2. minlmsetscale the is the damping factor ? in http://en.wikipedia.org/wiki/Levenberg%E2%80%93Marquardt_algorithm#Choice_of_damping_parameter ? |
Author: | Sergey.Bochkanov [ Mon Jun 06, 2011 7:15 am ] |
Post subject: | Re: optimization with alglib |
1. DiffStep should be some small value - small when compared to the scale of the variables (see below). It can be 0.001 or something else. 2. minlmsetscale() influences: stopping criteria, damping parameter (algorithm will use different per-variable damping), numerical differentiation. You should calculate "most typical magnitude" of your variables and use it as parameter to minlmsetscale(). |
Author: | Tenshi8x [ Fri Aug 05, 2011 2:37 pm ] |
Post subject: | Re: optimization with alglib |
Sergey.Bochkanov wrote: You have to reformulate your problem in order to fit into Levenberg-Marquardt framework. LM optimizer accepts function1_fvec(double[] x, double[] fi, object obj), where: x is a vector of parameters (it should contain H) fi is a vector of functions - you have 3*100 functions obj is additional parameter passed to the function - you should use it to pass list of points (m and M) to your function Your code should be something like: Code: public static void function1_fvec(double[] x, double[] fi, object obj) { my_special_type my_object = (my_special_type)obj; for(i=0; i<100; i++) { fi[3*i+0] = my_object.point_m[0, i] - x[0] * my_object.point_M[0, i] - x[1] * my_object.point_M[1, i] - x[2] * my_object.point_M[2, i]; fi[3*i+1] = my_object.point_m[1, i] - x[3] * my_object.point_M[0, i] - x[4] * my_object.point_M[1, i] - x[5] * my_object.point_M[2, i]; fi[3*i+2] = my_object.point_m[2, i] - x[6] * my_object.point_M[0, i] - x[7] * my_object.point_M[1, i] - x[8] * my_object.point_M[2, i]; } } where my_special_type is a class used to pass data to function1_fvec. It has only two fields - point_m and point_M, and its only purpose is to be container for these data. This object is passed as one of the parameters to minlmoptimize(). Good afternoon, I have a similar problem: I must pass additional data to the function, but minlmoptimize doesn't accept data pointer as one of parameters, nor if I pass a dummy void* pointer. I've seen there are five instances of overloaded function minlmoptimize, but none of them seems to accept that pointer. I attach here the code that gives me the error. It's just the modified example of LM optimization without gradient: Code: void *ptr;//dummy pointer double epsg = 0.0000000001; double epsf = 0; double epsx = 0; ae_int_t maxits = 0; minlmstate state; minlmreport rep; minlmcreatev(m, X, 0.0001, state); minlmsetcond(state, epsg, epsf, epsx, maxits); minlmoptimize(state, function1_fvec, rep, ptr);//here I have a compiler error; it just accepts minlmoptimize(state, function1_fvec); minlmresults(state, X, rep); printf("%d\n", int(rep.terminationtype)); // EXPECTED: 4 printf("%s\n", X.tostring(2).c_str()); // EXPECTED: [-3,+3] return 0; What should I do in order to pass my additional data structure (not reported here)? I forgot to mention I'm using C/C++ in Visual Studio 2010. Thank you for the attention. Have a nice day. |
Author: | Sergey.Bochkanov [ Sat Aug 06, 2011 6:18 pm ] |
Post subject: | Re: optimization with alglib |
Code: minlmoptimize(state, function1_fvec, rep, ptr);//here I have a compiler error; it just accepts minlmoptimize(state, function1_fvec); You've incorrectly specified third parameter. It should be either pointer to callback used for reports (function which is called after each iteration). Code below should work: Code: minlmoptimize(state, function1_fvec, my_delegate_for_iteration_reports, ptr);
|
Author: | Tenshi8x [ Sat Aug 06, 2011 8:31 pm ] |
Post subject: | Re: optimization with alglib |
Thank you for the answer, anyway I solved in a "brute force" way: while I was analyzing the code I saw that in the header both report pointer and additional data one were set to NULL, so I just had to remove "NULL" from data pointer and pass my data pointer in main method. Everything worked, as in method implementation report and data pointer are supported. Maybe I'll use your "cleaner" solution in future. Now I wonder if it's possible to set linear constraints instead of box ones, or even emulate linear constraints through penalities in objective function. Thank you again. Sincerely, Fabio |
Author: | qwerty [ Fri Sep 04, 2015 2:48 pm ] |
Post subject: | Re: optimization with alglib |
As a new user of ALGLIB, let me add my thanks for your efforts to assemble this comprehensive tool. I have a problem that is identical to that posted by 157679, which is the first post in this discussion. Unfortunately, I don't understand the last sentence in your reply: "This object is passed as one of the parameters to minlmoptimize()." I cannot find how to pass any additional parameters to minlmoptimize. The example code for minlm_d_v works fine in my application (which is in c++), but I would like to use the pointer argument in function1_fvec() to access the arrays m and H for use in the minimization to obtain the coefficients M, using the terminology of the earlier post. I'm sure this is a trivially simple problem, but the solution has eluded me for more than a day, so I would greatly appreciate your help with it. Thanks in advance. |
Author: | jalil [ Thu Feb 28, 2019 9:44 am ] |
Post subject: | Re: optimization with alglib |
Hello! I have a problem with minlmoptimize function. When call it from main or function like in the example: ... int main(int argc, char **argv) { minlmcreatev(2, x, 0.0001, state); minlmsetbc(state, bndl, bndu); minlmsetcond(state, epsx, maxits); alglib::minlmoptimize(state, function1_fvec); minlmresults(state, x, rep); return 0; } Everything works great !!! But calling from class method, void Inversion_::inversion() { minlmcreatev(2, x, 0.0001, state); minlmsetbc(state, bnd_min, bnd_max); minlmsetcond(state, epsx, maxits); alglib::minlmoptimize(state, function1_fvec); //no instance of overloaded function "alglib::minlmoptimize" matches the argument list //argument types are: (alglib::minlmstate, void(const alglib::real_1d_array &x, alglib::real_1d_array &fi, void *ptr)) minlmresults(state, x, rep); } I get this ... no instance of overloaded function "" matches the argument list Apparently the pointer in the argument should be redefined or something like this, but i cant understand how. Please, can you help to solve the problem? class Inversion_ { real_1d_array x; real_1d_array bnd_min; // Minimal Pu = 0 pu real_1d_array bnd_max; // Maximal Pu = 60 pu double epsx = 0.00001; ae_int_t maxits = 0; minlmstate state; minlmreport rep; public: Inversion_(); void inversion(); void function1_fvec(const alglib::real_1d_array &x, alglib::real_1d_array &fi, void *ptr); }; |
Author: | Sergey.Bochkanov [ Fri Mar 01, 2019 9:45 am ] |
Post subject: | Re: optimization with alglib |
Hi! In C++ class methods are different from "regular functions". You can not pass pointer to class method where regular function is expected. If you want to pass a class method to ALGLIB, you can write your own "adapter function" which uses ptr parameter to call desired method. |
Page 1 of 1 | All times are UTC |
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |