Changeset 2160


Ignore:
Timestamp:
Jan 13, 2010 6:09:28 PM (10 years ago)
Author:
wehart
Message:

Adding the pyomo2lp and pyomo2nl commands.
Setting up the convert.py module, which is used in these commands.
Various fixes to scripting utilities: fixed management of
traceback functions, and reworked the processing of the save_model
option.

Location:
coopr.pyomo/trunk
Files:
5 edited
1 moved

Legend:

Unmodified
Added
Removed
  • coopr.pyomo/trunk/coopr/pyomo/io/ampl.py

    r2064 r2160  
    9696                  for ndx in Vv:
    9797                           if Vv[ndx].id >= 0:
    98                               L = Vv[ndx].lb()
    99                               U = Vv[ndx].ub()
     98                              L = Vv[ndx].lb
     99                              U = Vv[ndx].ub
    100100                              if L is not None and value(L) == 0 \
    101101                                 and U is not None and value(U) == 1:
     
    267267                        continue
    268268                vv = var._varval[ndx]
    269                 L = vv.lb()
    270                 U = vv.ub()
     269                L = vv.lb
     270                U = vv.ub
    271271                if L is not None:
    272272                  Lv = str(value(L))
  • coopr.pyomo/trunk/coopr/pyomo/scripting/convert.py

    r2159 r2160  
    102102            action="store",
    103103            dest="save_model",
    104             default=None)
     104            default=True)
    105105
    106106    parser.usage=name+" [options] <model.py> [<model.dat>]"
     
    108108
    109109
     110format = None
     111
    110112def convert(options=Options(), args=(), parser=None):
     113    global format
     114    options.verbose = False
     115    options.format = format
    111116    if options.help_components:
    112117        util.print_components(options)
     118        return Container()
    113119    if not util.setup_environment(options, args):
     120        return                                               #pragma:nocover
     121    if not util.apply_preprocessing(options, parser, args):
    114122        return
    115     if not util.apply_preprocessing(options, parser, args):
    116123    instance = util.create_model(options, args)
    117124
    118 
    119125def pyomo2lp(args=None):
     126    global format
    120127    parser = create_parser('pyomo2lp')
    121     parser.format = ProblemFormat.cpxlp
    122     return util.run_command(run_pyomo, parser, args=args, name='pyomo2lp')
     128    format = ProblemFormat.cpxlp
     129    return util.run_command(convert, parser, args=args, name='pyomo2lp')
    123130
    124131def pyomo2nl(args=None):
     132    global format
    125133    parser = create_parser('pyomo2nl')
    126     parser.format = ProblemFormat.nl
    127     return util.run_command(run_pyomo, parser, args=args, name='pyomo2nl')
     134    format = ProblemFormat.nl
     135    return util.run_command(convert, parser, args=args, name='pyomo2nl')
    128136
  • coopr.pyomo/trunk/coopr/pyomo/scripting/pyomo.py

    r2148 r2160  
    200200        return Container()                                   #pragma:nocover
    201201    instance = util.create_model(options, args)
    202     if options.only_instance:
     202    if options.save_model or options.only_instance:
    203203        return Container(instance=instance)
    204204    results = util.apply_optimizer(options, instance)
  • coopr.pyomo/trunk/coopr/pyomo/scripting/util.py

    r2159 r2160  
    2222import pyutilib.misc
    2323
     24filter_excepthook=False
    2425
    2526#
     
    6263    #
    6364    pyomo.reset_debugging()
    64     if options.verbose:
    65        pyomo.set_debugging("verbose")
    6665    #
    6766    # Setup debugging for specific Pyomo components
     
    7069       for val in options.debug:
    7170         pyomo.set_debugging( val )
     71    if options.verbose:
     72       pyomo.set_debugging("verbose")
    7273    #
    7374    # Setup I/O redirect to a logfile
     
    8586    # Configure exception management
    8687    #
    87     filter_excepthook=True
    8888    def pyomo_excepthook(etype,value,tb):
     89        global filter_excepthook
    8990        model=""
    90         if len(args) > 1:
    91             model=args[1]
     91        if len(args) > 0:
     92            model=args[0]
    9293        print ""
    93         msg = "ERROR: Unexpected exception while %s Pyomo %s" % ('loading' if filter_excepthook else 'running', 'model '+model if model != "" else "")
     94        msg = "ERROR: Unexpected exception while %s %s" % ('loading' if filter_excepthook else 'running', 'model '+model if model != "" else "")
    9495        print msg
    9596        print "  ",value
     
    99100        if not pyomo.debug("all") and filter_excepthook:
    100101            while i < len(tb_list):
     102                print "Y",model,tb_list[i][0]
    101103                if model in tb_list[i][0]:
    102104                    break
    103105                i += 1
    104         print "Pyomo Traceback (most recent call last):"
     106        print "Traceback (most recent call last):"
    105107        for item in tb_list[i:]:
    106108            print "  File \""+item[0]+"\", line "+str(item[1])+", in "+item[2]
     
    132134#
    133135def create_model(options, args):
     136    global filter_excepthook
    134137    #
    135138    # Verify that files exist
     
    141144    # Create Model
    142145    #
     146    filter_excepthook=True
    143147    usermodel = pyutilib.misc.import_file(args[0])
    144148    filter_excepthook=False
     
    178182       instance.pprint()
    179183       print ""
    180        options.save_model = 'unknown.lp'
    181        print "Model instance written in file",options.save_model,"to allow debugging"
    182184
    183185    if not options.save_model is None:
    184186        if options.save_model == True:
    185             fname args[1][:-3]+'.'+str(options.format)
     187            if options.format in [ProblemFormat.cpxlp,ProblemFormat.lpxlp]:
     188                fname = (args[1])[:-3]+'lp'
     189            else:
     190                fname = (args[1])[:-3]+str(options.format)
    186191            format=options.format
    187192        else:
     
    191196        if not os.path.exists(fname):
    192197            print "ERROR: file "+fname+" has not been created!"
     198        else:
     199            print "Model written to file '"+str(fname)+"'"
     200
    193201    return instance
    194202
  • coopr.pyomo/trunk/scripts/pyomo2lp

    r2146 r2160  
    1515#  _________________________________________________________________________
    1616
    17 import traceback
    1817import sys
    1918if sys.version_info[0:2] < (2,5,3):
    2019   print ""
    21    print "ERROR: Pyomo requires Python 2.5.3 or newer"
     20   print "ERROR: pyomo2lp requires Python 2.5.3 or newer"
    2221   sys.exit(1)
    2322from os.path import abspath, dirname
     
    2524sys.path.insert(0, dirname(dirname(abspath(__file__))))
    2625sys.path.append(".")
    27 #currdir = dirname(abspath(__file__))
    28 #coopr_path = dirname(currdir)
    29 #pyutilib_path = dirname(coopr_path)+os.sep+"pyutilib"
    30 #sys.path.append(coopr_path)
    31 #sys.path.append(pyutilib_path)
    32 #sys.path.append(abspath(os.getcwd()))
    33 #
    34 # Recurse up the current path, looking for a subdirectory that
    35 # contains 'python' and 'bin'
    36 #
    37 #curr = os.path.abspath(os.getcwd())
    38 #while os.sep in curr:
    39     #if os.path.exists(curr+os.sep+"python") and os.path.exists(curr+os.sep+"bin"):
    40         #os.environ["PATH"] = curr+os.sep+"bin:"+os.environ["PATH"]
    41         #break
    42     #if os.path.basename(curr) == "":
    43         #break
    44     #curr = os.path.dirname(curr)
    4526
    46 import coopr.pyomo.scripting.pyomo2lp
     27import coopr.pyomo.scripting.convert
    4728
    4829try:
    49     coopr.pyomo.scripting.pyomo2lp.run(args=sys.argv[1:])
     30    coopr.pyomo.scripting.convert.pyomo2lp(args=sys.argv[1:])
    5031except SystemExit:
    5132    pass
  • coopr.pyomo/trunk/scripts/pyomo2nl

    r2146 r2160  
    1515#  _________________________________________________________________________
    1616
    17 import traceback
    1817import sys
    1918if sys.version_info[0:2] < (2,5,3):
    2019   print ""
    21    print "ERROR: Pyomo requires Python 2.5.3 or newer"
     20   print "ERROR: pyomo2nl requires Python 2.5.3 or newer"
    2221   sys.exit(1)
    2322from os.path import abspath, dirname
     
    2524sys.path.insert(0, dirname(dirname(abspath(__file__))))
    2625sys.path.append(".")
    27 #currdir = dirname(abspath(__file__))
    28 #coopr_path = dirname(currdir)
    29 #pyutilib_path = dirname(coopr_path)+os.sep+"pyutilib"
    30 #sys.path.append(coopr_path)
    31 #sys.path.append(pyutilib_path)
    32 #sys.path.append(abspath(os.getcwd()))
    33 #
    34 # Recurse up the current path, looking for a subdirectory that
    35 # contains 'python' and 'bin'
    36 #
    37 #curr = os.path.abspath(os.getcwd())
    38 #while os.sep in curr:
    39     #if os.path.exists(curr+os.sep+"python") and os.path.exists(curr+os.sep+"bin"):
    40         #os.environ["PATH"] = curr+os.sep+"bin:"+os.environ["PATH"]
    41         #break
    42     #if os.path.basename(curr) == "":
    43         #break
    44     #curr = os.path.dirname(curr)
    4526
    46 import coopr.pyomo.scripting.pyomo2lp
     27import coopr.pyomo.scripting.convert
    4728
    4829try:
    49     coopr.pyomo.scripting.pyomo2lp.run(args=sys.argv[1:])
     30    coopr.pyomo.scripting.convert.pyomo2nl(args=sys.argv[1:])
    5031except SystemExit:
    5132    pass
Note: See TracChangeset for help on using the changeset viewer.