source: coopr.pyomo/trunk/coopr/pyomo/scripting/pyomo.py @ 3681

Last change on this file since 3681 was 3681, checked in by wehart, 9 years ago

Migrating the profiling logic into scripting/util.py.
This allows it to be applied uniformly in the conversion scripts:
pyomo2nl and pyomo2lp.

  • Property svn:executable set to *
File size: 11.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
12import argparse
13from coopr.opt.base import SolverFactory
14from coopr.opt.parallel import SolverManagerFactory
15from pyutilib.misc import Options, Container
16
17try:
18   from pympler.muppy import muppy
19   from pympler.muppy import summary
20   from pympler.muppy import tracker
21   from pympler.asizeof import *
22   pympler_available = True
23except ImportError:
24   pympler_available = False
25
26import util
27
28
29def add_model_group(parser):
30    group = parser.add_argument_group('Model Options')
31
32    group.add_argument('--preprocess', '--load',
33        help='Specify a Python module that gets immediately executed (before '\
34             'the optimization model is setup).  If this option is specified '\
35             'multiple times, then the modules are executed in the specified '\
36             'order.',
37        action='append',
38        dest='preprocess',
39        default=[])
40    group.add_argument('--model-name',
41        help='The name of the model object that is created in the specified ' \
42             'Pyomo module',
43        action='store',
44        dest='model_name',
45        default='model')
46    group.add_argument('--model-options',
47        help='Options passed into a create_model() function to construct the '\
48             'model',
49        action='append',
50        dest='model_options',
51        default=[])
52    group.add_argument("--flatten-expressions", "--linearize-expressions",
53        help="EXPERIMENTAL: An option intended for use on linear or mixed-integer models " \
54             "in which expression trees in a model (constraints or objectives) are compacted " \
55             "into a more memory-efficient and concise form. The trees themselves are eliminated. ",
56        action="store_true",
57        dest="linearize_expressions",
58        default=False)
59    group.add_argument("--skip-canonical-repn",
60            help="Do not create the canonical representation. This is not necessary for solvers (e.g., ASL-based) that do not require it.",
61            action="store_true",
62            dest="skip_canonical_repn",
63            default=False)
64    group.add_argument('--save-model',
65        help='Specify the filename to which the model is saved.  The suffix ' \
66             'of this filename specifies the file format.  If debugging is '  \
67             "on, then this defaults to writing the file 'unknown.lp'.",
68        action='store',
69        dest='save_model',
70        default=None)
71    return group
72
73
74def add_logging_group(parser):
75    group = parser.add_argument_group('Logging Options')
76
77    group.add_argument('-q','--quiet',
78        help='Disable all log messages except for those that refer to errors.', 
79        action='store_true',
80        dest='quiet',
81        default=False)
82    group.add_argument('-w','--warning',
83        help='Enable warning log messages for coopr and pyutilib.', 
84        action='store_true',
85        dest='warning',
86        default=False)
87    group.add_argument('-i','--info',
88        help='Enable informative log messages for coopr and pyutilib.', 
89        action='store_true',
90        dest='info',
91        default=False)
92    group.add_argument('-v','--verbose',
93        help="Indicate that debugging log messages should be printed.  This option can be specified multiple times to add log messages for other parts of coopr and pyutilib.",
94        action='count',
95        dest='verbose',
96        default=0)
97    group.add_argument('-d', '--debug',
98        help='This option indicates that debugging is performed.  This implies the verbose flag, but it also allows exceptions to trigger a failure in which the program stack is printed.',
99        action='store_true',
100        dest='debug',
101        default=False)
102    return group
103
104
105def add_solver_group(parser):
106    solver_help=\
107        "This option specifies the type of solver that is used "\
108        "to solve the Pyomo model instance.  The following solver "\
109        "types are are currently supported:"
110    solver_list = SolverFactory.services()
111    solver_list = sorted( filter(lambda x: '_' != x[0], solver_list) )
112    solver_help += " %s." % ', '.join(solver_list)
113
114    parser.add_argument('--help-solvers',
115        help='Print information about the solvers that are available',
116        action='store_true',
117        dest='help_solvers',
118        default=False)
119
120    group = parser.add_argument_group('Solver Options')
121
122    group.add_argument('--solver',
123        help=solver_help,
124        action='store',
125        dest='solver',
126        #choices=solver_list,
127        default='glpk')
128    group.add_argument('--solver-manager',
129        help='Specify the technique that is used to manage solver executions.',
130        action='store',
131        dest='smanager_type',
132        #type='choice',
133        choices=SolverManagerFactory.services(),
134        default='serial')
135    group.add_argument('--solver-mipgap',
136        help='The solver termination mipgap',
137        action='store',
138        dest='solver_mipgap',
139        type=float,
140        default=None)
141    group.add_argument('--solver-options',
142        help='Options passed into the solver',
143        action='append',
144        dest='solver_options',
145        default=[])
146    group.add_argument('--solver-suffixes',
147        help='One or more solution suffixes to be extracted by the solver',
148        action='append',
149        dest='solver_suffixes',
150        default=[])
151    group.add_argument('--timelimit',
152        help='Limit to the number of seconds that the solver is run',
153        action='store',
154        dest='timelimit',
155        type=int,
156        default=0)
157    return group
158
159
160def add_postsolve_group(parser):
161    group = parser.add_argument_group('Post-Solve Options')
162
163    group.add_argument('--postprocess',
164        help='Specify a Python module that gets executed after optimization. ' \
165             'If this option is specified multiple times, then the modules '  \
166             'are executed in the specified order.',
167        action='append',
168        dest='postprocess',
169        default=[])
170    group.add_argument('-l','--log',
171        help='Print the solver logfile after performing optimization',
172        action='store_true',
173        dest='log',
174        default=False)
175    group.add_argument('--logfile',
176        help='Redirect output to the specified logfile',
177        action='store',
178        dest='logfile',
179        default=None)
180    group.add_argument('--save-results',
181        help='Specify the filename to which the results are saved.',
182        action='store',
183        dest='save_results',
184        default=None)
185    group.add_argument('--stream-output',
186        help='Stream the solver output to provide information about the '     \
187             "solver's progress.",
188        action='store_true',
189        dest='tee',
190        default=False)
191    group.add_argument('-s','--summary',
192        help='Summarize the final solution after performing optimization',
193        action='store_true',
194        dest='summary',
195        default=False)
196    return group
197
198
199def add_misc_group(parser):
200    parser.add_argument('--help-components',
201        help='Print information about modeling components supported by Pyomo',
202        action='store_true',
203        dest='help_components',
204        default=False)
205
206    group = parser.add_argument_group('Miscellaneous Options')
207
208    group.add_argument('--disable-gc',
209        help='Disable the garbage collecter',
210        action='store_true',
211        dest='disable_gc',
212        default=False)
213    group.add_argument('--interactive',
214        help='After executing Pyomo, launch an interactive Python shell.  If IPython is installed, this shell is an IPython shell.',
215        action='store_true',
216        dest='interactive',
217        default=False)
218    group.add_argument('-k','--keepfiles',
219        help='Keep temporary files',
220        action='store_true',
221        dest='keepfiles',
222        default=False)
223    group.add_argument('--path',
224        help='Give a path that is used to find the Pyomo python files',
225        action='store',
226        dest='path',
227        default='.')
228    group.add_argument('--profile',
229        help='Enable profiling of Python code.  The value of this option is ' \
230             'the number of functions that are summarized.',
231        action='store',
232        dest='profile',
233        type=int,
234        default=0)
235    if pympler_available is True:
236       group.add_argument("--profile-memory",
237                            help="If Pympler is available, report memory usage statistics for the generated instance and any associated processing steps. A value of 0 indicates disabled. A value of 1 forces summary memory statistics after major stages of the pyomo script. A value of 2 forces detailed memory statistics during instance creation and various steps of preprocessing. Values equal to 3 and higher currently provide no additional information. Higher values automatically enable all functionality associated with lower values, e.g., 2 turns on detailed and summary statistics.",
238                            action="store",
239                            dest="profile_memory",
240                            type=int,
241                            default=0)
242
243    group.add_argument('--tempdir',
244        help='Specify the directory where temporary files are generated',
245        action='store',
246        dest='tempdir',
247        default=None)
248    return group
249   
250
251def create_parser():
252    #
253    #
254    # Setup command-line options
255    #
256    #
257    parser = argparse.ArgumentParser(
258                usage = '%(prog)s [options] <model.py> [<model.dat>]'
259                )
260    group = add_model_group(parser)
261    group.add_argument('--instance-only',
262        help='Generate a model instance, and then return',
263        action='store_true',
264        dest='only_instance',
265        default=False)
266    add_solver_group(parser)
267    add_postsolve_group(parser)
268    add_logging_group(parser)
269    add_misc_group(parser)
270    parser.add_argument('model_file', action='store', nargs='?', default='', help='A Python module that defines a Pyomo model')
271    parser.add_argument('data_files', action='store', nargs='*', default=[], help='Pyomo data files that defined data used to create a model instance')
272    return parser
273
274
275def run_pyomo(options=Options(), parser=None):
276    if options.help_solvers:
277        util.print_solver_help(options)
278        return Container()
279    #
280    if options.help_components:
281        util.print_components(options)
282        return Container()
283    #
284    if not util.setup_environment(options):
285        return Container()                                   #pragma:nocover
286    #
287    if not util.apply_preprocessing(options, parser):
288        return Container()                                   #pragma:nocover
289    #
290    model_data = util.create_model(options)
291    if options.save_model and options.only_instance:
292        return Container(instance=model_data.instance)
293    #
294    results, opt = util.apply_optimizer(options, model_data.instance)
295    #
296    if not util.process_results(options, model_data.instance, results, opt):
297        return Container(instance=model_data.instance, results=results) #pragma:nocover
298    #
299    util.apply_postprocessing(options, model_data.instance, results)
300    #
301    util.finalize(options, model=model_data.model, instance=model_data.instance, results=results)
302    #       
303    return Container(instance=model_data.instance, results=results)
304
305
306def run(args=None):
307    result = util.run_command(run_pyomo, create_parser(), args=args, name='pyomo')
308    return result
309
Note: See TracBrowser for help on using the repository browser.