Run ampl with python script

1,134 views
Skip to first unread message

Alberto Reuters

unread,
Jun 28, 2011, 12:08:00 PM6/28/11
to AMPL Modeling Language
Hello,

Nowadays I'm executing ampl manually and then in its environment I
type include myfile.run;. I need run my model through python script.
Does anybody know how can I send the command "include" into ampl,
after start it with python?

Thank you.

Paul

unread,
Jun 28, 2011, 6:06:41 PM6/28/11
to am...@googlegroups.com
What about having the Python script write a temporary file that looks like

model mymodel.mod;
data mydata.dat;
include myotherfile.txt;
solve;

or something along those lines, then call AMPL with that temporary file as argument?  Or pipe the equivalent to AMPL through stdin?

Paul

Robert Fourer

unread,
Jun 29, 2011, 3:59:35 PM6/29/11
to am...@googlegroups.com
The command "ampl myfile.run" initiates AMPL, includes the file myfile.run,
and then terminates after all commands in myfile.run have been executed. So
one way to run your model from Python is to use Python's os module or
subprocess module to execute "ampl myfile.run". To get results back from
AMPL you will need to place some commands in myfile.run that write one or
more files for your Python script to read.

If you have several AMPL input files as Paul suggests, you can get AMPL to
read them one at a time by running a command like "ampl mymodel.mod
mydata.dat myotherfile.txt".

Bob Fourer
4...@ampl.com

renji...@gmail.com

unread,
Dec 5, 2014, 2:12:23 AM12/5/14
to am...@googlegroups.com, 4...@ampl.com
Hello 

I try to run ampl with python. I try the following command 
os.system('ampl mymodel.mod mydata.dat myotherfile.run' )
myotherfile.run is the ampl command script.

However, I get the error information like

mydata.dat, line 1 (offset 6):
    max_nodenumber is already defined
context:  param  >>> max_nodenumber: <<< =6;

As I want to change dat file at each running, what can I do? 

Many thanks

Best,
Jing Ren


在 2011年6月30日星期四UTC+8上午3时59分35秒,AMPL Optimization写道:

victor.z...@gmail.com

unread,
Dec 5, 2014, 9:49:21 AM12/5/14
to am...@googlegroups.com, 4...@ampl.com
Hello Jing,

Make sure that mydata.dat starts with the data command that switches AMPL to the data mode:

  data;
  param max_nodenumber ...

Otherwise AMPL tries to interpret the file as a model file causing the "already defined" error.
  
HTH,
Victor

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to am...@googlegroups.com.
Visit this group at http://groups.google.com/group/ampl.
For more options, visit https://groups.google.com/d/optout.

任婧

unread,
Dec 6, 2014, 11:07:02 AM12/6/14
to am...@googlegroups.com, 4...@ampl.com
Hi Victor

Thank you so much. 

Best,
Jing


--
You received this message because you are subscribed to a topic in the Google Groups "AMPL Modeling Language" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ampl/w9dBCaCmrVA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ampl+uns...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages