wrining a CVS table with index in rows and columns

73 views
Skip to first unread message

J C

unread,
Apr 8, 2022, 8:08:48 PM4/8/22
to AMPL-group
Hello everyone!

I am traying writ a .cvs table with my solution optimization problem. I have variables in 2-dimensions, as follows:

set T; is  T = 1...10
set A; is A = 1,2,3
set B; is B = 1,2,3,4,5,6.

I have a variable v[a,t], and other variable w[b,t],
My table should be something like this:
PER    var_a1   var_a2   var_a3    var_b1    var_b2   var_b3   var_b4    var_b5   .....
1          0.95           2.89     0.25         1.25       3.36       0.05       4.58        2.98
2          0.95           2.89     0.25         1.25       3.36       0.05       4.58        2.98
3          0.95           2.89     0.25         1.25       3.36       0.05       4.58        2.98
...........................................................................................................................
10          0.95           2.89     0.25         1.25       3.36       0.05       4.58        2.98

It is not necessary to write the header text in the table.

I was trying something like this:
table table_name OUT "amplcsv" "results.csv": {t in PER} -> [t],({k in A} v[k,t]),({k in B} w[k,t]);;
But the k-indexes are wroten as columns.

I hope can help me with ideas.

Regards

AMPL Google Group

unread,
Apr 9, 2022, 11:29:04 AM4/9/22
to AMPL Modeling Language
There's a discussion of this kind of table in the subsection on Indexed collections of data columns in the AMPL book. The "table Results1" example on page 197 is closest to what you are trying to write. As an example for your situation, if the model defines "var v {A,T};" and "var w {B,T};" then the following table statement could be used to get the kind of csv file that you want:

table csv2d OUT "amplcsv" "results.csv":
   {t in T} -> [PER], {k in A} <v[k,t] ~ ("v"&k)>, {k in B} <w[k,t] ~ ("w"&k)>;

This gives the attached csv file (with some arbitrary values assigned to the variables). If you don't want the header line, add "header=false" to the table statement after "amplcsv".


--
Robert Fourer
am...@googlegroups.com
{#HS:1841701276-109515#}
results.csv

J C

unread,
Apr 9, 2022, 7:10:28 PM4/9/22
to AMPL-group
Thanks for the recommendation dear Robert.

Regards

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/ampl/reply-77152-1841701276-5428956808-1649518139-1387417690%40helpscout.net.
Reply all
Reply to author
Forward
0 new messages