Nonlinear constraint parsing from .nl file

154 views
Skip to first unread message

Alan Delgado de Oliveira

unread,
Oct 4, 2017, 9:50:56 AM10/4/17
to AMPL Modeling Language
Hello Dear,

I am working with the ASL library to parser a .nl file in C++. I followed the example in lin3 file with success (http://netlib.sandia.gov/ampl/solvers/examples/lin3.c). However, my model has some constraints that are nonlinar. In that example, the constraints are defined using the vectors A_colstarts, A_rownos and A_vals that store the linear Jacobian coefficients. Hence, when I read my model the nonlinear part is dropped. I saw in documentation ("Hooking your solver to AMPL) that there is a way to read nonlinear problems using Cgrad offset, but I could not do that. The closest example that I found was the file amplfunc.c (http://netlib.sandia.gov/ampl/solvers/examples/amplfunc.c) and amplfun4.c (http://netlib.sandia.gov/ampl/solvers/examples/amplfun4.c). Would someone give a example how to use the conval and jacval functions to read these contraints throught column-wise approach?

Any help would be much appreciated.

Robert Fourer

unread,
Oct 5, 2017, 1:40:00 PM10/5/17
to am...@googlegroups.com
Since nonlinear functions usually have expressions -- like "x[1]/x[2]" or "y[1]*log(y[2])" -- that cannot be separated into terms that involve only one variable, AMPL can only provide row-wise descriptions of nonlinear objectives and constraints. There is no way to get a column-wise description that describes a nonlinear problem one variable at a time.

If your solver only needs to evaluate the problem's nonlinear functions and derivatives at specific points, then you should follow the section on "Reading nonlinear problems" beginning on page 11 of "Hooking Your Solver to AMPL". However if your solver needs to actually know what is in each nonlinear expression, then your interface will have to walk the expression trees; this is done for a number of "alternative" solvers and I can find you a small example if that will help.

Bob Fourer
am...@googlegroups.com

=======

Alan Delgado de Oliveira

unread,
Oct 5, 2017, 6:27:58 PM10/5/17
to AMPL Modeling Language
Mr Fourer,

thank you very much for your answer. I changed my approach and recode the model to a row-wise description. I have the following constraint:

-x1*lambda1 -1.15y1 - 0.05*y1*gamma12 + lambda1 in AMPL enviromnment.

When, for instance, I walk throught the cg offset the varno identifies the variable gamma12 in (- 0.05*y1*gamma12), but the value from cg->coef is zero. Therefore, I opened the file with the ASL_pfgh reader (using the library asl_pfgh.h), I executed the function jacval to store the jacobian matrix in a vector called "g". I checked g[cg->goff] but this value is zero too. 

My question is: how can I get the variable y1 and the coefficient -0.05 to comprise this constraint?

Sir, I don't need to  evaluate the values in these points. I only need to read the constraints.

Best,
Alan Delgado

Robert Fourer

unread,
Oct 11, 2017, 7:39:50 PM10/11/17
to am...@googlegroups.com
It doesn't seem that jacval is what you want here. The jacobian matrix contains values of the partial derivatives of the constraint functions, evaluated at a given point. If y1 and gamma12 are both variables, then the jacobian will contain the partials -0.05*gamma12 and -0.05*y1 evaluated at the current values of y1 and gamma12, but in general you won't find -0.05 there. (Probably you called jacval when all the variables were at zero, and hence these jacobian values were also zero.)

If you want access to the actual nonlinear constraint functions using the ASL routines, you will need to process the expression trees that the ASL reader sets up. This is not easy, as there is no documentation for using the ASL data structures in this way. I have some example code I can provide to get you started, but it's not supported and to figure it out you'll need to be familiar with C structures and programming.

Alternatively if your nonlinear functions have only a few simple forms, you might be able to create your own reader for the nl files that AMPL writes. The commands

option nl_comments 1;
write gtest;

will write a text file test.nl with some comments to help you understand it (perhaps with the help of the attached report). Possibly you could make use of the C++ NL Reader project that is described at https://ampl.github.io/nl-reader.html. Unlike ASL it has documented methods for dealing with nonlinear functions. However this is an experimental reader and so (unlike ASL) it not supported.
Writing .nl Files.pdf

Alan Delgado de Oliveira

unread,
Oct 16, 2017, 11:32:55 AM10/16/17
to AMPL Modeling Language
Mr Fourer,

thank you very much for your support. It has been helpfull. I got it. I made throught the following way:

With the expand command I checked which should be the coefficients from the nonlinear constraints. After that, I identify them directly in the nl file. In this way, I  coded routines to handle these string and added these coefficients in my model. 

again thank you very much, sir.

Best, 
Alan Delgado
Reply all
Reply to author
Forward
0 new messages