Changeset 2159


Ignore:
Timestamp:
Jan 13, 2010 4:59:23 PM (10 years ago)
Author:
wehart
Message:

Updates to reconfigure how files are written out by Pyomo.

Location:
coopr.pyomo/trunk/coopr/pyomo/scripting
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • coopr.pyomo/trunk/coopr/pyomo/scripting/pyomo2lp.py

    r2146 r2159  
    1212from optparse import OptionParser
    1313from coopr.pyomo import pyomo
     14from coopr.opt import ProblemFormat
    1415from coopr.opt.base import SolverFactory
    1516from pyutilib.misc import Options, Container
     
    1718import util
    1819
    19 #
    20 #
    21 # Setup command-line options
    22 #
    23 #
    24 debug_help=\
    25 "This option is used to turn on debugging output. This option "\
    26 "can be specified multiple times to turn on different debugging"\
    27 "output. The following debugging options can be specified:"
    28 debug_choices=[]
    29 for item in pyomo.Debug:
    30   if str(item) != "none":
    31      debug_choices.append( str(item) )
    32      debug_help += "\n.."+str(item)
     20def create_parser(name):
     21    #
     22    #
     23    # Setup command-line options
     24    #
     25    #
     26    debug_help=\
     27    "This option is used to turn on debugging output. This option "\
     28    "can be specified multiple times to turn on different debugging"\
     29    "output. The following debugging options can be specified:"
     30    debug_choices=[]
     31    for item in pyomo.Debug:
     32      if str(item) != "none":
     33         debug_choices.append( str(item) )
     34         debug_help += "\n.."+str(item)
    3335
    34 parser = OptionParser()
    35 parser.add_option("--path",
    36     help="Give a path that is used to find the Pyomo python files",
    37     action="store",
    38     dest="path",
    39     type="string",
    40     default=".")
    41 parser.add_option("--help-components",
    42         help="Print information about modeling components supported by Pyomo",
    43         action="store_true",
    44         dest="help_components",
    45         default=False)
    46 parser.add_option("--debug",
    47         help=debug_help,
    48         action="append",
    49         dest="debug",
    50         choices=debug_choices)
    51 parser.add_option("-k","--keepfiles",
    52         help="Keep temporary files",
    53         action="store_true",
    54         dest="keepfiles",
    55         default=False)
    56 parser.add_option("--tempdir",
    57         help="Specify the directory where temporary files are generated",
     36    parser = OptionParser()
     37    parser.add_option("--path",
     38        help="Give a path that is used to find the Pyomo python files",
    5839        action="store",
    59         dest="tempdir",
    60         default=None)
    61 parser.add_option("-q","--quiet",
    62         help="Turn off solver output",
    63         action="store_true",
    64         dest="quiet",
    65         default=False)
    66 parser.add_option("--logfile",
    67         help="Redirect output to the specified logfile",
    68         action="store",
    69         dest="logfile",
    70         default=None)
    71 parser.add_option("--profile",
    72         help="Enable profiling of Python code.  The value of this option is the number of functions that are summarized.",
    73         action="store",
    74         dest="profile",
    75         default=0)
    76 parser.add_option("--preprocess",
    77         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.",
    78         action="append",
    79         dest="preprocess",
    80         default=[])
    81 parser.add_option("--model-name",
    82         help="The name of the model object that is created in the specified Pyomo module",
    83         action="store",
    84         dest="model_name",
     40        dest="path",
    8541        type="string",
    86         default="model")
    87 parser.add_option("--model-options",
    88         help="Options passed into a create_model() function to construct the model",
    89         action="append",
    90         dest="model_options",
    91         type="string",
    92         default=[])
    93 parser.add_option("--disable-gc",
    94         help="Disable the garbage collecter",
    95         action="store_true",
    96         dest="disable_gc",
    97         default=False)
    98 parser.add_option("--save-model",
    99         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'.",
    100         action="store",
    101         dest="save_model",
    102         default=None)
     42        default=".")
     43    parser.add_option("--help-components",
     44            help="Print information about modeling components supported by Pyomo",
     45            action="store_true",
     46            dest="help_components",
     47            default=False)
     48    parser.add_option("--debug",
     49            help=debug_help,
     50            action="append",
     51            dest="debug",
     52            choices=debug_choices)
     53    parser.add_option("-k","--keepfiles",
     54            help="Keep temporary files",
     55            action="store_true",
     56            dest="keepfiles",
     57            default=False)
     58    parser.add_option("--tempdir",
     59            help="Specify the directory where temporary files are generated",
     60            action="store",
     61            dest="tempdir",
     62            default=None)
     63    parser.add_option("-q","--quiet",
     64            help="Turn off solver output",
     65            action="store_true",
     66            dest="quiet",
     67            default=False)
     68    parser.add_option("--logfile",
     69            help="Redirect output to the specified logfile",
     70            action="store",
     71            dest="logfile",
     72            default=None)
     73    parser.add_option("--profile",
     74            help="Enable profiling of Python code.  The value of this option is the number of functions that are summarized.",
     75            action="store",
     76            dest="profile",
     77            default=0)
     78    parser.add_option("--preprocess",
     79            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.",
     80            action="append",
     81            dest="preprocess",
     82            default=[])
     83    parser.add_option("--model-name",
     84            help="The name of the model object that is created in the specified Pyomo module",
     85            action="store",
     86            dest="model_name",
     87            type="string",
     88            default="model")
     89    parser.add_option("--model-options",
     90            help="Options passed into a create_model() function to construct the model",
     91            action="append",
     92            dest="model_options",
     93            type="string",
     94            default=[])
     95    parser.add_option("--disable-gc",
     96            help="Disable the garbage collecter",
     97            action="store_true",
     98            dest="disable_gc",
     99            default=False)
     100    parser.add_option("--save-model",
     101            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'.",
     102            action="store",
     103            dest="save_model",
     104            default=None)
    103105
    104 parser.usage="pyomo2lp [options] <model.py> [<model.dat>]"
     106    parser.usage=name+" [options] <model.py> [<model.dat>]"
     107    return parser
    105108
    106109
    107 def run_pyomo(options=Options(), args=()):
     110def convert(options=Options(), args=(), parser=None):
    108111    if options.help_components:
    109112        util.print_components(options)
     
    114117
    115118
    116 def run(args=None):
     119def pyomo2lp(args=None):
     120    parser = create_parser('pyomo2lp')
     121    parser.format = ProblemFormat.cpxlp
    117122    return util.run_command(run_pyomo, parser, args=args, name='pyomo2lp')
    118123
     124def pyomo2nl(args=None):
     125    parser = create_parser('pyomo2nl')
     126    parser.format = ProblemFormat.nl
     127    return util.run_command(run_pyomo, parser, args=args, name='pyomo2nl')
     128
  • coopr.pyomo/trunk/coopr/pyomo/scripting/util.py

    r2148 r2159  
    182182
    183183    if not options.save_model is None:
    184         instance.write(filename=options.save_model, format=None)
    185         if not os.path.exists(options.save_model):
    186             print "ERROR: file "+options.save_model+" has not been created!"
     184        if options.save_model == True:
     185            fname args[1][:-3]+'.'+str(options.format)
     186            format=options.format
     187        else:
     188            fname = options.save_model
     189            format=None
     190        instance.write(filename=fname, format=format)
     191        if not os.path.exists(fname):
     192            print "ERROR: file "+fname+" has not been created!"
    187193    return instance
    188194
Note: See TracChangeset for help on using the changeset viewer.