Obtaining gradient and hessian after optimization

108 views
Skip to first unread message

Gaston Illanes

unread,
Aug 19, 2016, 12:36:23 PM8/19/16
to AMPL Modeling Language
Hi,

I am trying to solve an optimization problem where it would be useful to have the values of the gradient and the hessian at the optimum. How can I obtain them?

Thanks!

Gaston

Victor Zverovich

unread,
Aug 19, 2016, 6:31:14 PM8/19/16
to am...@googlegroups.com
There is an AMPL "solver" called gjh that computes the current objective gradient, constraint Jacobian matrix, and Lagrangian Hessian and writes them as AMPL params. gjh is described here: http://www.netlib.org/ampl/solvers/examples/README.gjh and it is included in the demo packages.

HTH,
Victor

--
You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ampl+uns...@googlegroups.com.
To post to this group, send email to am...@googlegroups.com.
Visit this group at https://groups.google.com/group/ampl.
For more options, visit https://groups.google.com/d/optout.

Gaston Illanes

unread,
Aug 24, 2016, 2:46:45 PM8/24/16
to AMPL Modeling Language
Hi Victor,

Thanks a lot for your reply. I'm trying to use gjh, but I am running into problems when trying to control the stub of the gjh file.

More specifically, I can solve the model using gjh, and see the /tmp/at####.gjh files being created. However, I am not running AMPL interactively, so I'd like to be able to control the stub name in order to include the appropriate output file after the model has been solved. I have included the command "write "b/tmp/mod_1_SE" before the solve command, but when executing the command "include "/tmp/mod_1_SE.gjh" I get the following error:

Error at _cmdno 6 executing "delete" command

(file /tmp/mod_1_SE.gjh, line 1, offset 0):



    Cannot delete J: it has dependents.

    use xref to see dependents, and

    use purge to delete them.

Any help getting around this problem would be greatly appreciated. As an aside, the gjh readme points readers to pages 332-333 of the AMPL book for information on how to use the write command to modify solver output. This reference is outdated, as in the latest version of the book these pages correspond to the Network Linear Programs chapter.

Thanks again!

Victor Zverovich

unread,
Aug 25, 2016, 12:37:56 PM8/25/16
to am...@googlegroups.com
What version of gjh and AMPL do you use?

- Victor

Gaston Illanes

unread,
Aug 26, 2016, 4:17:23 PM8/26/16
to AMPL Modeling Language
AMPL Version 20131213 (Linux x86_64)

And I downloaded gjh off http://www.netlib.org/ampl/solvers/examples/, not sure how to check the version

Victor Zverovich

unread,
Aug 29, 2016, 1:52:34 PM8/29/16
to AMPL Modeling Language
The netlib packages haven't been updated for a long time. I recommend using gjh which comes with the demo packages available for download from http://ampl.com/try-ampl/download-a-free-demo/

HTH,
Victor

tangi...@gmail.com

unread,
Oct 2, 2016, 1:12:53 PM10/2/16
to AMPL Modeling Language
It may be that your model contain a parameter named J.
Since the .gjh file begins with "delete" commands it tries to delete your existing parameter J.
You can solve the problem by giving another name to the output of gjh. For instance before the solve command add :
option gjh_options 'h=hessian j=jacobian g=gradient';

Gaston Illanes

unread,
Oct 3, 2016, 3:53:18 PM10/3/16
to am...@googlegroups.com

That makes a lot of sense, thanks!

I'm having trouble controlling the name of the gjh output files. I'm using the AMPL API, and trying the command:





You received this message because you are subscribed to a topic in the Google Groups "AMPL Modeling Language" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ampl/RIkQ_1004Yw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ampl+unsubscribe@googlegroups.com.

Gaston Illanes

unread,
Oct 3, 2016, 3:54:59 PM10/3/16
to am...@googlegroups.com
Apologies for the premature send.

I'm having trouble controlling the name of the gjh output files. I'd like the files to be named "results.gjh". I'm using the AMPL API, and trying the command:

ampl.eval('write "b/results"=outopt;');

does not work. I can't figure out exactly how to do this correctly. Any help would be greatly appreciated.

Best,

Gaston Illanes

Gaston Illanes

unread,
Oct 3, 2016, 4:20:55 PM10/3/16
to am...@googlegroups.com
I have made progress on the previous question, but I am still bumping into issues. I execute the following commands:

ampl.setOption('solver', gjh path goes here);
ampl.eval('write "bgjh_solution";');
ampl.setOption('gjh_options','h=hessian j=jacobian g=gradient');
ampl.solve;
ampl.eval('include gjh_solution.gjh remove gjh_solution.gjh;');
ampl.eval('display g;')

And receive the following output:

gjh: h=hessian
j=jacobian
g=gradient
gjh: "gjh_solution.gjh" written.  Execute

    include "gjh_solution.gjh"
    remove "gjh_solution.gjh";

g is not defined
Line: 1, Offset: 8, Context:
display g;

Gaston Illanes

unread,
Oct 4, 2016, 10:59:28 AM10/4/16
to AMPL Modeling Language
Found the issue: the command is ampl.eval('display gradient;'), not ampl.eval('display g;').

Thanks for all your help!
Reply all
Reply to author
Forward
0 new messages