source: coopr.pyomo/stable/2.3/coopr/pyomo/scripting/pyomo.py @ 2315

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

Merged revisions 2232-2314 via svnmerge from
https://software.sandia.gov/svn/public/coopr/coopr.pyomo/trunk

........

r2242 | wehart | 2010-02-01 20:08:36 -0700 (Mon, 01 Feb 2010) | 2 lines


Documentation update.

........

r2244 | wehart | 2010-02-01 20:10:18 -0700 (Mon, 01 Feb 2010) | 2 lines


Tagging coopr.pyomo 2.3 release

........

r2253 | wehart | 2010-02-03 13:57:16 -0700 (Wed, 03 Feb 2010) | 2 lines


Changes due to termination condition refinement

........

r2264 | wehart | 2010-02-05 08:20:30 -0700 (Fri, 05 Feb 2010) | 3 lines


Misc rework of the scripting modules. These changes allow this
functionality to be used more effectively within other Python scripts.

........

r2274 | wehart | 2010-02-06 08:13:24 -0700 (Sat, 06 Feb 2010) | 8 lines


Added the 'include' command, which can include other *.dat files. This works
like the AMPL include command, with the exception that it must be terminated by
a semicolon.


Reworked the AMPL datafile plugin to simple execute an include command
on a *.dat file. This rework has the advantage that filename/lineno information
is now propigated throught he AMPL command processing.

........

r2278 | wehart | 2010-02-07 13:06:10 -0700 (Sun, 07 Feb 2010) | 7 lines


A major rework of the parsing of data command files (formerly known
as AMPL DAT files). This rework is the first step towards a full
parse of these files. I've used PLY to lex/yacc these files. This should
resolve a variety of issues that we've observed relating to the sensitivity of
the old parser to whitespace. This is also a first step towards a careful parse
of command lines for the 'import' command.

........

r2279 | wehart | 2010-02-08 06:07:14 -0700 (Mon, 08 Feb 2010) | 2 lines


Adding support for a basic 'import' command.

........

r2283 | jwatson | 2010-02-08 11:28:22 -0700 (Mon, 08 Feb 2010) | 1 line


Added suffix option to pyomo script, to enable specification of suffixes on the command-line.

........

r2285 | wehart | 2010-02-08 11:48:54 -0700 (Mon, 08 Feb 2010) | 2 lines


Working through more examples of the 'import' command.

........

r2287 | wehart | 2010-02-08 12:12:41 -0700 (Mon, 08 Feb 2010) | 2 lines


Adding a script to test the parser.

........

r2289 | wehart | 2010-02-08 15:13:22 -0700 (Mon, 08 Feb 2010) | 4 lines


Setting up 'import' tests for the other simple examples.
These seem to work, but the internal code is too ugly. I'm
going to clean that up next to help explain what is going on...

........

r2292 | wehart | 2010-02-09 12:21:24 -0700 (Tue, 09 Feb 2010) | 10 lines


Rework of the 'import' semantics. This supports
a simple import semantics, such as:


import foobar.tab: [A,B] X Y Z;


as well as index/parameter renaming:


import foobar.tab: J=[A,B] R=X S=Y T=Z;

........

r2293 | wehart | 2010-02-09 14:22:23 -0700 (Tue, 09 Feb 2010) | 2 lines


Bug fix for dealing with MS Windows filenames.

........

r2295 | wehart | 2010-02-09 16:23:37 -0700 (Tue, 09 Feb 2010) | 4 lines


Reworking tests to do basic evaluation of the
interface to Excel spreadsheets.

........

r2302 | wehart | 2010-02-09 20:25:42 -0700 (Tue, 09 Feb 2010) | 2 lines


Adding tests for loading sets and parameters from spreadsheets.

........

r2303 | wehart | 2010-02-09 20:50:12 -0700 (Tue, 09 Feb 2010) | 2 lines


Adding a plugin for reading CSV files.

........

r2304 | wehart | 2010-02-09 20:51:27 -0700 (Tue, 09 Feb 2010) | 4 lines


