Printing Commands and Options

Printing Commands

display I, a ; print a[1] ;

We use the display and print commands to print out the value, expression or formula associated with our model entities. The main difference between display and print is that the first can have model entity collections in the list of entities to be printed, while the second can only take a list of individual model entities.

In the example above, suppose that I is a set declared with set I = 1..10; and that a is a parameter collection declared with param a{I} default 0;. With the display statement we are able to print out both the set and the collection, while with the print command we can only print the parameter indicated with the subscript [1].

printf "Objective: %f\n" , z.val ;

printf stands for print formatted. The first argument of the statement is the format of the string, which is basically a template string with missing values. The missing values are specified in the template with a % followed by a character indicating the type of value that goes there. %f means that the missing value is a real number (a floating point value), %i means it is an integer, and %s means it is a string. \n is a special sequence of characters that places a line break at that point.

After the template string comes a comma separated list of template fillers, which are the values with which the template will be filled. The first filler goes where the first missing value is, the second filler goes where the second missing value is, and so on. In the above example, supposing that z is an objective function, the command will print out into the terminal the objective's current value, e.g. Objective: 348.000000, followed by a line break.

The table below shows the possible template items types specifications:

TEMPLATE ITEM MEANING %s string %c single character %f double-precision floating-point %d or %i signed decimal notation %u unsigned decimal notation %e double-precision floating-point, exponential notation using e %E double-precision floating-point, exponential notation using E %g double-precision floating-point, using %f or %e %G double-precision floating-point, using %f or %E %q quote string appropriately for data values %Q always quote string %x unsigned hexadecimal, using abcdef, without leading 0x %X unsigned hexadecimal, using ABCDEF, without leading 0X %o unsigned octal notation, without leading 0
NOTE: In order to print a % character we use %%, which instructs the AMPL processor to interpret that symbol not as a template item, but as a literal %.

A template item can also have other specifications on how the item should be formatted, such as justification, precision, padding and width. These can indicated in the template item specification between the % and the letter that indicates its type. See below the available formatting options:

OPTION EXAMPLE Precision — .num¹ ²
printf "%.2f", pi;
Width — num¹
printf "%12f", pi;
Left justification — - (use it with width)
printf "%-12f", pi;
Zero Padding — 0 (use it with width)
printf "%012f", pi;
Force Sign — + always print number sign, no matter if it is negative or not.
printf "%+f", pi;
¹ num can be either a number or a *. If it is a *, the precision/width is specified in the list of items to be printed, just before the template filler that fills in that template item. For instance, printf "%.*f", 12, pi; is equivalent to the precision example above.
² When used with a string template item %s, a precision specification instructs the AMPL processor to print only the first num character of the string. For instance, printf "%.5s", "New York"; will print out New Y.

NOTE: We can specify multiple formatting options in a single template item. For instance, printf "%-12.2f", pi; specifies left justification, a width of 12 and a precision of 2, resulting in the output 3.14        .

Printed strings can also contain escape sequences, which are specific character sequences that have special meaning for terminal and shell programs, such as:

  • \n — new line
  • \r — carriage return
  • \t — tab
  • \a — alert bell
expand z, constraint1 ; solexpand z, constraint1 ;

The expand and solexpand commands print the expressions and formulas of objective functions and constraints in an expanded form, meaning that reduction math operations, such as sum and prod, are expanded to explicitly display the full expression that they reduce.

They can also take variables within the entities list, in which case they print the linear coeficient of the variable in all relevant constraints and objective functions, also indicating when the variable participates nonlinearly in a constraint or objective.

The difference between expand and solexpand is that solexpand prints out how the model entities appear to the solver. That means that the commands may print different values in certain circunstances, e.g. when the AMPL processor eliminates constraints in the presolve phase.

csvdisplay I, a ; _display I, a ;

The csvdisplay and _display are similar to the display command, but the values are printed in a comma separated values (csv) format.

Indexed Printing

All of the commands above can take an indexing expression, as shown below:

print {i in I : i >= 5} a[i] ;

The indexing expression instructs the the AMPL processor to iterate over all entries in the specified expression and print out the listed entities (orange part). The above statement is roughly equivalent to the for-loop below, but the code below will print each entry in a new line:

for {i in I : i >= 5} { print a[i]; }

Printing Options

Several aspects of the default behavior of the commands display, print and expand can be changed with the option command: precision, number of columns, column width, ommit zeroes, and so on. Other commands, such as solve, can also have its printing behavior affected by options.

option display_precision 2 ;

In general, the options that affect the behavior of command cmd use cmd as a prefix, e.g. display_precision. See below the available options for each command.

display OPTIONS DESCRIPTION display_1col Maximum elements for a 1D table to be displayed one element per line. display_eps Display absolute numeric values < $display_eps as zero. display_max_2d_cols If > 0, maximum data columns in a 2D display. display_precision Precision for display command when $display_round is not numeric. display_round Places past decimal for display command to round. display_transpose Transpose tables if rows − columns < $display_transpose. display_width Maximum line length for print and display commands. omit_zero_cols If nonzero, omit all-zero columns from displays. omit_zero_rows If nonzero, omit all-zero rows from displays. gutter_width Separation between columns for display command. print OPTIONS DESCRIPTION print_precision Precision for print command when $print_round is not numeric. print_round Places past decimal for print command to round. print_separator Separator for values printed by print command. expand OPTIONS DESCRIPTION expand_precision Precision for expand command when $expand_round is not numeric. expand_round Places past decimal for expand command to round. csvdisplay and _display OPTIONS DESCRIPTION csvdisplay_precision Precision for _display and csvdisplay (0 is full precision). csvdisplay_round Rounding for _display and csvdisplay ('' is full precision). csvdisplay_header If nonzero, omit headers in the csvdisplay command. OTHER OPTIONS DESCRIPTION objective_precision Precision for objective value displayed by solver. output_precision Precision used in nonlinear expression (.nl) files. solution_precision Precision for solve or solution command when $solution_round is not numeric. solution_round Places past decimal for solve or solution command to round.