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

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

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

........

r2399 | wehart | 2010-02-24 11:36:56 -0700 (Wed, 24 Feb 2010) | 2 lines


Adding a work-around when pstats cannot be imported.

........

r2400 | wehart | 2010-02-24 14:51:08 -0700 (Wed, 24 Feb 2010) | 2 lines


Misc documentation updates.

........

r2408 | wehart | 2010-02-28 05:41:04 -0700 (Sun, 28 Feb 2010) | 3 lines


Allow an earlier version of Python. I'm still not sure this is a good idea,
but it's necessary for Jython.

........

r2409 | wehart | 2010-02-28 05:42:33 -0700 (Sun, 28 Feb 2010) | 7 lines


Rework of profiling imports.


Refinement of Pyomo command-line parsing, which is more
specific now.


Adding automatic import of pyomo to package.

........

r2415 | jwatson | 2010-03-08 20:53:13 -0700 (Mon, 08 Mar 2010) | 3 lines


Significantly improved performance of PyomoModel? _clear_attribute method, mainly by eliminate unnecessary calls to it through _setattr_exec_.

........

r2416 | jwatson | 2010-03-09 16:45:22 -0700 (Tue, 09 Mar 2010) | 5 lines


Modified the AMPL dat file parser to instantiate the lexer and yaccer objects only once, for the lifetime of the module.


They were being inadvertently created at each invocation, which pyomo users wouldn't notice. But PySP users, who can be creating 1K or more instances, do!

........

r2422 | wehart | 2010-03-11 16:01:09 -0700 (Thu, 11 Mar 2010) | 3 lines


Rework of unit tests to (a) import pyutilib.th as 'unittest' and
(b) employ test skipping.

........

r2430 | wehart | 2010-03-11 23:38:22 -0700 (Thu, 11 Mar 2010) | 2 lines


Reworking class decorators.

........

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