Changeset 2414


Ignore:
Timestamp:
Mar 8, 2010 10:18:14 PM (9 years ago)
Author:
jwatson
Message:

Various EF writer changes in PySP, including:
1) Added a --verbose option, and by default am disabling echo of the scenario tree.
2) Changed the default behavior, due to recent experience with lot sizing and wind farm examples, to enable garbage collection.
3) Added a --disable-gc option to disable garbage collection, set to False by default.

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

Legend:

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

    r2413 r2414  
    247247#
    248248
    249 def write_ef_from_scratch(model_directory, instance_directory, output_filename, \
     249def write_ef_from_scratch(model_directory, instance_directory, output_filename,
     250                          verbose_output,
    250251                          generate_weighted_cvar, cvar_weight, risk_alpha):
    251252
     
    328329   # print the input tree for validation/information purposes.
    329330   #
    330    scenario_tree.pprint()
     331   if verbose_output is True:
     332      scenario_tree.pprint()
    331333
    332334   #
  • coopr.pysp/trunk/coopr/pysp/ef_writer_script.py

    r2248 r2414  
    2424
    2525#
    26 # Setup command-line options
     26# utility method to construct an option parser for ef writer arguments
    2727#
    2828
    29 parser = OptionParser()
    30 parser.add_option("--model-directory",
    31                   help="The directory in which all model (reference and scenario) definitions are stored. Default is \".\".",
    32                   action="store",
    33                   dest="model_directory",
    34                   type="string",
    35                   default=".")
    36 parser.add_option("--instance-directory",
    37                   help="The directory in which all instance (reference and scenario) definitions are stored. Default is \".\".",
    38                   action="store",
    39                   dest="instance_directory",
    40                   type="string",
    41                   default=".")
    42 parser.add_option("--generate-weighted-cvar",
    43                   help="Add a weighted CVaR term to the primary objective",
    44                   action="store_true",
    45                   dest="generate_weighted_cvar",
    46                   default=False)
    47 parser.add_option("--cvar-weight",
    48                   help="The weight associated with the CVaR term in the risk-weighted objective formulation.",
    49                   action="store",
    50                   dest="cvar_weight",
    51                   type="float",
    52                   default=1.0)
    53 parser.add_option("--risk-alpha",
    54                   help="The probability threshold associated with cvar (or any future) risk-oriented performance metrics.",
    55                   action="store",
    56                   dest="risk_alpha",
    57                   type="float",
    58                   default=0.95)
    59 parser.add_option("--output-file",
    60                   help="Specify the name of the extensive form output file",
    61                   action="store",
    62                   dest="output_file",
    63                   type="string",
    64                   default="efout.lp")
    65 parser.add_option("--profile",
    66                   help="Enable profiling of Python code.  The value of this option is the number of functions that are summarized.",
    67                   action="store",
    68                   dest="profile",
    69                   default=0)
    70 parser.usage="efwriter [options]"
     29def construct_ef_writer_options_parser(usage_string):
    7130
     31   parser = OptionParser()
     32   parser.add_option("--verbose",
     33                     help="Generate verbose output, beyond the usual status output. Default is False.",
     34                     action="store_true",
     35                     dest="verbose",
     36                     default=False)
     37   parser.add_option("--model-directory",
     38                     help="The directory in which all model (reference and scenario) definitions are stored. Default is \".\".",
     39                     action="store",
     40                     dest="model_directory",
     41                     type="string",
     42                     default=".")
     43   parser.add_option("--instance-directory",
     44                     help="The directory in which all instance (reference and scenario) definitions are stored. Default is \".\".",
     45                     action="store",
     46                     dest="instance_directory",
     47                     type="string",
     48                     default=".")
     49   parser.add_option("--generate-weighted-cvar",
     50                     help="Add a weighted CVaR term to the primary objective",
     51                     action="store_true",
     52                     dest="generate_weighted_cvar",
     53                     default=False)
     54   parser.add_option("--cvar-weight",
     55                     help="The weight associated with the CVaR term in the risk-weighted objective formulation.",
     56                     action="store",
     57                     dest="cvar_weight",
     58                     type="float",
     59                     default=1.0)
     60   parser.add_option("--risk-alpha",
     61                     help="The probability threshold associated with cvar (or any future) risk-oriented performance metrics.",
     62                     action="store",
     63                     dest="risk_alpha",
     64                     type="float",
     65                     default=0.95)
     66   parser.add_option("--output-file",
     67                     help="Specify the name of the extensive form output file",
     68                     action="store",
     69                     dest="output_file",
     70                     type="string",
     71                     default="efout.lp")
     72   parser.add_option("--profile",
     73                     help="Enable profiling of Python code.  The value of this option is the number of functions that are summarized.",
     74                     action="store",
     75                     dest="profile",
     76                     default=0)
     77   parser.add_option("--disable-gc",
     78                     help="Disable the python garbage collecter. Default is False.",
     79                     action="store_true",
     80                     dest="disable_gc",
     81                     default=False)
     82   parser.usage=usage_string
     83
     84   return parser
     85   
    7286def run_ef_writer(options, args):
    7387
     
    8498      risk_alpha = options.risk_alpha
    8599
    86    write_ef_from_scratch(os.path.expanduser(options.model_directory), os.path.expanduser(options.instance_directory), os.path.expanduser(options.output_file), \
     100   write_ef_from_scratch(os.path.expanduser(options.model_directory),
     101                         os.path.expanduser(options.instance_directory),
     102                         os.path.expanduser(options.output_file),
     103                         options.verbose,
    87104                         generate_weighted_cvar, cvar_weight, risk_alpha)
    88 
    89    return
    90105
    91106def run(args=None):
     
    96111    #
    97112
    98     # for a one-pass execution, garbage collection doesn't make
    99     # much sense - so I'm disabling it. Because: It drops the run-time
    100     # by a factor of 3-4 on bigger instances.
    101     gc.disable()       
    102 
    103113    #
    104114    # Parse command-line options.
    105115    #
    106116    try:
    107        (options, args) = parser.parse_args(args=args)
     117       options_parser = construct_ef_writer_options_parser("efwriter [options]")
     118       (options, args) = options_parser.parse_args(args=args)
    108119    except SystemExit:
    109120       # the parser throws a system exit if "-h" is specified - catch
    110121       # it to exit gracefully.
    111122       return
     123
     124    if options.disable_gc is True:
     125       gc.disable()
     126    else:
     127       gc.enable()
    112128
    113129    if options.profile > 0:
     
    140156
    141157    gc.enable()
     158   
    142159    return ans
    143160
Note: See TracChangeset for help on using the changeset viewer.