source: coopr.pyomo/stable/2.3/coopr/pyomo/scripting/util.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.

........

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
11import sys
12import os
13import textwrap
14import traceback
15try:
16    import cProfile as profile
17except ImportError:
18    import profile
19try:
20    import pstats
21    pstats_available=True
22except ImportError:
23    pstats_available=False
24import gc
25from coopr.pyomo import *
26from coopr.opt.base import SolverFactory
27from coopr.opt.parallel import SolverManagerFactory
28import pyutilib.services
29import pyutilib.misc
30
31filter_excepthook=False
32
33#
34# Print information about modeling components supported by Pyomo
35#
36def print_components(options):
37    print ""
38    print "----------------------------------------------------------------" 
39    print "Pyomo Model Components:"
40    print "----------------------------------------------------------------" 
41    components = pyomo.model_components()
42    index = pyutilib.misc.sort_index(components)
43    for i in index:
44        print ""
45        print " "+components[i][0]
46        for line in textwrap.wrap(components[i][1], 59):
47            print "    "+line
48    print ""
49    print "----------------------------------------------------------------" 
50    print "Pyomo Virtual Sets:"
51    print "----------------------------------------------------------------" 
52    pyomo_sets = pyomo.predefined_sets()
53    index = pyutilib.misc.sort_index(pyomo_sets)
54    for i in index:
55        print ""
56        print " "+pyomo_sets[i][0]
57        print "    "+pyomo_sets[i][1]
58
59#
60# Setup Pyomo execution environment
61#
62def setup_environment(options, args):
63    #
64    # Disable garbage collection
65    #
66    if options.disable_gc:
67        gc.disable()
68    #
69    # Setup verbose debugging mode
70    #
71    pyomo.reset_debugging()
72    #
73    # Setup debugging for specific Pyomo components
74    #
75    if options.debug is not None:
76       for val in options.debug:
77         pyomo.set_debugging( val )
78    if options.verbose:
79       pyomo.set_debugging("verbose")
80    #
81    # Setup I/O redirect to a logfile
82    #
83    if not options.logfile is None:
84        pyutilib.misc.setup_redirect(options.logfile)
85    #
86    # Setup management for temporary files
87    #
88    if not options.tempdir is None:
89        if not os.path.exists(options.tempdir):
90            raise ValueError, "Directory for temporary files does not exist: "+options.tempdir
91        pyutilib.services.TempfileManager.tempdir = options.tempdir
92    #
93    # Configure exception management
94    #
95    def pyomo_excepthook(etype,value,tb):
96        global filter_excepthook
97        model=""
98        if len(args) > 0:
99            model=args[0]
100        print ""
101        msg = "ERROR: Unexpected exception while %s %s" % ('loading' if filter_excepthook else 'running', 'model '+model if model != "" else "")
102        print msg
103        print "  ",value
104        print ""
105        tb_list = traceback.extract_tb(tb,None)
106        i=0
107        if not pyomo.debug("all") and filter_excepthook:
108            while i < len(tb_list):
109                print "Y",model,tb_list[i][0]
110                if model in tb_list[i][0]:
111                    break
112                i += 1
113        print "Traceback (most recent call last):"
114        for item in tb_list[i:]:
115            print "  File \""+item[0]+"\", line "+str(item[1])+", in "+item[2]
116            if item[3] is not None:
117                print "    "+item[3]
118        sys.exit(1)
119    sys.excepthook = pyomo_excepthook
120    return True
121
122#
123# Execute preprocessing files
124#
125def apply_preprocessing(options, parser, args):
126    #
127    #
128    # Setup solver and model
129    #
130    #
131    if len(args) == 0:
132        parser.print_help()
133        return False
134    #
135    for file in options.preprocess:
136        preprocess = pyutilib.misc.import_file(file)
137    return True
138       
139#
140# Create instance of Pyomo model
141#
142def create_model(options, args):
143    global filter_excepthook
144    #
145    # Verify that files exist
146    #
147    for file in args:
148        if not os.path.exists(file):
149            raise IOError, "File "+file+" does not exist!"
150    #
151    # Create Model
152    #
153    filter_excepthook=True
154    usermodel = pyutilib.misc.import_file(args[0])
155    filter_excepthook=False
156    if options.model_name in dir(usermodel):
157        model = getattr(usermodel, options.model_name)
158        if model is None:
159            print ""
160            raise SystemExit, "'%s' object equals 'None' in module %s" % (options.model_name, args[0])
161            sys.exit(0)
162    elif 'create_model' in dir(usermodel):
163        model = getattr(usermodel, 'create_model')( pyutilib.misc.Container(*options.model_options) )
164    else:
165       print ""
166       raise SystemExit, "Neither '%s' nor 'create_model' are available in module %s" % (options.model_name,args[0])
167       #sys.exit(0)
168    #
169    # Create Problem Instance
170    #
171    if len(args) > 2:
172        #
173        # Load a list of *.dat files
174        #
175        modeldata = ModelData()
176        for file in args[1:]:
177            suffix = (file).split(".")[-1]
178            if suffix != "dat":
179                raise SystemExit, "When specifying multiple data files, they must all be *.dat files: "+str(file)
180            modeldata.add_data_file(file)
181        modeldata.read(model)
182        instance = model.create(modeldata)
183         
184    elif len(args) == 2:
185       #
186       # Load a *.dat file or process a *.py data file
187       #
188       suffix = (args[1]).split(".")[-1]
189       if suffix == "dat":
190          instance = model.create(args[1])
191       elif suffix == "py":
192          userdata = pyutilib.misc.import_file(args[1])
193          if "modeldata" not in dir(userdata):
194             raise SystemExit, "No 'modeldata' object created in module "+args[1]
195          if userdata.modeldata is None:
196             raise SystemExit, "'modeldata' object equals 'None' in module "+args[1]
197          userdata.modeldata.read(model)
198          instance = model.create(userdata.modeldata)
199       else:
200          raise ValueError, "Unknown data file type: "+args[1]
201    else:
202       instance = model.create()
203    if pyomo.debug("instance"):
204       print "MODEL INSTANCE"
205       instance.pprint()
206       print ""
207
208    fname=None
209    symbol_map=None
210    if not options.save_model is None:
211        if options.save_model == True:
212            if options.format in [ProblemFormat.cpxlp,ProblemFormat.lpxlp]:
213                fname = (args[1])[:-3]+'lp'
214            else:
215                fname = (args[1])[:-3]+str(options.format)
216            format=options.format
217        else:
218            fname = options.save_model
219            format=None
220        [fname, symbol_map] = instance.write(filename=fname, format=format)
221        if not os.path.exists(fname):
222            print "ERROR: file "+fname+" has not been created!"
223        else:
224            print "Model written to file '"+str(fname)+"'"
225
226    return pyutilib.misc.Container(instance=instance, symbol_map=symbol_map, filename=fname)
227
228#
229# Perform optimization with concrete instance
230#
231def apply_optimizer(options, instance):
232    #
233    # Create Solver and Perform Optimization
234    #
235    solver = options.solver
236    subsolver=None
237    if not solver is None and ':' in solver:
238        solver, subsolver = solver.split(':')
239    opt = SolverFactory( solver )
240    if opt is None:
241       raise ValueError, "Problem constructing solver `"+str(solver)+"'"
242    opt.keepFiles=options.keepfiles or options.log
243    if options.timelimit == 0:
244       options.timelimit=None
245    if options.solver_mipgap is not None:
246       opt.mipgap = options.solver_mipgap
247    opt.suffixes = options.solver_suffixes
248    if not subsolver is None:
249        subsolver=' solver='+subsolver
250    else:
251        subsolver=''
252    opt.set_options(" ".join(options.solver_options)+subsolver)
253    #
254    solver_mngr = SolverManagerFactory( options.smanager_type )
255    if solver_mngr is None:
256       raise ValueError, "Problem constructing solver manager `"+str(options.smanager_type)+"'"
257    results = solver_mngr.solve(instance, opt=opt, tee=options.tee, timelimit=options.timelimit)
258    if results == None:
259            raise ValueError, "opt.solve returned None"
260    return results, opt
261
262#
263# Process results
264#
265def process_results(options, instance, results, opt):
266   
267    if options.log:
268       print ""
269       print "=========================================================="
270       print "Solver Logfile:",opt.log_file
271       print "=========================================================="
272       print ""
273       INPUT = open(opt.log_file, "r")
274       for line in INPUT:
275         print line,
276       INPUT.close()
277   
278    try:
279        instance.load(results)
280    except Exception, e:
281        print "Problem loading solver results"
282        raise
283    print ""
284    results.write(num=1)
285   
286    if options.summary:
287       print ""
288       print "=========================================================="
289       print "Solution Summary"
290       print "=========================================================="
291       if len(results.solution(0).variable) > 0:
292          print ""
293          display(instance)
294       else:
295          print "No solutions reported by solver."
296    return True
297
298def apply_postprocessing(options, instance, results):
299    for file in options.postprocess:
300        postprocess = pyutilib.misc.import_file(file)
301        if "postprocess" in dir(postprocess):
302            postprocess.postprocess(instance,results)
303       
304
305def run_command(command, parser, args=None, name='unknown'):
306    #
307    # Execute a function that processes command-line arguments and then
308    # calls a command-line driver.  This
309    # is segregated from the driver to enable profiling.
310    #
311
312    #
313    #
314    # Parse command-line options
315    #
316    #
317    try:
318       (options, nargs) = parser.parse_args(args=args)
319    except SystemExit:
320       # the parser throws a system exit if "-h" is specified - catch
321       # it to exit gracefully.
322       return
323
324    #
325    # Call the main Pyomo runner with profiling
326    #
327    if options.profile > 0:
328        if not pstats_available:
329            raise ValueError, "Cannot use the 'profile' option.  The Python 'pstats' package cannot be imported!"
330        tfile = pyutilib.services.TempfileManager.create_tempfile(suffix=".profile")
331        tmp = profile.runctx(command.__name__+'(options=options,args=nargs,parser=parser)',globals(),locals(),tfile)
332        p = pstats.Stats(tfile).strip_dirs()
333        p.sort_stats('time', 'cum')
334        options.profile = eval(options.profile)
335        p = p.print_stats(options.profile)
336        p.print_callers(options.profile)
337        p.print_callees(options.profile)
338        p = p.sort_stats('cum','calls')
339        p.print_stats(options.profile)
340        p.print_callers(options.profile)
341        p.print_callees(options.profile)
342        p = p.sort_stats('calls')
343        p.print_stats(options.profile)
344        p.print_callers(options.profile)
345        p.print_callees(options.profile)
346        pyutilib.services.TempfileManager.clear_tempfiles()
347        ans = [tmp, None]
348    else:
349        #
350        # Call the main Pyomo runner without profiling
351        #
352        try:
353            ans = command(options=options, args=nargs, parser=parser)
354        except SystemExit, err:
355            if pyomo.debug('errors'):
356                sys.exit(0)
357            print 'Exiting %s: %s' % (name, str(err))
358            ans = None
359        except Exception, err:
360            if pyomo.debug('errors'):
361                raise
362            print ""
363            print "ERROR:",str(err)
364            ans = None
365
366    if options.disable_gc:
367        gc.enable()
368    return ans
369
Note: See TracBrowser for help on using the repository browser.