Write Non-Zeros?

Is there syntax for writing only non-zero values to a database file from the solution? I can’t figure it out from the documentation.

Here’s an example to get you started. Suppose that you have solved the problem given by the attached files transp.mod and transp.dat. Then you want to write to a database only the nonzero values of the variable:

var Trans {ORIG, DEST} >= 0;

You can do it using the following table statement:

table TransTab OUT "transp.tab":
  {i in ORIG, j in DEST: Trans[i,j] > 0} -> [From,To], Trans[i,j];

Then the AMPL statement “write table TransTab;” will write the following to the file transp.tab:

ampl.tab 2 1
From	To	'Trans[i,j]'
GARY	FRE	1100
GARY	LAF	300.00000000000034
CLEV	DET	1200
CLEV	LAN	600
CLEV	WIN	400
CLEV	LAF	399.9999999999998
PITT	FRA	900.0000000000008
PITT	STL	1699.9999999999995
PITT	LAF	300.00000000000006

To write to an actual database file, instead of a .tab file as in this example, you’ll need to adjust the table statement to reference the database that you’re using.