NL file with a user-defined function

73 views
Skip to first unread message

Eric

unread,
Mar 16, 2022, 10:36:25 PM3/16/22
to AMPL Modeling Language
Hello,

My AMPL model contains a user-defined function. I can't find a way to write it correctly into the NL format:

For example if Test.mod contains the following lines:

load amplgsl.dll;
function gsl_log1p;

the output file of "ampl -ogTest Test.mod Test.dat" does not contain any information on where to find the function.

For exemple, passing this to lgo I get:

"function gsl_log1p not available".

Do I miss something about the NL format?

Thanks a lot!

Eric

AMPL Google Group

unread,
Mar 17, 2022, 12:30:38 PM3/17/22
to AMPL Modeling Language
It turns out that there are several issues here.

First, if gsl_log1p is not applied to any variable that is being sent to the solver, then it will not be mentioned in the nl file. (For instance, it might only be applied to a parameter.) But otherwise you will see a line like "F0 0 1 gsl_log1p" and other lines like "f0 1" where the function is used.

Second, when running a solver separately from AMPL, you need to set the system environment variable ampl_funclibs to the location of amplgsl.dll. For example, on a Windows computer this would be done with a command like this:

set ampl_funclibs='C:\Users\AMPL\Desktop\Solvers\amplgsl.dll'

Finally, some versions of LGO for AMPL have a bug that prevents them from working with the latest version of amplgsl.dll. If you see the error message "function gsl_sort: ftype = 16; expected 0 or 1" when attempting to use amplgsl.dll, then use this older version instead:

https://ampl.com/dl/amplgsl/amplgsl20181224.zip


--
Robert Fourer
am...@googlegroups.com
{#HS:1818230948-109148#}

Charles Chaa

unread,
Mar 18, 2022, 1:12:24 PM3/18/22
to AMPL Modeling Language
Hello,

Thanks a lot for your really nice answer!

The rest of my Test.mod file actually calls the user-defined function. The problem whas indeed that I did not set the ampl_funclibs variable. Now it works well with the lgo solver !

I might have another problem though but with lindoglobal optimizer. I have tried to solve my Test model and launched the lindoglobal solver from AMPL and I get : 

" Bad NL Format 
Errorcode=2056: A user-defined function has not been set yet"

I have not been able to find any information on this error code in the lindoglobal documentation.

I get the same result when I try to launch lindoglobal directly with the nl file (and the variable set, and the -i option to be sure...).

Thanks a lot ! 

P.S : If I can make use of your experise on that, from what I understand, LGO and lindoglobal are one of the fiew black box NLP non convex global optimizer using branch and bound techniques. Because of the black box of course I can get no guarantee on optimality from the solver with a branch and bound technique, but actually I know how my fucntion looks (12 variables) and it's not that bad, it has a lot of local minima's but it's differentiable almost everywhere. I use a numerical library to compute de differentiate of my objective (user-defined) and from what I understand, it will depend on the optimizer to use the differentiate or not, or to compute it numerically with it's own routine.

My question is, do you know any other that I could try? LGO doesn't work that well for my problem, even after a lot of tweaking of its parameters I get a solution that is quite far from what other library such as NLOPT ISRES gives me. I like the branch and bound approch (which is not the way ISRES works) but LGO comes with a lot of limitation in that sense (either I get limited because one of the partition get too small and lgo stops so I decide to linearly rescale my problem but then lgo is hardwire limited to the number of partition and this becomes the limiting factor).

Cheers,
Eric

AMPL Google Group

unread,
Mar 21, 2022, 9:09:14 AM3/21/22
to AMPL Modeling Language
AMPL works with several global mixed-integer nonlinear solvers based on branch-and-bound: BARON, LindoGlobal, Octeract, and Couenne. However, all of them are limited to using a subset of standard operators and functions -- like ^ (power), log, and cos -- that have been chosen in advance. Thus they reject user-defined functions, even ones that have derivatives available.

LGO is a continuous nonlinear solver that combines local and global (branching) approaches. With its different algorithmic approach, it is able to accommodate user-defined functions, but also it has some limitations as you have seen.

Knitro, a local mixed-integer nonlinear solver, accepts user-defined functions, and if you have time for multiple solves, it has a multi-start feature that automatically tries different starting points to try to find better solutions.


--
Robert Fourer
am...@googlegroups.com
{#HS:1818230948-109148#}
On Fri, Mar 18, 2022 at 5:12 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Hello,

Thanks a lot for your really nice answer!

The rest of my Test.mod file actually calls the user-defined function. The problem whas indeed that I did not set the ampl_funclibs variable. Now it works well with the lgo solver !

I might have another problem though but with lindoglobal optimizer. I have tried to solve my Test model and launched the lindoglobal solver from AMPL and I get :

" Bad NL Format
Errorcode=2056: A user-defined function has not been set yet"

I have not been able to find any information on this error code in the lindoglobal documentation.

I get the same result when I try to launch lindoglobal directly with the nl file (and the variable set, and the -i option to be sure...).

Thanks a lot !

P.S : If I can make use of your experise on that, from what I understand, LGO and lindoglobal are one of the fiew black box NLP non convex global optimizer using branch and bound techniques. Because of the black box of course I can get no guarantee on optimality from the solver with a branch and bound technique, but actually I know how my fucntion looks (12 variables) and it's not that bad, it has a lot of local minima's but it's differentiable almost everywhere. I use a numerical library to compute de differentiate of my objective (user-defined) and from what I understand, it will depend on the optimizer to use the differentiate or not, or to compute it numerically with it's own routine.

My question is, do you know any other that I could try? LGO doesn't work that well for my problem, even after a lot of tweaking of its parameters I get a solution that is quite far from what other library such as NLOPT ISRES gives me. I like the branch and bound approch (which is not the way ISRES works) but LGO comes with a lot of limitation in that sense (either I get limited because one of the partition get too small and lgo stops so I decide to linearly rescale my problem but then lgo is hardwire limited to the number of partition and this becomes the limiting factor).

Cheers,
Eric
On Thu, Mar 17, 2022 at 4:30 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
It turns out that there are several issues here.

First, if gsl_log1p is not applied to any variable that is being sent to the solver, then it will not be mentioned in the nl file. (For instance, it might only be applied to a parameter.) But otherwise you will see a line like "F0 0 1 gsl_log1p" and other lines like "f0 1" where the function is used.

Second, when running a solver separately from AMPL, you need to set the system environment variable ampl_funclibs to the location of amplgsl.dll. For example, on a Windows computer this would be done with a command like this:

set ampl_funclibs='C:\Users\AMPL\Desktop\Solvers\amplgsl.dll'

Finally, some versions of LGO for AMPL have a bug that prevents them from working with the latest version of amplgsl.dll. If you see the error message "function gsl_sort: ftype = 16; expected 0 or 1" when attempting to use amplgsl.dll, then use this older version instead:

https://ampl.com/dl/amplgsl/amplgsl20181224.zip


--
Robert Fourer
am...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages