source: trunk/coopr/sucasa/run_ampl.py @ 1768

Last change on this file since 1768 was 1768, checked in by wehart, 11 years ago

Rework of Coopr to use the new PyUtilib? package decomposition.

NOTE: to use Coopr with this update, we need to work with a new version of coopr_install.

File size: 3.1 KB
Line 
1#  _________________________________________________________________________
2#
3#  Coopr: A COmmon Optimization Python Repository
4#  Copyright (c) 2008 Sandia Corporation.
5#  This software is distributed under the BSD License.
6#  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
7#  the U.S. Government retains certain rights in this software.
8#  For more information, see the Coopr README.txt file.
9#  _________________________________________________________________________
10
11
12import os
13from string import Template
14import pyutilib.subprocess
15
16
17ampl_script=Template("""
18##
19## AMPL script that creates ${app_label}.nl (or ${app_label}.mps),
20## ${app_label}.col, ${app_label}.row and ${app_label}.val
21##
22## The output of this script is used to create ${app_label}.val
23##
24#
25# Read in the model and data files
26#
27model ${model_file} ;
28${data_line}
29#
30# Tells ampl to create *.row and *.col files
31#
32option auxfiles rc;
33#
34# Manage whether AMPL preprocesses the model.
35#
36option presolve ${presolve};
37#
38# Write out an *.nl or *.mps file
39#
40write ${file_type}${app_label};
41#
42# Print end-of-job tag
43#
44print "# SUCCESSFUL_TERMINATION";
45""")
46
47
48def run_ampl(symbols=[], app_label="undefined", model_file="undefined", data_file="undefined", presolve=0, keepFiles=False, using_mps=False, only_script=False):
49    """
50    Run AMPL on a customized script
51    """
52    print "  . Running AMPL to generate application instance"
53    if using_mps is False:
54        file_type="b"
55    else:
56        file_type="m"
57    if presolve == True:
58        presolve=1
59    elif presolve == False:
60        presolve=0
61    if data_file is None:
62        data_line="#"
63    else:
64        data_line=" data "+data_file+";"
65    cmd = ampl_script.substitute(app_label=app_label, model_file=model_file, data_line=data_line, presolve=presolve, file_type=file_type)
66    for symbol in symbols:
67        cmd += "print \""+symbol+"\";"
68        cmd += "_display "+symbol+";"
69        cmd += "print \";\";"
70    OUTPUT=open("ampl_script.in","w")
71    print >>OUTPUT, cmd
72    OUTPUT.close()
73    #
74    # Return if the script does not need to be executed
75    #
76    if only_script:
77        return
78    #
79    # Run AMPL
80    #
81    print "  . Running AMPL ...",
82    pyutilib.subprocess.run("ampl ampl_script.in",app_label+".val")
83    print "done."
84    #
85    # Check for errors
86    #
87    errmsg="      SUCASA ran 'ampl ampl_script.in', and the output is in '"+app_label+".val'"
88    if not "SUCCESSFUL_TERMINATION" in file(app_label+".val").read():
89        print errmsg
90        raise IOError, "ERROR: sucasa called AMPL, which failed to terminate successfully!"
91    if "Error executing" in file(app_label+".val").read():
92        print errmsg
93        raise IOError, "ERROR: sucasa called AMPL, which had an error executing a command!"
94    if using_mps:
95        if not os.path.exists(app_label+".mps"):
96            print errmsg
97            raise IOError, "ERROR: sucasa failed to generate MPS file for "+app_label+" using AMPL!"
98    elif not os.path.exists(app_label+".nl"):
99        print errmsg
100        raise IOError, "ERROR: sucasa failed to generate NL file for "+app_label+" using AMPL!"
101    if not keepFiles:
102        os.remove("ampl_script.in")
Note: See TracBrowser for help on using the repository browser.