Changeset 2147


Ignore:
Timestamp:
Jan 12, 2010 12:44:35 AM (11 years ago)
Author:
wehart
Message:

Fixing bugs

Location:
coopr.pyomo/trunk/coopr/pyomo
Files:
1 deleted
4 edited

Legend:

Unmodified
Added
Removed
  • coopr.pyomo/trunk/coopr/pyomo/base/pyomo.py

    r2005 r2147  
    3030  return tmp is not Debug.none and tmp in _debug
    3131
     32def reset_debugging():
     33    global _debug
     34    _debug = []
     35
    3236def set_debugging(*args):
    3337    global _debug
    34     _debug = []
    3538    if args == ():
    3639       args=["all"]
  • coopr.pyomo/trunk/coopr/pyomo/scripting/pyomo.py

    r2146 r2147  
    1717import util
    1818
    19 #
    20 #
    21 # Setup command-line options
    22 #
    23 #
    24 solver_help=\
    25 "This option specifies the type of solver that is used "\
    26 "to solve the Pyomo model instance.  The following solver "\
    27 "types are are currently supported:"
    28 _tmp = SolverFactory()
    29 _tmp.sort()
    30 for item in _tmp:
    31   if item[0] != "_":
    32      solver_help += "\n.."+item
    33 solver_help += ".\nThe default solver is 'glpk'."
    34 debug_help=\
    35 "This option is used to turn on debugging output. This option "\
    36 "can be specified multiple times to turn on different debugging"\
    37 "output. The following debugging options can be specified:"
    38 debug_choices=[]
    39 for item in pyomo.Debug:
    40   if str(item) != "none":
    41      debug_choices.append( str(item) )
    42      debug_help += "\n.."+str(item)
    43 
    44 parser = OptionParser()
    45 parser.add_option("--solver",
    46     help=solver_help,
    47     action="store",
    48     dest="solver",
    49     type="string",
    50     default="glpk")
    51 parser.add_option("--path",
    52     help="Give a path that is used to find the Pyomo python files",
    53     action="store",
    54     dest="path",
    55     type="string",
    56     default=".")
    57 parser.add_option("--help-components",
    58         help="Print information about modeling components supported by Pyomo",
    59         action="store_true",
    60         dest="help_components",
    61         default=False)
    62 parser.add_option("--debug",
    63         help=debug_help,
    64         action="append",
    65         dest="debug",
    66         choices=debug_choices)
    67 parser.add_option("-k","--keepfiles",
    68         help="Keep temporary files",
    69         action="store_true",
    70         dest="keepfiles",
    71         default=False)
    72 parser.add_option("--tempdir",
    73         help="Specify the directory where temporary files are generated",
     19def create_parser():
     20    #
     21    #
     22    # Setup command-line options
     23    #
     24    #
     25    solver_help=\
     26    "This option specifies the type of solver that is used "\
     27    "to solve the Pyomo model instance.  The following solver "\
     28    "types are are currently supported:"
     29    _tmp = SolverFactory()
     30    _tmp.sort()
     31    for item in _tmp:
     32      if item[0] != "_":
     33         solver_help += "\n.."+item
     34    solver_help += ".\nThe default solver is 'glpk'."
     35    debug_help=\
     36    "This option is used to turn on debugging output. This option "\
     37    "can be specified multiple times to turn on different debugging"\
     38    "output. The following debugging options can be specified:"
     39    debug_choices=[]
     40    for item in pyomo.Debug:
     41      if str(item) != "none":
     42         debug_choices.append( str(item) )
     43         debug_help += "\n.."+str(item)
     44
     45    parser = OptionParser()
     46    parser.add_option("--solver",
     47        help=solver_help,
    7448        action="store",
    75         dest="tempdir",
    76         default=None)
    77 parser.add_option("-q","--quiet",
    78         help="Turn off solver output",
    79         action="store_true",
    80         dest="quiet",
    81         default=False)
    82 parser.add_option("-l","--log",
    83         help="Print the solver logfile after performing optimization",
    84         action="store_true",
    85         dest="log",
    86         default=False)
    87 parser.add_option("--logfile",
    88         help="Redirect output to the specified logfile",
     49        dest="solver",
     50        type="string",
     51        default="glpk")
     52    parser.add_option("--path",
     53        help="Give a path that is used to find the Pyomo python files",
    8954        action="store",
    90         dest="logfile",
    91         default=None)
    92 parser.add_option("-s","--summary",
    93         help="Summarize the final solution after performing optimization",
    94         action="store_true",
    95         dest="summary",
    96         default=False)
    97 parser.add_option("--instance-only",
    98         help="Generate a model instance, and then return",
    99         action="store_true",
    100         dest="only_instance",
    101         default=False)
    102 parser.add_option("--profile",
    103         help="Enable profiling of Python code.  The value of this option is the number of functions that are summarized.",
    104         action="store",
    105         dest="profile",
    106         default=0)
    107 parser.add_option("--timelimit",
    108         help="Limit to the number of seconds that the solver is run",
    109         action="store",
    110         dest="timelimit",
    111         type="int",
    112         default=0)
    113 parser.add_option("--postprocess",
    114         help="Specify a Python module that gets executed after optimization.  If this option is specified multiple times, then the modules are executed in the specified order.",
    115         action="append",
    116         dest="postprocess",
    117         default=[])
    118 parser.add_option("--preprocess",
    119         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.",
    120         action="append",
    121         dest="preprocess",
    122         default=[])
    123 parser.add_option("-v","--verbose",
    124         help="Make solver output verbose",
    125         action="store_true",
    126         dest="verbose",
    127         default=False)
    128 parser.add_option("--solver-options",
    129         help="Options passed into the solver",
    130         action="append",
    131         dest="solver_options",
     55        dest="path",
    13256        type="string",
    133         default=[])
    134 parser.add_option("--solver-mipgap",
    135         help="The solver termination mipgap",
    136         action="store",
    137         dest="solver_mipgap",
    138         type="float",
    139         default=None)
    140 parser.add_option("--model-name",
    141         help="The name of the model object that is created in the specified Pyomo module",
    142         action="store",
    143         dest="model_name",
    144         type="string",
    145         default="model")
    146 parser.add_option("--model-options",
    147         help="Options passed into a create_model() function to construct the model",
    148         action="append",
    149         dest="model_options",
    150         type="string",
    151         default=[])
    152 parser.add_option("--disable-gc",
    153         help="Disable the garbage collecter",
    154         action="store_true",
    155         dest="disable_gc",
    156         default=False)
    157 parser.add_option("--solver-manager",
    158         help="Specify the technique that is used to manage solver executions.",
    159         action="store",
    160         dest="smanager_type",
    161         default="serial")
    162 parser.add_option("--stream-output",
    163         help="Stream the solver output to provide information about the solver's progress.",
    164         action="store_true",
    165         dest="tee",
    166         default=False)
    167 parser.add_option("--save-model",
    168         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'.",
    169         action="store",
    170         dest="save_model",
    171         default=None)
    172 #
    173 # These options are depricated until we have a specific use-case for them
    174 #
    175 ##if False:
    176 ##  parser.add_option("--seed",
    177 ##        help="Specify a seed to derandomize the solver",
    178 ##        action="store",
    179 ##        dest="seed",
    180 ##        type="int",
    181 ##        default=0)
    182 ##  parser.add_option("--first-feasible",
    183 ##        help="Terminate after the first feasible incumbent",
    184 ##        action="store_true",
    185 ##        dest="ff",
    186 ##        default=False)
    187 parser.usage="pyomo [options] <model.py> [<model.dat>]"
    188 
    189 
    190 def run_pyomo(options=Options(), args=()):
     57        default=".")
     58    parser.add_option("--help-components",
     59            help="Print information about modeling components supported by Pyomo",
     60            action="store_true",
     61            dest="help_components",
     62            default=False)
     63    parser.add_option("--debug",
     64            help=debug_help,
     65            action="append",
     66            dest="debug",
     67            choices=debug_choices)
     68    parser.add_option("-k","--keepfiles",
     69            help="Keep temporary files",
     70            action="store_true",
     71            dest="keepfiles",
     72            default=False)
     73    parser.add_option("--tempdir",
     74            help="Specify the directory where temporary files are generated",
     75            action="store",
     76            dest="tempdir",
     77            default=None)
     78    parser.add_option("-q","--quiet",
     79            help="Turn off solver output",
     80            action="store_true",
     81            dest="quiet",
     82            default=False)
     83    parser.add_option("-l","--log",
     84            help="Print the solver logfile after performing optimization",
     85            action="store_true",
     86            dest="log",
     87            default=False)
     88    parser.add_option("--logfile",
     89            help="Redirect output to the specified logfile",
     90            action="store",
     91            dest="logfile",
     92            default=None)
     93    parser.add_option("-s","--summary",
     94            help="Summarize the final solution after performing optimization",
     95            action="store_true",
     96            dest="summary",
     97            default=False)
     98    parser.add_option("--instance-only",
     99            help="Generate a model instance, and then return",
     100            action="store_true",
     101            dest="only_instance",
     102            default=False)
     103    parser.add_option("--profile",
     104            help="Enable profiling of Python code.  The value of this option is the number of functions that are summarized.",
     105            action="store",
     106            dest="profile",
     107            default=0)
     108    parser.add_option("--timelimit",
     109            help="Limit to the number of seconds that the solver is run",
     110            action="store",
     111            dest="timelimit",
     112            type="int",
     113            default=0)
     114    parser.add_option("--postprocess",
     115            help="Specify a Python module that gets executed after optimization.  If this option is specified multiple times, then the modules are executed in the specified order.",
     116            action="append",
     117            dest="postprocess",
     118            default=[])
     119    parser.add_option("--preprocess",
     120            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.",
     121            action="append",
     122            dest="preprocess",
     123            default=[])
     124    parser.add_option("-v","--verbose",
     125            help="Make solver output verbose",
     126            action="store_true",
     127            dest="verbose",
     128            default=False)
     129    parser.add_option("--solver-options",
     130            help="Options passed into the solver",
     131            action="append",
     132            dest="solver_options",
     133            type="string",
     134            default=[])
     135    parser.add_option("--solver-mipgap",
     136            help="The solver termination mipgap",
     137            action="store",
     138            dest="solver_mipgap",
     139            type="float",
     140            default=None)
     141    parser.add_option("--model-name",
     142            help="The name of the model object that is created in the specified Pyomo module",
     143            action="store",
     144            dest="model_name",
     145            type="string",
     146            default="model")
     147    parser.add_option("--model-options",
     148            help="Options passed into a create_model() function to construct the model",
     149            action="append",
     150            dest="model_options",
     151            type="string",
     152            default=[])
     153    parser.add_option("--disable-gc",
     154            help="Disable the garbage collecter",
     155            action="store_true",
     156            dest="disable_gc",
     157            default=False)
     158    parser.add_option("--solver-manager",
     159            help="Specify the technique that is used to manage solver executions.",
     160            action="store",
     161            dest="smanager_type",
     162            default="serial")
     163    parser.add_option("--stream-output",
     164            help="Stream the solver output to provide information about the solver's progress.",
     165            action="store_true",
     166            dest="tee",
     167            default=False)
     168    parser.add_option("--save-model",
     169            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'.",
     170            action="store",
     171            dest="save_model",
     172            default=None)
     173    #
     174    # These options are depricated until we have a specific use-case for them
     175    #
     176    ##if False:
     177    ##  parser.add_option("--seed",
     178    ##        help="Specify a seed to derandomize the solver",
     179    ##        action="store",
     180    ##        dest="seed",
     181    ##        type="int",
     182    ##        default=0)
     183    ##  parser.add_option("--first-feasible",
     184    ##        help="Terminate after the first feasible incumbent",
     185    ##        action="store_true",
     186    ##        dest="ff",
     187    ##        default=False)
     188    parser.usage="pyomo [options] <model.py> [<model.dat>]"
     189    #
     190    return parser
     191
     192
     193def run_pyomo(options=Options(), args=(), parser=None):
    191194    if options.help_components:
    192195        util.print_components(options)
     
    199202    if options.only_instance:
    200203        return Container(instance=instance)
    201     results = apply_optimizer(options, instance)
    202     if not util.process_results(options, results):
     204    results = util.apply_optimizer(options, instance)
     205    if not util.process_results(options, instance, results):
    203206        return Container(instance=instance, results=results)
    204207    util.apply_postprocessing(options, instance, results)
     
    207210
    208211def run(args=None):
    209     return util.run_command(run_pyomo, parser, args=args, name='pyomo')
    210 
     212    return util.run_command(run_pyomo, create_parser(), args=args, name='pyomo')
     213
  • coopr.pyomo/trunk/coopr/pyomo/scripting/util.py

    r2146 r2147  
    6161    # Setup verbose debugging mode
    6262    #
     63    pyomo.reset_debugging()
    6364    if options.verbose:
    6465       pyomo.set_debugging("verbose")
     
    212213# Process results
    213214#
    214 def process_results(options, results):
     215def process_results(options, instance, results):
    215216   
    216217    if options.log:
     
    275276    if options.profile > 0:
    276277        tfile = pyutilib.services.TempfileManager.create_tempfile(suffix=".profile")
    277         tmp = cProfile.runctx(command.__name__+'(options=options,args=nargs)',globals(),locals(),tfile)
     278        tmp = cProfile.runctx(command.__name__+'(options=options,args=nargs,parser=parser)',globals(),locals(),tfile)
    278279        p = pstats.Stats(tfile).strip_dirs()
    279280        p.sort_stats('time', 'cum')
     
    297298        #
    298299        try:
    299             ans = command(options=options, args=nargs)
     300            ans = command(options=options, args=nargs, parser=parser)
    300301        except SystemExit, err:
    301302            if pyomo.debug('errors'):
  • coopr.pyomo/trunk/coopr/pyomo/tests/examples/test_pyomo.py

    r2049 r2147  
    1414import pyutilib.subprocess
    1515import pyutilib.th
    16 import coopr.pyomo.main_script as main
     16import coopr.pyomo.scripting.pyomo as main
    1717import StringIO
    1818from pyutilib.misc import setup_redirect, reset_redirect
Note: See TracChangeset for help on using the changeset viewer.