source: coopr.pyomo/trunk/coopr/pyomo/scripting/convert.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: 2.4 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__all__ = ['pyomo2lp', 'pyomo2nl']
12
13
14import argparse
15from coopr.opt import ProblemFormat
16from coopr.opt.base import SolverFactory
17from pyutilib.misc import Options, Container
18
19import util
20import pyomo
21
22def create_parser(cmd):
23    #
24    #
25    # Setup command-line options
26    #
27    #
28    parser = argparse.ArgumentParser(
29                usage = '%s [options] <model.py> [<model.dat>]' % cmd
30                )
31    pyomo.add_model_group(parser)
32    pyomo.add_logging_group(parser)
33    pyomo.add_misc_group(parser)
34    parser.add_argument('model_file', action='store', nargs='?', default='', help='A Python      module that defines a Pyomo model')
35    parser.add_argument('data_files', action='store', nargs='*', default=[], help='Pyomo data    files that defined data used to create a model instance')
36    return parser
37
38format = None
39
40def convert(options=Options(), parser=None):
41    global format
42    if options.save_model is None:
43        if format == ProblemFormat.cpxlp:
44            options.save_model = 'unknown.lp'
45        else:
46            options.save_model = 'unknown.'+str(format)
47    options.format = format
48    #
49    if options.help_components:
50        util.print_components(options)
51        return Container()
52    #
53    if not util.setup_environment(options):
54        return Container()
55    #
56    if not util.apply_preprocessing(options, parser):
57        return Container()
58    #
59    model_data = util.create_model(options)
60    #
61    util.finalize(options, model=model_data.model)
62    #
63    return Container(model=model_data.model)
64
65def pyomo2lp(args=None):
66    global format
67    parser = create_parser('pyomo2lp')
68    format = ProblemFormat.cpxlp
69    return util.run_command(convert, parser, args=args, name='pyomo2lp')
70
71def pyomo2nl(args=None):
72    global format
73    parser = create_parser('pyomo2nl')
74    format = ProblemFormat.nl
75    return util.run_command(convert, parser, args=args, name='pyomo2nl')
76
Note: See TracBrowser for help on using the repository browser.