Passing arguments to ampl script from linux prompt

1,376 views
Skip to first unread message

Justin

unread,
May 7, 2008, 10:59:10 PM5/7/08
to AMPL Modeling Language
I would like to pass arguments to an ampl script from a linux command
prompt. For example:

ampl myscript.run datafile.dat

In other words, I would like to pass the dat file 'datafile.dat' to
the script 'myscript.run'. The functionality I am looking for is
similar to argv in C or python.

Thank you,

Justin Goodson

Robert Fourer

unread,
May 9, 2008, 11:58:31 PM5/9/08
to am...@googlegroups.com, Justin
Justin,

That's exactly how it is done:

ampl [options] [file [file...]]

For a list of options give the command

ampl -?

Bob Fourer
4...@ampl.com

Justin

unread,
May 12, 2008, 10:52:06 AM5/12/08
to AMPL Modeling Language
Thank you for your reply. I should have been a little more specific.
In addition to what you mentioned, I would like to set certain
parameter values from a linux prompt. For example, suppose my run
file uses a parameter 'T'. Can I pass a value for T from the linux
prompt to the run file? Something like this:

ampl myscript.run 5

Now, in 'myscript.run' I would somehow set 'T' to 5. Thank you for
any additional information you may have.

-Justin

Robert Fourer

unread,
May 12, 2008, 7:01:51 PM5/12/08
to am...@googlegroups.com, Justin
Justin,

There's no way to pass the value of T through an ampl command-line argument,
but a value can be passed through an environment variable. Here's how I
would do it from a Windows DOS console:

C:\>set Tvalue=5

C:\>ampl
ampl: param T integer > 0;
ampl: let T := num($Tvalue);

ampl: display T;
T = 5

Environment variables become option strings inside of AMPL, and $Tvalue
refers to the value of option Tvalue. Something similar would work under
linux, depending on the shell you are using.

Akshay

unread,
Mar 31, 2011, 12:07:50 AM3/31/11
to am...@googlegroups.com, Justin, 4...@ampl.com
I was wondering about the original question Justin asked: Is it possible to pass the name of a data file as a command line argument ?

I tried doing: ampl example.run example.dat

AMPL seemed to be reading the commands in the .run file but it did not read the data file.

Robert Fourer

unread,
Mar 31, 2011, 9:09:18 AM3/31/11
to am...@googlegroups.com, Justin

Without seeing the files I can't be entirely sure what is happening here.  However I can say that when you give the statement

 

   ampl example.run example.dat

 

AMPL first reads and processes all of the file example.run, and then reads and processes all of the files example.dat.  So I would expect that by the time the data file is read, all the commands in the run file have already been executed, and AMPL receives no instructions for doing anything with the data (such as solving a problem).

 

You could of course put "data example.dat" inside the run file, and then execute only "ampl example.run".  But if you need to control the choice of data file from the command line, then you will need to use a command like

 

   ampl example1.run example.dat example2.run

 

where the run file has been broken into two parts, one with the commands to be executed before reading the data, and one with the commands to be executed after reading the data.

 

Bob Fourer

4...@ampl.com

 

 

From: am...@googlegroups.com [mailto:am...@googlegroups.com]

On Behalf Of Akshay [aksha...@gmail.com]
Sent: Wednesday, March 30, 2011 11:08 PM
To: am...@googlegroups.com
Cc: 'Justin'; 4...@ampl.com
Subject: Re: RE: [AMPL 4507] Passing arguments to ampl script from linux prompt

Reply all
Reply to author
Forward
0 new messages