Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test with ADNLPModels.jl #31

Open
jbcaillau opened this issue Jan 31, 2025 · 12 comments
Open

Test with ADNLPModels.jl #31

jbcaillau opened this issue Jan 31, 2025 · 12 comments

Comments

@jbcaillau
Copy link
Member

hi @amontoison @PierreMartinon ; trying to test a hand-coded version of Goddard problem with ADNLPModels, and running into the error below (sparsity detection). both the cost and the inplace constraint evaluate properly, but when defining the model in

nlp = ADNLPModel!(f, z, lvar, uvar, con!, lcon, ucon)

the following error is encountered (apparently when trying to detect sparsisty):

Image

any clue? the code (+ Project.toml) is here:
https://github.com/control-toolbox/CTBenchmarks.jl/blob/main/adnlp/goddard-ad.jl

@jbcaillau
Copy link
Member Author

PS. @amontoison i think it is a good occasion to make your new github avatar shine 🙂

@amontoison
Copy link

amontoison commented Jan 31, 2025

@jbcaillau When you pass the functions to ADNLPModels.jl at the end of your code,
It should be the objective and the constraints.
I think you don't provide the constraints with this line:
https://github.com/control-toolbox/CTBenchmarks.jl/blob/main/adnlp/goddard-ad.jl#L193

I mean that your cons! seems to be a tuple:
https://github.com/control-toolbox/CTBenchmarks.jl/blob/main/adnlp/goddard-ad.jl#L141
I don't understand why you have nothing in the definition of the constraints.

@jbcaillau
Copy link
Member Author

@amontoison thanks for the feedback. con! is inplace and returns nothing (not a tuple).

@amontoison
Copy link

If cons! is inplace, it should return the vector of constraints updated.

@amontoison
Copy link

amontoison commented Jan 31, 2025

@jbcaillau My function Lagrangian will not like that otherwise in ADNLPModels.jl:
https://github.com/JuliaSmoothOptimizers/ADNLPModels.jl/blob/main/src/sparsity_pattern.jl#L48
It will do a dot product between Nothing and Vector{Float64}.

You know what, I can call cons!(cx, x) on a different line such that this function returns anything.

@jbcaillau
Copy link
Member Author

jbcaillau commented Jan 31, 2025

ah ok, didn't notice that (it is indeed the case in this doc example). yes, nicer not to assume that. waiting for JuliaSmoothOptimizers/ADNLPModels.jl#323 (you know i don't like to merge before tests are complete 🥲), thanks

@amontoison
Copy link

@jbcaillau

julia> include("goddard-ad.jl")
This is Ipopt version 3.14.17, running with linear solver MUMPS 5.7.3.

Number of nonzeros in equality constraint Jacobian...:     1904
Number of nonzeros in inequality constraint Jacobian.:        0
Number of nonzeros in Lagrangian Hessian.............:     1111

Total number of variables............................:      405
                     variables with only lower bounds:      102
                variables with lower and upper bounds:      202
                     variables with only upper bounds:        0
Total number of equality constraints.................:      304
Total number of inequality constraints...............:        0
        inequality constraints with only lower bounds:        0
   inequality constraints with lower and upper bounds:        0
        inequality constraints with only upper bounds:        0

iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
   0 -1.0100000e+00 9.00e-01 2.00e+00   0.0 0.00e+00    -  0.00e+00 0.00e+00   0
   1 -1.0090722e+00 8.99e-01 6.78e+01   1.3 1.67e+02    -  3.70e-03 5.93e-04f  1
   2 -1.0000907e+00 8.73e-01 1.93e+02   1.0 6.31e+00    -  3.37e-02 2.97e-02h  1
   3 -1.0023839e+00 8.36e-01 1.06e+04   1.0 6.92e+00    -  1.91e-01 4.27e-02f  1
   4 -1.0025122e+00 7.66e-01 8.42e+03   1.4 4.31e+00    -  1.00e+00 8.33e-02f  1
   5 -1.0033877e+00 7.13e-01 2.32e+05   2.3 3.74e+00    -  5.60e-01 6.86e-02f  1
   6 -1.0137050e+00 8.96e-03 9.94e+04   2.3 7.13e-01    -  3.84e-01 9.90e-01h  1
   7 -1.0099666e+00 3.16e-03 3.40e+05   1.8 4.32e-01    -  5.44e-01 9.90e-01h  1
   8 -1.0068998e+00 2.81e-04 1.55e+06   0.6 2.17e-01    -  7.02e-01 9.91e-01h  1
   9 -1.0068334e+00 4.51e-06 2.15e+07   0.1 1.12e-01    -  7.18e-01 1.00e+00h  1
iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
  10 -1.0068338e+00 1.72e-10 6.68e+05  -5.1 3.74e-04    -  9.89e-01 1.00e+00h  1
  11 -1.0068350e+00 4.32e-10 7.58e+03  -7.0 4.82e-04    -  9.89e-01 1.00e+00h  1
  12 -1.0079594e+00 6.12e-04 8.99e+03  -3.0 4.61e-01    -  6.49e-01 7.54e-01f  1
  13 -1.0081967e+00 3.11e-06 7.67e+03  -9.0 1.15e-02    -  8.76e-01 1.00e+00h  1
  14 -1.0092440e+00 1.72e-04 4.11e+02  -4.5 2.26e-01    -  1.00e+00 8.65e-01h  1
  15 -1.0106706e+00 2.19e-04 1.52e+02  -4.7 2.25e-01    -  1.00e+00 7.05e-01h  1
  16 -1.0118589e+00 9.30e-05 1.65e-03  -5.3 9.93e-02    -  1.00e+00 1.00e+00h  1
  17 -1.0123711e+00 5.50e-05 1.92e+01  -5.6 9.70e-02    -  1.00e+00 9.16e-01h  1
  18 -1.0125010e+00 1.98e-05 2.19e-04  -6.1 9.58e-02    -  1.00e+00 1.00e+00h  1
  19 -1.0125534e+00 1.02e-05 1.90e+00  -6.7 9.04e-02    -  1.00e+00 9.68e-01h  1
iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
  20 -1.0125690e+00 4.93e-06 1.19e-01  -7.5 9.66e-02    -  1.00e+00 9.94e-01h  1
  21 -1.0125708e+00 1.93e-06 4.96e-06  -8.0 1.46e-01    -  1.00e+00 1.00e+00h  1
  22 -1.0125715e+00 7.00e-07 2.30e-03  -8.8 1.09e-01    -  1.00e+00 9.97e-01h  1
  23 -1.0125716e+00 2.60e-08 3.61e-04  -9.4 1.92e-02    -  1.00e+00 9.97e-01h  1
  24 -1.0125716e+00 8.67e-11 4.72e-10 -11.0 1.43e-03    -  1.00e+00 1.00e+00h  1

Number of Iterations....: 24

                                   (scaled)                 (unscaled)
Objective...............:  -1.0125716188852516e+00   -1.0125716188852516e+00
Dual infeasibility......:   4.7236740752417433e-10    4.7236740752417433e-10
Constraint violation....:   1.8882118091312350e-11    8.6655016495740256e-11
Variable bound violation:   5.1043528986882691e-09    5.1043528986882691e-09
Complementarity.........:   1.2638315525660684e-11    1.2638315525660684e-11
Overall NLP error.......:   1.8882118091312350e-11    4.7236740752417433e-10


Number of objective function evaluations             = 25
Number of objective gradient evaluations             = 25
Number of equality constraint evaluations            = 25
Number of inequality constraint evaluations          = 0
Number of equality constraint Jacobian evaluations   = 25
Number of inequality constraint Jacobian evaluations = 0
Number of Lagrangian Hessian evaluations             = 24
Total seconds in IPOPT                               = 10.350

EXIT: Optimal Solution Found.
lcon    : true
ucon    : true
dynamics: 1.0859130379933743e-10
This is Ipopt version 3.14.17, running with linear solver MUMPS 5.7.3.

Number of nonzeros in equality constraint Jacobian...:     1904
Number of nonzeros in inequality constraint Jacobian.:      304
Number of nonzeros in Lagrangian Hessian.............:     1111

Total number of variables............................:      405
                     variables with only lower bounds:        0
                variables with lower and upper bounds:        0
                     variables with only upper bounds:        0
Total number of equality constraints.................:      304
Total number of inequality constraints...............:      304
        inequality constraints with only lower bounds:      102
   inequality constraints with lower and upper bounds:      202
        inequality constraints with only upper bounds:        0

iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
   0 -1.0125716e+00 8.67e-11 2.81e-02  -1.0 0.00e+00    -  0.00e+00 0.00e+00   0
   1 -1.0111610e+00 8.31e-05 1.02e+01  -1.7 1.12e-01    -  5.78e-01 9.90e-01f  1
   2 -1.0101393e+00 1.56e-04 9.07e+02  -1.7 1.76e-01    -  4.77e-01 9.30e-01f  1
   3 -1.0091937e+00 5.94e-05 2.35e+02  -1.7 1.02e-01    -  1.00e+00 9.91e-01f  1
   4 -1.0083592e+00 5.20e-05 4.81e-01  -1.7 3.74e-01    -  1.00e+00 1.00e+00f  1
   5 -1.0083388e+00 1.19e-08 1.87e-02  -3.8 1.42e-03    -  1.00e+00 1.00e+00h  1
   6 -1.0091035e+00 1.69e-05 5.29e-03  -3.8 8.04e-02    -  1.00e+00 1.00e+00h  1
   7 -1.0091568e+00 5.46e-08 6.42e-06  -3.8 8.26e-03    -  1.00e+00 1.00e+00h  1
   8 -1.0097679e+00 7.71e-06 1.20e+03  -5.7 4.81e-02    -  9.19e-01 1.00e+00h  1
   9 -1.0118339e+00 1.53e-04 1.17e+02  -5.7 1.53e-01    -  9.03e-01 8.03e-01h  1
iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
  10 -1.0123562e+00 4.80e-05 7.58e-02  -5.7 9.11e-02    -  1.00e+00 8.37e-01h  1
  11 -1.0124172e+00 1.16e-05 1.53e-04  -5.7 8.18e-02    -  1.00e+00 1.00e+00f  1
  12 -1.0124174e+00 2.43e-07 1.63e-06  -5.7 1.54e-02    -  1.00e+00 1.00e+00h  1
  13 -1.0125387e+00 6.16e-06 6.55e+00  -8.6 5.90e-02    -  8.31e-01 7.95e-01h  1
  14 -1.0125666e+00 5.76e-06 9.91e-02  -8.6 8.90e-02    -  8.09e-01 8.45e-01h  1
  15 -1.0125708e+00 2.33e-06 1.01e-01  -8.6 8.91e-02    -  8.58e-01 8.73e-01h  1
  16 -1.0125714e+00 1.56e-06 5.52e-02  -8.6 1.69e-01    -  9.15e-01 9.78e-01f  1
  17 -1.0125714e+00 1.26e-07 8.14e-08  -8.6 3.89e-02    -  1.00e+00 1.00e+00f  1
  18 -1.0125714e+00 1.89e-10 1.11e-10  -8.6 2.05e-03    -  1.00e+00 1.00e+00h  1

Number of Iterations....: 18

                                   (scaled)                 (unscaled)
Objective...............:  -1.0125714090188309e+00   -1.0125714090188309e+00
Dual infeasibility......:   1.1143212901759571e-10    1.1143212901759571e-10
Constraint violation....:   1.8903026296628088e-10    1.8903026296628088e-10
Variable bound violation:   0.0000000000000000e+00    0.0000000000000000e+00
Complementarity.........:   2.5126078866705649e-09    2.5126078866705649e-09
Overall NLP error.......:   2.5126078866705649e-09    2.5126078866705649e-09


Number of objective function evaluations             = 19
Number of objective gradient evaluations             = 19
Number of equality constraint evaluations            = 19
Number of inequality constraint evaluations          = 19
Number of equality constraint Jacobian evaluations   = 19
Number of inequality constraint Jacobian evaluations = 19
Number of Lagrangian Hessian evaluations             = 18
Total seconds in IPOPT                               = 6.471

EXIT: Optimal Solution Found.
"Execution stats: first-order stationary"

@amontoison
Copy link

amontoison commented Jan 31, 2025

I do a new release of ADNLPModels.jl this morning after JuliaSmoothOptimizers/ADNLPModels.jl#323.

Update: JuliaRegistries/General#124103

@jbcaillau
Copy link
Member Author

jbcaillau commented Jan 31, 2025

👍🏽 @amontoison same result replacing

con!(c, z) = (build(c, z, N :: Int); nothing)

by

con!(c, z) = (build(c, z, N :: Int); c)

@jbcaillau
Copy link
Member Author

nicer not to assume that the value is changed inplace and returned

@PierreMartinon
Copy link
Member

Hi there. Yes @jbcaillau , if you check the CTDirect code in docp.jl you'll see the constraints vector is explicitely returned. I added a comment some time ago to remember why :D

@jbcaillau
Copy link
Member Author

@PierreMartinon this code makes a comparison of OptimalControl.jl with a hand-coded ADNLPModels.jl Goddard:

  • first solve with OC to find a solution that will be used as a common initial guess for both solves
  • results indicate more allocations for OC, to be compared with the upcoming OC / CTDirect implementation
  • run below for N = 200
  • NB : regarding run times, the computation doesn't seem fair for ADNLP as no bounds on the unknowns are given (generic constraints are used, instead)

Image

@jbcaillau jbcaillau reopened this Jan 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants