Changeset 3482


Ignore:
Timestamp:
Jan 15, 2011 8:16:29 PM (9 years ago)
Author:
khunter
Message:

Dynamic solver list, minor import fixups.

Make the --help options show the list of available solvers, like pyomo --help.

Also some driveby option additions:

-k (--keep-solver-solutions)
-m (--model-directory)
-i (--instance-directory)

Location:
coopr.pysp/trunk/coopr/pysp
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • coopr.pysp/trunk/coopr/pysp/ef_writer_script.py

    r3374 r3482  
    1010
    1111
     12import gc
     13import os
     14import pstats
    1215import sys
    13 import os
     16import traceback
     17
    1418from optparse import OptionParser, OptionGroup
    1519
    16 import pyutilib.services
    17 import textwrap
    18 import traceback
    1920try:
    2021    import cProfile as profile
    2122except ImportError:
    2223    import profile
    23 import pstats
    24 import gc
    25 
    26 from coopr.pysp.ef import *
    2724
    2825from coopr.opt.base import SolverFactory
    2926from coopr.opt.parallel import SolverManagerFactory
    30 
     27from coopr.pysp.ef import *
     28from coopr.pysp.solutionwriter import ISolutionWriterExtension
    3129from pyutilib.component.core import ExtensionPoint
    32 from coopr.pysp.solutionwriter import ISolutionWriterExtension
     30from pyutilib.services import TempfileManager
    3331
    3432#
     
    3836def construct_ef_writer_options_parser(usage_string):
    3937
     38   solver_list = SolverFactory.services()
     39   solver_list = sorted( filter(lambda x: '_' != x[0], solver_list) )
     40   solver_help = \
     41   "Specify the solver with which to solve scenario sub-problems.  The "      \
     42   "following solver types are currently supported: %s; Default: cplex"
     43   solver_help %= ', '.join( solver_list )
     44
    4045   parser = OptionParser()
    4146   parser.usage=usage_string
    4247
    4348   inputOpts        = OptionGroup( parser, 'Input Options' )
    44    scenarioTreeOpts = OptionGroup( parser, 'Scenario Tree Options' )   
     49   scenarioTreeOpts = OptionGroup( parser, 'Scenario Tree Options' )
    4550   efOpts           = OptionGroup( parser, 'EF Options' )
    4651   solverOpts       = OptionGroup( parser, 'Solver Options' )
     
    7883     dest="scenario_tree_downsample_fraction",
    7984     type="float",
    80      default=1.0)   
     85     default=1.0)
    8186
    8287   efOpts.add_option('--cvar-weight',
     
    117122     default=False)
    118123   solverOpts.add_option('--solver',
    119      help='The type of solver used to solve scenario sub-problems. Default is cplex.',
     124     help=solver_help,
    120125     action='store',
    121126     dest='solver_type',
     
    163168     dest='disable_gc',
    164169     default=False)
    165    otherOpts.add_option('--keep-solver-files',
     170   otherOpts.add_option('-k','--keep-solver-files',
    166171     help='Retain temporary input and output files for solve.',
    167172     action='store_true',
     
    291296        # Call the main ef writer with profiling.
    292297        #
    293         tfile = pyutilib.services.TempfileManager.create_tempfile(suffix=".profile")
     298        tfile = TempfileManager.create_tempfile(suffix=".profile")
    294299        tmp = profile.runctx('run_ef_writer(options,args)',globals(),locals(),tfile)
    295300        p = pstats.Stats(tfile).strip_dirs()
     
    307312        p.print_callers(options.profile)
    308313        p.print_callees(options.profile)
    309         pyutilib.services.TempfileManager.clear_tempfiles()
     314        TempfileManager.clear_tempfiles()
    310315        ans = [tmp, None]
    311316    else:
  • coopr.pysp/trunk/coopr/pysp/phinit.py

    r3480 r3482  
    1010
    1111
     12import gc      # garbage collection control.
    1213import os
     14import pickle  # for serializing
     15import pstats  # for profiling
    1316import sys
    1417
    1518from optparse import OptionParser, OptionGroup
    16 
    17 import pyutilib.services
    18 import pyutilib.misc
    19 
    20 # garbage collection control.
    21 import gc
    2219
    2320# for profiling
     
    2623except ImportError:
    2724    import profile
    28 import pstats
    29 
    30 # for serializing
    31 import pickle
    32 
    33 from coopr.pysp.convergence import *
    34 from coopr.pysp.scenariotree import *
    35 from coopr.pysp.ph import *
    36 from coopr.pysp.ef import *
     25
     26from pyutilib.component.core import ExtensionPoint
     27from pyutilib.misc import import_file
     28from pyutilib.services import TempfileManager
     29
    3730from coopr.opt.base import SolverFactory
    3831from coopr.opt.parallel import SolverManagerFactory
    39 
    40 from pyutilib.component.core import ExtensionPoint
     32from coopr.pysp.convergence import *
     33from coopr.pysp.ef import *
     34from coopr.pysp.ph import *
     35from coopr.pysp.scenariotree import *
    4136from coopr.pysp.solutionwriter import ISolutionWriterExtension
    4237
     
    4742
    4843def construct_ph_options_parser(usage_string):
     44
     45   solver_list = SolverFactory.services()
     46   solver_list = sorted( filter(lambda x: '_' != x[0], solver_list) )
     47   solver_help = \
     48   "Specify the solver with which to solve scenario sub-problems.  The "      \
     49   "following solver types are currently supported: %s; Default: cplex"
     50   solver_help %= ', '.join( solver_list )
    4951
    5052   parser = OptionParser()
     
    215217     default=[])
    216218   solverOpts.add_option('--solver',
    217      help="The type of solver used to solve scenario sub-problems. Default is cplex.",
     219     help=solver_help,
    218220     action="store",
    219221     dest="solver_type",
     
    334336     dest="disable_gc",
    335337     default=False)
    336    otherOpts.add_option('--keep-solver-files',
     338   otherOpts.add_option('-k','--keep-solver-files',
    337339     help="Retain temporary input and output files for scenario sub-problem solves",
    338340     action="store_true",
     
    379381      if options.verbose is True:
    380382         print "Scenario reference model filename="+reference_model_filename
    381       model_import = pyutilib.misc.import_file(reference_model_filename)
     383      model_import = import_file(reference_model_filename)
    382384      if "model" not in dir(model_import):
    383385         print ""
     
    453455      if options.verbose is True:
    454456         print "Scenario reference model filename="+reference_model_filename
    455       model_import = pyutilib.misc.import_file(reference_model_filename)
     457      model_import = import_file(reference_model_filename)
    456458      if "model" not in dir(model_import):
    457459         print "***ERROR: Exiting test driver: No 'model' object created in module "+reference_model_filename
     
    768770        # Call the main PH routine with profiling.
    769771        #
    770         tfile = pyutilib.services.TempfileManager.create_tempfile(suffix=".profile")
     772        tfile = TempfileManager.create_tempfile(suffix=".profile")
    771773        tmp = profile.runctx('exec_ph(options)',globals(),locals(),tfile)
    772774        p = pstats.Stats(tfile).strip_dirs()
     
    783785        p.print_callers(options.profile)
    784786        p.print_callees(options.profile)
    785         pyutilib.services.TempfileManager.clear_tempfiles()
     787        TempfileManager.clear_tempfiles()
    786788        ans = [tmp, None]
    787789    else:
  • coopr.pysp/trunk/coopr/pysp/phserver.py

    r3481 r3482  
    1010
    1111
     12import gc         # garbage collection control.
     13import os
     14import pickle
     15import pstats     # for profiling
    1216import sys
    13 import os
     17import time
     18import traceback
     19
    1420from optparse import OptionParser
    15 
    16 import pyutilib.services
    17 import pyutilib.misc
    18 import textwrap
    19 import traceback
    20 import time
    21 
    22 # Coopr
    23 from coopr.opt.base import SolverFactory
    24 from coopr.pysp.scenariotree import *
    25 from phutils import *
    26 from phobjective import *
    27 
    28 # Pyro
    29 import Pyro.core
    30 import Pyro.naming
    31 from Pyro.errors import PyroError,NamingError
    32 
    33 # garbage collection control.
    34 import gc
    3521
    3622# for profiling
     
    3925except ImportError:
    4026    import profile
    41 import pstats
    42 
    43 import pickle
     27
     28# Coopr
     29from coopr.opt.base import SolverFactory
     30from coopr.pysp.scenariotree import *
     31
     32from pyutilib.misc import import_file
     33from pyutilib.services import TempfileManager
     34
     35# Pyro
     36import Pyro.core
     37import Pyro.naming
     38from Pyro.errors import PyroError, NamingError
     39
     40from phutils import *
     41from phobjective import *
    4442
    4543# disable multi-threading. for a solver server, this
     
    193191def construct_options_parser(usage_string):
    194192
     193   solver_list = SolverFactory.services()
     194   solver_list = sorted( filter(lambda x: '_' != x[0], solver_list) )
     195   solver_help = \
     196   "Specify the solver with which to solve scenario sub-problems.  The "      \
     197   "following solver types are currently supported: %s; Default: cplex"
     198   solver_help %= ', '.join( solver_list )
     199
    195200   parser = OptionParser()
    196201   parser.add_option("--verbose",
     
    219224                     dest="report_weights",
    220225                     default=False)
    221    parser.add_option("--model-directory",
     226   parser.add_option('-m',"--model-directory",
    222227                     help="The directory in which all model (reference and scenario) definitions are stored. Default is \".\".",
    223228                     action="store",
     
    225230                     type="string",
    226231                     default=".")
    227    parser.add_option("--instance-directory",
     232   parser.add_option('-i',"--instance-directory",
    228233                     help="The directory in which all instance (reference and scenario) definitions are stored. Default is \".\".",
    229234                     action="store",
     
    232237                     default=".")
    233238   parser.add_option("--solver",
    234                      help="The type of solver used to solve scenario sub-problems. Default is cplex.",
     239                     help=solver_help,
    235240                     action="store",
    236241                     dest="solver_type",
     
    249254                     type="float",
    250255                     default=None)
    251    parser.add_option("--keep-solver-files",
     256   parser.add_option('-k',"--keep-solver-files",
    252257                     help="Retain temporary input and output files for scenario sub-problem solves",
    253258                     action="store_true",
     
    307312      if options.verbose is True:
    308313         print "Scenario reference model filename="+reference_model_filename
    309       model_import = pyutilib.misc.import_file(reference_model_filename)
     314      model_import = import_file(reference_model_filename)
    310315      if "model" not in dir(model_import):
    311316         print ""
     
    506511        # Call the main PH routine with profiling.
    507512        #
    508         tfile = pyutilib.services.TempfileManager.create_tempfile(suffix=".profile")
     513        tfile = TempfileManager.create_tempfile(suffix=".profile")
    509514        tmp = profile.runctx('exec_ph(options)',globals(),locals(),tfile)
    510515        p = pstats.Stats(tfile).strip_dirs()
     
    521526        p.print_callers(options.profile)
    522527        p.print_callees(options.profile)
    523         pyutilib.services.TempfileManager.clear_tempfiles()
     528        TempfileManager.clear_tempfiles()
    524529        ans = [tmp, None]
    525530    else:
Note: See TracChangeset for help on using the changeset viewer.