TRY NOW!
AMPL > >Displays and Results > >How do I list all variables that have a certain property in the optimal solution?

The built-in parameter _nvars contains the number of variables; a symbolic parameter _varname[j] contains the AMPL name of the jth variable; and _var[j] is a “synonym” that refers to the jth variable. Thus to show the names of all variables that are below their upper bounds at the optimum, for example, you can write:

ampl: display {j in 1.._nvars:
ampl?   _var[j] < _var[j].ub - 0.00001} _varname[j];
_varname[j] [*] :=
2  "PD_Ship['SE']"
5  "DW_Ship['NE','BWI']"
6  "DW_Ship['SE','EWR']"
7  "DW_Ship['SE','BWI']"
;

This is only one of the uses for AMPL’s generic synonyms for variables, constraints and objectives. See the discussion of AMPL’s generic names in Section A.19.4 of the reference appendix to the AMPL book.

Posted in: Displays and Results