Invoke AMPL from Python

680 views
Skip to first unread message

Vaibhav Rungta

unread,
Apr 20, 2017, 12:51:24 PM4/20/17
to am...@googlegroups.com, 4...@ampl.com
Hello Folks,
I want to invoke an AMPL script from Python.

The AMPL folder I am using has ampl.exe, minos.exe, the run and mod files.
The python program has been saved at other location.

I checked the support available online. It suggests use of "ampl name_of_run_file.run" with subprocess.call but I am getting an error when I include path along with file name.

For once I tried saving the python file in same folder as the run file and os.startfile. That works. But I wish to keep the python file in separate folder and call the ampl from there. Please suggest.

P.S: I am a beginner with Python. Still learning.

Regards,
Vaibhav Rungta
Graduate Student - Industrial and Systems Engineering
Graduate Assistant - Toyota Production Systems Lab
Research Assistant - University Transport Research Center

Filipe Brandão

unread,
Apr 21, 2017, 8:26:41 AM4/21/17
to AMPL Modeling Language, 4...@ampl.com, vr2...@rit.edu
Hi Vaibhav,

If you set the environment variable ampl_include (with os.environ["ampl_include"] = "path/to/folder"), AMPL will search for files in the other folder too and the script should work.

from __future__ import print_function
from subprocess import Popen, PIPE
import os
os
.environ["ampl_include"] = "models/"

process
= Popen(["ampl", "models/model.run"], stdin=PIPE, stdout=PIPE)
for line in process.stdout:
   
print(line, end="")

By the way, a Python API for AMPL is planned to be included in the next released of the AMPL API.

Best regards,
Filipe

Vaibhav Rungta

unread,
Apr 21, 2017, 10:44:48 AM4/21/17
to Filipe Brandão, AMPL Modeling Language, 4...@ampl.com
Hello Filipe,
I'll try the steps you have recommended and get back to you in case of any query. 
Thank you for the much needed help :)

Regards,
Vaibhav Rungta
Graduate Student - Industrial and Systems Engineering
Graduate Assistant - Toyota Production Systems Lab
Research Assistant - University Transport Research Center


Filipe Brandão

unread,
Apr 21, 2017, 12:27:11 PM4/21/17
to vr2...@rit.edu, AMPL Modeling Language, Robert Fourer
Hi Vaibhav,

Since you have spaces in the path, you may need to use raw strings instead (i.e., r"C:/Users/Public/Music/Sample Music/AMPL/ampl_mswin64/" instead of "C:/Users/Public/Music/Sample Music/AMPL/ampl_mswin64/").

Best regards,
Filipe


On Fri, Apr 21, 2017 at 5:10 PM, Vaibhav Rungta <vr2...@rit.edu> wrote:
Hello Filipe,
I used the code shared above and declared environment variable.
I am still getting an error (screenshot attached herewith). I might have done something wrong. I'll appreciate your help. Thanks.

Regards,
Vaibhav Rungta
Graduate Student - Industrial and Systems Engineering
Graduate Assistant - Toyota Production Systems Lab
Research Assistant - University Transport Research Center


Vaibhav Rungta

unread,
Apr 21, 2017, 12:32:10 PM4/21/17
to Filipe Brandão, AMPL Modeling Language, 4...@ampl.com
Hello Filipe,
I used the code shared above and declared environment variable.
I am still getting an error (screenshot attached herewith). I might have done something wrong. I'll appreciate your help. Thanks.
Regards,
Vaibhav Rungta
Graduate Student - Industrial and Systems Engineering
Graduate Assistant - Toyota Production Systems Lab
Research Assistant - University Transport Research Center


On Fri, Apr 21, 2017 at 9:37 AM, Vaibhav Rungta <vr2...@g.rit.edu> wrote:
Error on executing the code.pdf

Vaibhav Rungta

unread,
Apr 27, 2017, 7:56:11 PM4/27/17
to Filipe Brandão, AMPL Modeling Language, Robert Fourer
Hello Filipe,
I tried your your recommendation. I couldn't get it to work.

