snopt / snopt-interface

C/C++ API for SNOPT7
MIT License
27 stars 12 forks source link

User-defined derivative issue #14

Closed ShihaoWang closed 6 years ago

ShihaoWang commented 6 years ago

Hi there,

I am a new user of SNOPT and currently I get stuck in using the user-defined derivative to conduct a nonlinear optimization. After the definition of neG, iGfun and jGvar, I give the analytical expression of the jacobian matrix. However, the solver won't agree with the provided jacobian. After I set the "verify" option to 3, I find that actually the main reason for this problem is the discrepancy between the finite-difference gradient and the user-defined gradient.

Even though I may have an idea of what is causing this problem, I am not sure how to address this issue. For example, I define the neG, iGfun and jGvar as follows, // The Jacobian matrix neG = 0; iGfun[neG] = 0; jGvar[neG] = 2; neG++; iGfun[neG] = 1; jGvar[neG] = 1; neG++; iGfun[neG] = 1; jGvar[neG] = 2; neG++; iGfun[neG] = 1; jGvar[neG] = 6; neG++; iGfun[neG] = 1; jGvar[neG] = 7; neG++; iGfun[neG] = 1; jGvar[neG] = 8; neG++; iGfun[neG] = 2; jGvar[neG] = 1; neG++; iGfun[neG] = 2; jGvar[neG] = 2; neG++; iGfun[neG] = 2; jGvar[neG] = 6; neG++; It is clearly that these gradient position is not at the same column but from the finite-difference result,

  3  1.37080000E+00  1.30E-06         1         2    1.00000000E+00   -3.80984368E-01  bad?
                                                          2         3   -3.80984683E-01   -4.77450202E-01  bad?
                                                          3         4   -3.80984683E-01    2.63021396E-01  bad?
                                                          4         5   -2.11916696E-01    6.67226605E-02  bad?
                                                          5         6   -4.82328598E-02    4.52904509E-01  bad?
                                                          6         7    1.00000000E+00   -2.56602655E-01  bad?
                                                          7         8   -4.77450403E-01   -9.92130338E-01  bad?
                                                          8         9   -4.77450403E-01   -1.07612780E+00  bad?
  3  1.37080000E+00  1.30E-06           Objective   -3.99992654E-01   -3.99991355E-01  ok

the verification is conducted on the same column. I am not sure how to address this problem. Can you help me with this issue?

Thanks, Shihao

gnowzil commented 6 years ago

I can't really debug your problem with only a snippet of your definitions for iGfun/jGvar.

Are you specifying all derivatives (i.e., are you setting option 'Derivative option' to 0 or 1)?

Your Verify output is for the 3rd variable, when iGvar == 2 (in C, since SNOPT is Fortran and 1-indexed). Make sure you are computing the values of G in your user-defined function in the same order as you define the coordinates in iGfun/jGvar.

If you haven't already, I would recommend looking at the documentation (http://ccom.ucsd.edu/~optimizers/static/pdfs/snopt7-6.pdf), in particular sections 3.2 and 3.3. Just remember the doc is for the Fortran library so the indices start at 1.

ShihaoWang commented 6 years ago

Sorry for the previous confusion. Actually I just identifiy the cause of my problem. I forget to delete the computerJac() command while using my own gradient. After I remove this finite-difference gradient computation, the optimization solver works perfectly!