Renaming csv.py to csv_table.py


The former filename masked the system 'csv' module. :P

........

r2305 | wehart | 2010-02-09 20:57:12 -0700 (Tue, 09 Feb 2010) | 2 lines


Adding CSV data files for the tutorial.

........

r2306 | wehart | 2010-02-09 20:58:42 -0700 (Tue, 09 Feb 2010) | 2 lines


Adding tests for importing CSV files.

........

r2307 | wehart | 2010-02-09 22:02:40 -0700 (Tue, 09 Feb 2010) | 3 lines


Adding a test that is used to verify that data commands can include indexed variables with astrices
in their arguments.

........

r2309 | wehart | 2010-02-10 22:06:28 -0700 (Wed, 10 Feb 2010) | 3 lines


Misc edits, which preserve some data that I was playing with to validate some
advanced parser features.

........

r2310 | wehart | 2010-02-10 22:38:44 -0700 (Wed, 10 Feb 2010) | 2 lines


Generalizing parsing behavior for set strings.

........

  • Property svn:executable set to *
File size: 8.3 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
12from optparse import OptionParser
13from coopr.pyomo import pyomo
14from coopr.opt.base import SolverFactory
15from pyutilib.misc import Options, Container
16
17import util
18
19def create_parser():
20    #
21    #
22    # Setup command-line options
23    #
24    #
25    solver_help=\
26    "This option specifies the type of solver that is used "\
27    "to solve the Pyomo model instance.  The following solver "\
28    "types are are currently supported:"
29    _tmp = SolverFactory()
30    _tmp.sort()
31    for item in _tmp:
32      if item[0] != "_":
33         solver_help += "\n.."+item
34    solver_help += ".\nThe default solver is 'glpk'."
35    debug_help=\
36    "This option is used to turn on debugging output. This option "\
37    "can be specified multiple times to turn on different debugging"\
38    "output. The following debugging options can be specified:"
39    debug_choices=[]
40    for item in pyomo.Debug:
41      if str(item) != "none":
42         debug_choices.append( str(item) )
43         debug_help += "\n.."+str(item)
44
45    parser = OptionParser()
46    parser.add_option("--solver",
47        help=solver_help,
48        action="store",
49        dest="solver",
50        type="string",
51        default="glpk")
52    parser.add_option("--path",
53        help="Give a path that is used to find the Pyomo python files",
54        action="store",
55        dest="path",
56        type="string",
57        default=".")
58    parser.add_option("--help-components",
59            help="Print information about modeling components supported by Pyomo",
60            action="store_true",
61            dest="help_components",
62            default=False)
63    parser.add_option("--debug",
64            help=debug_help,
65            action="append",
66            dest="debug",
67            choices=debug_choices)
68    parser.add_option("-k","--keepfiles",
69            help="Keep temporary files",
70            action="store_true",
71            dest="keepfiles",
72            default=False)
73    parser.add_option("--tempdir",
74            help="Specify the directory where temporary files are generated",
75            action="store",
76            dest="tempdir",
77            default=None)
78    parser.add_option("-q","--quiet",
79            help="Turn off solver output",
80            action="store_true",
81            dest="quiet",
82            default=False)
83    parser.add_option("-l","--log",
84            help="Print the solver logfile after performing optimization",
85            action="store_true",
86            dest="log",
87            default=False)
88    parser.add_option("--logfile",
89            help="Redirect output to the specified logfile",
90            action="store",
91            dest="logfile",
92            default=None)
93    parser.add_option("-s","--summary",
94            help="Summarize the final solution after performing optimization",
95            action="store_true",
96            dest="summary",
97            default=False)
98    parser.add_option("--instance-only",
99            help="Generate a model instance, and then return",
100            action="store_true",
101            dest="only_instance",
102            default=False)
103    parser.add_option("--profile",
104            help="Enable profiling of Python code.  The value of this option is the number of functions that are summarized.",
105            action="store",
106            dest="profile",
107            default=0)
108    parser.add_option("--timelimit",
109            help="Limit to the number of seconds that the solver is run",
110            action="store",
111            dest="timelimit",
112            type="int",
113            default=0)
114    parser.add_option("--postprocess",
115            help="Specify a Python module that gets executed after optimization.  If this option is specified multiple times, then the modules are executed in the specified order.",
116            action="append",
117            dest="postprocess",
118            default=[])
119    parser.add_option("--preprocess",
120            help="Specify a Python module that gets immediately executed (before the optimization model is setup).  If this option is specified multiple times, then the modules are executed in the specified order.",
121            action="append",
122            dest="preprocess",
123            default=[])
124    parser.add_option("-v","--verbose",
125            help="Make solver output verbose",
126            action="store_true",
127            dest="verbose",
128            default=False)
129    parser.add_option("--solver-options",
130            help="Options passed into the solver",
131            action="append",
132            dest="solver_options",
133            type="string",
134            default=[])
135    parser.add_option("--solver-mipgap",
136            help="The solver termination mipgap",
137            action="store",
138            dest="solver_mipgap",
139            type="float",
140            default=None)
141    parser.add_option("--solver-suffixes",
142            help="One or more solution suffixes to be extracted by the solver",
143            action="append",
144            dest="solver_suffixes",
145            type="string",
146            default=[])
147    parser.add_option("--model-name",
148            help="The name of the model object that is created in the specified Pyomo module",
149            action="store",
150            dest="model_name",
151            type="string",
152            default="model")
153    parser.add_option("--model-options",
154            help="Options passed into a create_model() function to construct the model",
155            action="append",
156            dest="model_options",
157            type="string",
158            default=[])
159    parser.add_option("--disable-gc",
160            help="Disable the garbage collecter",
161            action="store_true",
162            dest="disable_gc",
163            default=False)
164    parser.add_option("--solver-manager",
165            help="Specify the technique that is used to manage solver executions.",
166            action="store",
167            dest="smanager_type",
168            default="serial")
169    parser.add_option("--stream-output",
170            help="Stream the solver output to provide information about the solver's progress.",
171            action="store_true",
172            dest="tee",
173            default=False)
174    parser.add_option("--save-model",
175            help="Specify the filename to which the model is saved.  The suffix of this filename specifies the file format.  If debugging is on, then this defaults to writing the file 'unknown.lp'.",
176            action="store",
177            dest="save_model",
178            default=None)
179    #
180    # These options are depricated until we have a specific use-case for them
181    #
182    ##if False:
183    ##  parser.add_option("--seed",
184    ##        help="Specify a seed to derandomize the solver",
185    ##        action="store",
186    ##        dest="seed",
187    ##        type="int",
188    ##        default=0)
189    ##  parser.add_option("--first-feasible",
190    ##        help="Terminate after the first feasible incumbent",
191    ##        action="store_true",
192    ##        dest="ff",
193    ##        default=False)
194    parser.usage="pyomo [options] <model.py> [<model.dat>]"
195    #
196    return parser
197
198
199def run_pyomo(options=Options(), args=(), parser=None):
200    if options.help_components:
201        util.print_components(options)
202        return Container()
203    if not util.setup_environment(options, args):
204        return                                               #pragma:nocover
205    if not util.apply_preprocessing(options, parser, args):
206        return Container()                                   #pragma:nocover
207    model = util.create_model(options, args)
208    if options.save_model or options.only_instance:
209        return Container(instance=model.instance)
210    results, opt = util.apply_optimizer(options, model.instance)
211    if not util.process_results(options, model.instance, results, opt):
212        return Container(instance=model.instance, results=results) #pragma:nocover
213    util.apply_postprocessing(options, model.instance, results)
214    return Container(instance=model.instance, results=results)
215
216
217def run(args=None):
218    return util.run_command(run_pyomo, create_parser(), args=args, name='pyomo')
219
Note: See TracBrowser for help on using the repository browser.