<!-- Ready to Review --> ---+<a name="top"></a> Displaying and Printing in AMPL 1 [[#dp][How many decimal places?]] 1 [[#display][Displaying Information]] 1 [[#print][Printing Information]] 1 [[#file][Printing to a File]] ---++<a name="dp"></a>How many decimal places? When solving mathematical programming problems, the accuracy of your solutions should *%RED%never%ENDCOLOR%* be more than the accuracy of your data. You can set the precision that AMPL displays values via the =display_precision= option. For example, if your inputs are specified to 4 significant digits, e.g., =0.013=, you can use <pre> option display_precision 4; </pre> to get your solution values to the same number of significant digits. <img src="%ATTACHURLPATH%/four_dp.jpg" alt="four_dp.jpg" width='669' height='290' /> Note that AMPL still keeps its numbers at full precision, so you need to be careful with your objective function values. <img src="%ATTACHURLPATH%/five_dp.jpg" alt="five_dp.jpg" width='669' height='290' /> When we change the =display_precision= to 5 significant digits, the displayed =TotalCost= and the cost calculated by using the displayed =Amount= values differ. If you are using rounded solution values, make sure to *%RED%check the objective value%ENDCOLOR%* before quoting your solution. ---++<a name="display"></a> Displaying Information You have already seen how to display a variable using the =display= command. We can also display [[ExpressionsInAMPL][AMPL expressions]] the same way, e.g., we might want to see how supply we are using in a transportation problem. <img src="%ATTACHURLPATH%/display.jpg" alt="display.jpg" width='669' height='386' /> Often when we display something (like variable values) many of the resulting numbers are 0 and we are only interested in the non-zero numbers. To stop any rows of zeros being displayed you can set the =omit_zero_rows= option: <pre> option omit_zero_rows 1; </pre> To stop any columns of zeros being displayed you can set the =omit_zero_cols= option: <pre> option omit_zero_cols 1; </pre> <img src="%ATTACHURLPATH%/omit.jpg" alt="omit.jpg" width='669' height='242' /> You can also force =display= to use either tables or a single column by using the =display_1col= option. This option will use one column if the number of values to display is less than =display_1col=. The initial value of =display_1col= is 20, so any =display= command that shows less than 20 values will be displayed as a column. Setting =display_1col= to 0 forces =display= to use tables whenever possible. <img src="%ATTACHURLPATH%/display_1col.jpg" alt="display_1col.jpg" width='669' height='302' /> ---++<a name="print"></a> Printing Information By playing with the =display= options we can get the =display= command to format output in a nice way. However, we can also decide exactly what is displayed by using =print= and =printf=. <img src="%ATTACHURLPATH%/print.jpg" alt="print.jpg" width='669' height='122' /> The =print= command only writes strings to the output. The =printf= command allows you to print text and values together in a format you can control. It uses the same [[https://www.cppreference.com/stdio/printf.html][<tt>printf</tt> format]] as C and Matlab. <img src="%ATTACHURLPATH%/printf.jpg" alt="printf.jpg" width='669' height='218' /> You can print over [[SetsInAMPL][sets]] or [[SetsInAMPL#expressions][set expressions]] as well <img src="%ATTACHURLPATH%/printf_set.jpg" alt="printf_set.jpg" width='669' height='218' /> ---++<a name="file"></a> Printing to a File All the output commands can be directed to a file. Adding => <filename>= to the end of an output command creates the file with the given name and writes to it. Subsequent output commands append output to the file by adding =>> <filename>= to the commands. You should close your file when done so you can open it with another program. This is very useful for saving your solutions (in a useful format with =printf=), for example <pre> # brewery.run reset; model transportation.mod; data brewery.dat; option solver cplex; solve; print 'TRANSPORTATION SOLUTION -- Non-zero shipments' > brewery.out; display TotalCost >> brewery.out; printf {s in SUPPLY_NODES, d in DEMAND_NODES : Flow[s, d] > 0} 'Ship %d crates of beer from warehouse %s to pub %s\n', Flow[s, d], s, d >> brewery.out; close brewery.out; </pre> Running =brewery.run= in AMPL creates a file =brewery.out=: <pre> TRANSPORTATION SOLUTION -- Non-zero shipments TotalCost = 8600 Ship 300 crates of beer from warehouse A to pub 1 Ship 700 crates of beer from warehouse A to pub 5 Ship 200 crates of beer from warehouse B to pub 1 Ship 900 crates of beer from warehouse B to pub 2 Ship 1800 crates of beer from warehouse B to pub 3 Ship 200 crates of beer from warehouse B to pub 4 </pre> -- Main.MichaelOSullivan - 02 Mar 2008
This topic: OpsRes
>
WebHome
>
AMPLGuide
>
AMPLSyntax
>
PrintingInAMPL
Topic revision: r6 - 2008-03-04 - MichaelOSullivan
Copyright © 2008-2025 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki?
Send feedback