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 | |
---|
14 | import argparse |
---|
15 | from coopr.opt import ProblemFormat |
---|
16 | from coopr.opt.base import SolverFactory |
---|
17 | from pyutilib.misc import Options, Container |
---|
18 | |
---|
19 | import util |
---|
20 | import pyomo |
---|
21 | |
---|
22 | def 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 | |
---|
38 | format = None |
---|
39 | |
---|
40 | def 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 | |
---|
65 | def 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 | |
---|
71 | def 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 | |
---|