Changeset 3261


Ignore:
Timestamp:
Nov 29, 2010 5:26:46 PM (10 years ago)
Author:
jwatson
Message:

Adding two options to the runef and runph pysp scripts, to facilitate scenario downsampling - the case where you have a big tree, but you don't want to use it all.

The options are:
--scenario-tree-downsample-fraction=X
--scenario-tree-random-seed

The options are fairly self-explanatory - the only possible nuance is that the downsample fraction is the fraction of scenarios retained.

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

Legend:

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

    r3217 r3261  
    508508
    509509def write_ef_from_scratch(model_directory, instance_directory, output_filename,
    510                           verbose_output, linearize,
     510                          verbose_output, linearize_expressions, tree_downsample_fraction, tree_random_seed,
    511511                          generate_weighted_cvar, cvar_weight, risk_alpha):
    512512
     
    577577
    578578   #
     579   # compress/down-sample the scenario tree, if operation is required.
     580   #
     581   if tree_downsample_fraction < 1.0:
     582       
     583      scenario_tree.downsample(tree_downsample_fraction, tree_random_seed, verbose_output)   
     584
     585   #
    579586   # print the input tree for validation/information purposes.
    580587   #
     
    626633                                                      verbose=verbose_output,
    627634                                                      preprocess=True,
    628                                                       linearize=linearize)
     635                                                      linearize=linearize_expressions)
    629636
    630637      scenario_instances[scenario._name] = scenario_instance
  • coopr.pysp/trunk/coopr/pysp/ef_writer_script.py

    r3155 r3261  
    4141   parser.usage=usage_string
    4242
    43    inputOpts  = OptionGroup( parser, 'Input Options' )
    44    efOpts     = OptionGroup( parser, 'EF Options' )
    45    solverOpts = OptionGroup( parser, 'Solver Options' )
    46    outputOpts = OptionGroup( parser, 'Output Options' )
    47    otherOpts  = OptionGroup( parser, 'Other Options' )
     43   inputOpts        = OptionGroup( parser, 'Input Options' )
     44   scenarioTreeOpts = OptionGroup( parser, 'Scenario Tree Options' )   
     45   efOpts           = OptionGroup( parser, 'EF Options' )
     46   solverOpts       = OptionGroup( parser, 'Solver Options' )
     47   outputOpts       = OptionGroup( parser, 'Output Options' )
     48   otherOpts        = OptionGroup( parser, 'Other Options' )
    4849   parser.add_option_group( inputOpts )
     50   parser.add_option_group( scenarioTreeOpts )
    4951   parser.add_option_group( efOpts )
    5052   parser.add_option_group( solverOpts )
     
    6466     type='string',
    6567     default='.')
     68
     69   scenarioTreeOpts.add_option('--scenario-tree-seed',
     70     help="The random seed associated with manipulation operations on the scenario tree (e.g., down-sampling). Default is 0, indicating unassigned.",
     71     action="store",
     72     dest="scenario_tree_random_seed",
     73     type="int",
     74     default=None)
     75   scenarioTreeOpts.add_option('--scenario-tree-downsample-fraction',
     76     help="The proportion of the scenarios in the scenario tree that are actually used. Specific scenarios are selected at random. Default is 1.0, indicating no down-sampling.",
     77     action="store",
     78     dest="scenario_tree_downsample_fraction",
     79     type="float",
     80     default=1.0)   
    6681
    6782   efOpts.add_option('--cvar-weight',
     
    192207                                                                               options.verbose,
    193208                                                                               options.linearize_expressions,
     209                                                                               options.scenario_tree_downsample_fraction,
     210                                                                               options.scenario_tree_random_seed,
    194211                                                                               generate_weighted_cvar, cvar_weight, risk_alpha)
    195212
  • coopr.pysp/trunk/coopr/pysp/phinit.py

    r3168 r3261  
    5151   parser.usage = usage_string
    5252
    53    inputOpts       = OptionGroup( parser, 'Input Options' )
    54    phOpts          = OptionGroup( parser, 'PH Options' )
    55    solverOpts      = OptionGroup( parser, 'Solver Options' )
    56    postprocessOpts = OptionGroup( parser, 'Postprocessing Options' )   
    57    outputOpts      = OptionGroup( parser, 'Output Options' )
    58    otherOpts       = OptionGroup( parser, 'Other Options' )
     53   # NOTE: these groups should eventually be queried from the PH, scenario tree, etc. classes (to facilitate re-use).
     54   inputOpts        = OptionGroup( parser, 'Input Options' )
     55   scenarioTreeOpts = OptionGroup( parser, 'Scenario Tree Options' )
     56   phOpts           = OptionGroup( parser, 'PH Options' )
     57   solverOpts       = OptionGroup( parser, 'Solver Options' )
     58   postprocessOpts  = OptionGroup( parser, 'Postprocessing Options' )   
     59   outputOpts       = OptionGroup( parser, 'Output Options' )
     60   otherOpts        = OptionGroup( parser, 'Other Options' )
    5961   parser.add_option_group( inputOpts )
     62   parser.add_option_group( scenarioTreeOpts )
    6063   parser.add_option_group( phOpts )
    6164   parser.add_option_group( solverOpts )
     
    8184     dest="bounds_cfgfile",
    8285     default=None)
     86
     87   scenarioTreeOpts.add_option('--scenario-tree-seed',
     88     help="The random seed associated with manipulation operations on the scenario tree (e.g., down-sampling). Default is 0, indicating unassigned.",
     89     action="store",
     90     dest="scenario_tree_random_seed",
     91     type="int",
     92     default=None)
     93   scenarioTreeOpts.add_option('--scenario-tree-downsample-fraction',
     94     help="The proportion of the scenarios in the scenario tree that are actually used. Specific scenarios are selected at random. Default is 1.0, indicating no down-sampling.",
     95     action="store",
     96     dest="scenario_tree_downsample_fraction",
     97     type="float",
     98     default=1.0)   
    8399
    84100   phOpts.add_option('-r','--default-rho',
     
    412428   scenario_tree = ScenarioTree(scenarioinstance=reference_instance,
    413429                                scenariotreeinstance=scenario_tree_instance)
     430
     431   #
     432   # compress/down-sample the scenario tree, if operation is required.
     433   #
     434   if options.scenario_tree_downsample_fraction < 1.0:
     435       
     436      scenario_tree.downsample(options.scenario_tree_downsample_fraction, options.scenario_tree_random_seed, options.verbose)
    414437
    415438   return reference_model, reference_instance, scenario_tree, scenario_tree_instance
  • coopr.pysp/trunk/coopr/pysp/scenariotree.py

    r3218 r3261  
    1616
    1717from math import fabs
     18
     19import random
    1820
    1921from coopr.pyomo import *
     
    654656            probability = probability * tree_node._conditional_probability
    655657         scenario._probability = probability
     658
     659   #
     660   # utility for automatically selecting a proportion of scenarios from the
     661   # tree to retain, eliminating the rest.
     662   #
     663
     664   def downsample(self, fraction_to_retain, random_seed, verbose=False):
     665
     666      random.seed(random_seed)
     667
     668      random_sequence=range(0,len(self._scenarios))
     669      random.shuffle(random_sequence)
     670
     671      number_to_retain = max(int(round(float(len(random_sequence)*fraction_to_retain))), 1)
     672
     673      scenario_bundle_list = []
     674      for i in range(0,number_to_retain):
     675         scenario_bundle_list.append(self._scenarios[random_sequence[i]]._name)
     676
     677      if verbose is True:
     678         print "Downsampling scenario tree - retained scenarios: "+str(scenario_bundle_list)
     679
     680      self.compress(scenario_bundle_list)
     681
    656682
    657683   #
Note: See TracChangeset for help on using the changeset viewer.