Therefore, I moved all the AMPL files to the folder which has Python files. It worked for sometime but for some reason (I don't know why) the Python program giving an error of "data file not found", again. Python file attached here with.

- Creates .dat file through Python. Similar to writing a text file. The .dat file gets overwritten and is called in .run file.
- I have also included a 3 line script in the .run file to write output (just capturing final speed and acceleration time) to a .txt file.
- Reads the .txt file from Python and saves the values for use in SUMO.

I can't figure out what's wrong. I deleted the old .exe files and extracted all the files again in the same folder but I still can't get it to work. Screen shot attached.


Regards,
Vaibhav Rungta
Graduate Student - Industrial and Systems Engineering
Graduate Assistant - Toyota Production Systems Lab
Research Assistant - University Transport Research Center


call_ampl.py
Can’t find file in the same folder.pptx

Filipe Brandão

unread,
Apr 28, 2017, 8:28:05 AM4/28/17
to AMPL Modeling Language, fdabr...@gmail.com, 4...@ampl.com, vr2...@rit.edu
Hi Vaibhav,

The only problem that I noticed with your code was that subprocess.call("ampl deccrun_new.run") should be replaced subprocess.call(["ampl", "deccrun_new.run"]) (https://docs.python.org/2/library/subprocess.html).

Regarding the screen-shot, could you please run "cd;" in AMPL after that error in order to check the current working of AMPL?

Best regards,
Filipe

Vaibhav Rungta

unread,
May 1, 2017, 12:57:02 PM5/1/17
to Filipe Brandão, AMPL Modeling Language, Robert Fourer
Hello Filipe,
I checked the ampl.exe with cd; command. It shows the existing work directory where all the other .mod, .dat and .run files are placed.
I also moved the files to another folder and have deleted the ampl files and copied again. Still the same error. Attached herewith are some screenshots which might provide  more information.
You have been very supportive. 
I have used ampl in past but I never came across this issue. I have tried multiple things but it even the most basic operations aren't working. I'll check for Python once I fix this. Kindly suggest.

Regards,
Vaibhav Rungta
Graduate Student - Industrial and Systems Engineering
Graduate Assistant - Toyota Production Systems Lab
Research Assistant - University Transport Research Center


Checked ampl with ‘cd.pptx

AMPL Support

unread,
May 1, 2017, 11:06:23 PM5/1/17
to am...@googlegroups.com

Hi Vaibhav,

On your second slide, it appears that your file is named deccrun_new1.run but you have given the command "include decc_new1.run" -- that would explain why the file can't be found.  It is not immediately clear why, on the first slide, data_veh.dat is not being found, so we suggest some more troubleshooting steps.

First, move the folder ampl.mswin64 (shown in the second slide) to your desktop, and try running AMPL from there.  If you still have a problem, send us again the screenshots showing the output of cd and the error, and a screenshot of the folder display -- it will be a normal Windows folder display rather than the Music Library display that we are currently seeing.  Also attach the model, data, and run files that you are trying to use in your test.

AMPL Support Services
sup...@ampl.com


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

John

unread,
Jun 19, 2020, 10:33:46 AM6/19/20
to AMPL Modeling Language
Hi!!!

I am a bit late for the conversation but I have got the same problem: dat afile is not found.
As I see python manages to run AMPL.exe and read mydatafile.run
but when running * .run file the data file is not found.
I use thefollowing code in python:

process = Popen([pathToamplExe, patheToRunFile], stdin=PIPE, stdout=PIPE, shell=True)

for line in process.stdout:
print(line, end="")
   print("\n")

The message is:
b'C:\\Users\\yauhenik\\OneDrive\\Python\\Combinator_Web\\CombinatorHybridWork\\CombinatorHybrid.run, line 125 (offset 7655):\r\n'

b'\tCan\'t find  file "CombinatorOriginalHybrid.dat"\r\n'

b'context:  data  >>> CombinatorOriginalHybrid.dat; <<< \r\r\n'

вторник, 2 мая 2017 г., 5:06:23 UTC+2 пользователь AMPL Support написал:

To unsubscribe from this group and stop receiving emails from it, send an email to ampl+unsubscribe@googlegroups.com.

AMPL Google Group

unread,
Jun 21, 2020, 9:55:36 AM6/21/20
to AMPL Modeling Language
You missed the line that sets the environment variable ampl_include to the directory containing the model files. The following should work:


from subprocess import Popen, PIPE
import os

pathToamplExe = r'C:/path/to/ampl'
patheToRunFile = r'C:/path/to/models/file.run'

os.environ["ampl_include"] = r'C:/path/to/models/'
process = Popen([pathToamplExe, patheToRunFile], stdin=PIPE, stdout=PIPE)

for line in process.stdout:
print(line, end="")
print("\n")


Please note however, that there is a Python API for AMPL called amplpy. You may want to use amplpy instead (see, e.g., https://amplpy.readthedocs.io/en/latest/quick-start.html) as it will simplify the interaction between Python and AMPL.



--
Filipe Brandão
am...@googlegroups.com
{#HS:1199891284-80834#}

To unsubscribe from this group and stop receiving emails from it, send an email to ampl+uns...@googlegroups.com.


To post to this group, send email to am...@googlegroups.com.
Visit this group at https://groups.google.com/group/ampl.
For more options, visit https://groups.google.com/d/optout.

--

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/0cad4c8a-5c2e-40e7-9962-b48c99925502o%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages