Changeset 3035


Ignore:
Timestamp:
Sep 21, 2010 11:49:05 AM (11 years ago)
Author:
jwatson
Message:

Added boundary issue checks in computeconf script, in cases where there aren't enough samples to populate n_g batches to compute a confidence interval.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • coopr.pysp/trunk/scripts/computeconf

    r3034 r3035  
    155155   reference_model, reference_instance, full_scenario_tree, scenario_tree_instance = load_reference_and_scenario_models(options)
    156156   if (reference_model is None) or (reference_instance is None) or (full_scenario_tree is None) or (scenario_tree_instance is None):
    157       print "***ERROR: Failed to initialize reference model/instance pair and/or the scenario tree."
    158       sys.exit(1)
     157      raise RuntimeError, "***ERROR: Failed to initialize reference model/instance pair and/or the scenario tree."
    159158
    160159   # verify that we're dealing with a minimization problem - the script might be easily made to work with
     
    163162   objective = reference_model.active_components()[Objective][objective_name]
    164163   if objective.sense != minimize:
    165       print "***ERROR: Confidence intervals are available only for minimization problems"
    166       sys.exit(1)
     164      raise RuntimeError, "***ERROR: Confidence intervals are available only for minimization problems"
    167165
    168166   print "Starting confidence interval calculation..."
     
    170168   scenario_count = len(full_scenario_tree._stages[-1]._tree_nodes)
    171169   if len(full_scenario_tree._stages) > 2:
    172       print "***ERROR: Confidence intervals are available only for two stage stochastic programs;"+str(len(full_scenario_tree._stages))+" stages specified"
    173       sys.exit(1)
     170      raise RuntimeError, "***ERROR: Confidence intervals are available only for two stage stochastic programs;"+str(len(full_scenario_tree._stages))+" stages specified"
    174171
    175172   # randomly permute the indices to extract a subset to compute xhat.
     
    184181   num_scenarios_per_sample = int((scenario_count - num_scenarios_for_solution) / n_g) # 'n' in Morton's slides
    185182   wasted_scenarios = scenario_count - num_scenarios_for_solution - n_g * num_scenarios_per_sample
     183
     184   if num_scenarios_per_sample is 0:
     185      raise RuntimeError, "Computed number of scenarios per sample group equals 0 - "+str(scenario_count - num_scenarios_for_solution)+" scenarios cannot be divided into "+str(n_g)+" groups!"
    186186   
    187187   print "Problem contains "+str(scenario_count)+" scenarios, of which "+str(num_scenarios_for_solution)+" will be used to find a solution xhat."
     
    193193   # directly, mainly out of convenience - we're leveraging the code in ph_for_bundle to create the
    194194   # scenario tree and scenario instances.
     195   print ""
    195196   print "Loading scenario instances and initializing scenario tree for xhat scenario bundle."
    196197   xhat_ph = ph_for_bundle(0, num_scenarios_for_solution, reference_model, full_scenario_tree, reference_instance, scenario_tree_instance, index_list, options)
     
    243244      start_index = num_scenarios_for_solution + (k-1)*num_scenarios_per_sample
    244245      stop_index = start_index + num_scenarios_per_sample - 1
     246      print ""
    245247      print "Computing statistics for sample k="+str(k)+"."
    246248      if options.verbose is True:
     
    298300   g_var = g_var / (n_g - 1)    # sample var
    299301   print ""
     302   print "Raw results:"
    300303   print "g_bar=",g_bar
    301304   print "g_stddev=",math.sqrt(g_var)
     
    303306
    304307   if n_g in t_table_values:
    305       print ""     
     308      print ""
     309      print "Results summary:"
    306310      t_table_entries = t_table_values[n_g]
    307311      for key in sorted(t_table_entries.keys()):
     
    359363                                         scenariobundlelist=scenarios_to_bundle)
    360364   if scenario_tree_for_soln.validate() is False:
    361       print "***ERROR: Bundled scenario tree is invalid!!!"
    362       sys.exit(1)
     365      raise RuntimeError, "***ERROR: Bundled scenario tree is invalid!!!"
    363366
    364367   ph = create_ph_from_scratch(options, reference_model, reference_instance, scenario_tree_for_soln)
Note: See TracChangeset for help on using the changeset viewer.