Import a set from excel

459 views
Skip to first unread message

Sebastián Varas Kittel

unread,
Oct 28, 2014, 12:44:59 PM10/28/14
to am...@googlegroups.com

Hello,

In the code below, I'm importing the data for the parameter C_link from excel...and it's working well. But now I want to define the sets ORIG and DEST in the excel file and then import them to AMPL. In that way, I won't have to write "{"n1","n2","n3","n4","n5","n6"}" in the .mod file. Is this possible? How can I do this?

 

Thank you in advance for your help.


---------------------------------------------------------------------


reset;


set ORIG={"n1","n2","n3","n4","n5","n6"};

set DEST={"n1","n2","n3","n4","n5","n6"};

set link within {ORIG,DEST};


param C_link{link}>=0;


table LINKS IN "ODBC" "BBDD_Modelo_Basico_v00.xls" "LINKS":

link<-[ORIG,DEST], C_link;

read table LINKS;


var x{link}>=0;


Robert Fourer

unread,
Oct 29, 2014, 10:19:40 PM10/29/14
to am...@googlegroups.com
You can create a range named, say, Origins in your spreadsheet, consisting of one column having the following cells:

ORIG
n1
n2
n3
n4
n5
n6

Then use a table statement like this:

table orig IN "ODBC" "BBDD_Modelo_Basico_v00.xls" "Origins": ORIG <- [ORIG];

Subsequently "read table orig;" will read just the origins into the set ORIG, and you can do something similar for the destinations.

Alternatively you can define ORIG and DEST in terms of what is found in the link set:

set LINKS dimen 2;
set ORIG = setof {(i,j) in LINKS} i;
set DEST = setof {(i,j) in LINKS} j;

However then you will not get any error message if an origin or destination name is mis-typed in the link listing.

Bob Fourer
am...@googlegroups.com

=======

From: am...@googlegroups.com [mailto:am...@googlegroups.com]
On Behalf Of Sebastián Varas Kittel
Sent: Tuesday, October 28, 2014 11:45 AM
To: am...@googlegroups.com
Subject: [AMPL 9402] Import a set from excel

In the code below, I'm importing the data for the parameter C_link from excel...and it's working well. But now I want to define the sets ORIG and DEST in the excel file and then import them to AMPL. In that way, I won't have to write "{"n1","n2","n3","n4","n5","n6"}" in the .mod file. Is this possible? How can I do this?

---------------------------------------------------------------------

Sebastián Varas Kittel

unread,
Nov 2, 2014, 6:21:03 PM11/2/14
to am...@googlegroups.com
Thank you! It works!

Regards,
SVK.
Reply all
Reply to author
Forward
0 new messages