Write table problem

68 views
Skip to first unread message

heidi

unread,
Sep 29, 2016, 5:01:02 PM9/29/16
to AMPL Modeling Language
Hi

I have written a complex model but I'm trying to learn to export a table to a  xlsx or csv file so I'm just trying to learn it using a simple problem.
 But I'm having trouble and I have looked at chapter 10 but not any luck. I am using mac os x. I have installed ampltabl.dll. but it is not working.


 
option ampl_include '.\
TABLES';

var x1;
var x2; 
maximize theobjective: x1+x2;
  subject to con1: x1<=100;
  subject to con2: x2 <=50;
solve; 
table The_table OUT "ODBC" "test.xlsx" "sheet1"
theobjective; 
write The_table;

syntax error
context:   >>> theobjective; <<< 
 


 

Robert Fourer

unread,
Sep 30, 2016, 5:36:47 PM9/30/16
to am...@googlegroups.com
It's easy to export to a csv file, by using csvdisplay instead of display. For example the command

csvdisplay Buy.lb,Buy,Buy.ub,cost >buydata.out;

produces a file buydata.out that contains these lines:

BEEF,0,0,100,3.19
CHK,0,0,100,2.59
FISH,0,0,100,2.29
HAM,0,0,100,2.89
MCH,0,46.66666666666666,100,1.89
MTL,0,1.5761812194954111e-15,100,1.99
SPG,0,8.429823983987501e-15,100,1.99
TUR,0,0,100,2.49

You can similarly redirect to a file the output of display, and of printf, which allows for detailed control over the format. Also you can write a text table with the commands

table buydata OUT: [FOOD],Buy.lb,Buy,Buy.ub,cost;
write table buydata;

which produces this file buydata.tab:

ampl.tab 1 4
FOOD Buy.lb Buy Buy.ub cost
BEEF 0 0 100 3.19
CHK 0 0 100 2.59
FISH 0 0 100 2.29
HAM 0 0 100 2.89
MCH 0 46.66666666666666 100 1.89
MTL 0 1.5761812194954111e-15 100 1.99
SPG 0 8.429823983987501e-15 100 1.99
TUR 0 0 100 2.49

For your example, to write a single value, the table statement should have the form "table ...: [], theobjective;". There's only a connection from AMPL tables to spreadsheets under Windows, however, so you won't be able to export directly to an xlsx file under MacOSX.

Bob Fourer
am...@googlegroups.com

=======
Reply all
Reply to author
Forward
0 new messages