Changeset 2837


Ignore:
Timestamp:
Jul 22, 2010 11:12:02 AM (9 years ago)
Author:
jwatson
Message:

Various touch-ups to compute conf PySP script - it looks good to go!

File:
1 edited

Legend:

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

    r2835 r2837  
    4949                                     action="store_true",
    5050                                     dest="solve_hatx_with_ef_only",
    51                                      default=False)     
     51                                     default=False)
     52      conf_options_parser.add_option("--random-seed",
     53                                     help="Seed the random number generator used to select samples. Defaults to 0, indicating time seed will be used.",
     54                                     action="store",
     55                                     dest="random_seed",
     56                                     type="int",
     57                                     default=0)
     58      conf_options_parser.add_option("--append-file",
     59                                     help="File to which summary run information is appended, for output tracking purposes.",
     60                                     action="store",
     61                                     dest="append_file",
     62                                     type="string",
     63                                     default=None)     
     64
    5265      (options, args) = conf_options_parser.parse_args(args=args)
    5366   except SystemExit:
     
    5669      return
    5770
    58    # HEY!!!!! Look at this!!!!
    59    random.seed(17)
     71   if options.random_seed > 0:
     72      random.seed(random_seed)
    6073
    6174   reference_model, reference_instance, full_scenario_tree, si = load_reference_and_scenario_models(options)
     
    6376   # HERE: If we get an objective function with maximization, bail right away.
    6477
    65    print "Confidence Interval Calculations"
     78   print "Starting confidence interval calculations"
    6679#   print "full_scenario_tree pprint:"
    6780#   full_scenario_tree.pprint()
     
    8194   wastedscenarios = scenariocount - num_scenarios_for_solution - n_g * num_scenarios_per_sample
    8295   
    83    print scenariocount, "Scenarios,from which ",num_scenarios_for_solution, " are to be used to find a solution and "
    84    print n_g," groups of ",num_scenarios_per_sample," are to be used for the confidence interval."
     96   print str(scenariocount)+" scenarios, from which "+str(num_scenarios_for_solution)+" will be used to find a solution."
     97   print str(n_g)+" groups of "+str(num_scenarios_per_sample)+" will be used to compute the confidence interval."
    8598   if wastedscenarios > 0:
    8699      print "(so ",wastedscenarios," will not be used.)"
     
    88101   # create ph for finding the solution
    89102   hatxph = ph_for_bundle(0, num_scenarios_for_solution, reference_model, full_scenario_tree, reference_instance, si, IndList, options)
     103
     104   xhatobj = None
    90105
    91106   if options.solve_hatx_with_ef_only is True:
     
    96111      # the following is just for printing things in an organized manner.
    97112      hatxph._scenario_tree.snapshotSolutionFromInstances(hatxph._instances)
    98       hatxph._scenario_tree.pprintSolution()
    99       hatxph._scenario_tree.pprintCosts(hatxph._instances)
     113#      hatxph._scenario_tree.pprintSolution()
     114#      hatxph._scenario_tree.pprintCosts(hatxph._instances)
     115      xhatobj = float(ef_results.solution(0).objective['f'].value)  ## DLW to JPW: we need the gap too
    100116   else:
    101117      print "SOLVING HATX VIA PH!"
     
    108124   G_supk_of_hatx = [] # really not always needed... http://www.eecs.berkeley.edu/~mhoemmen/cs194/Tutorials/variance.pdf
    109125   Gbar = 0
     126   sum_E_f_of_Hatx = 0
    110127   for k in range(1, n_g+1):
    111128      start = num_scenarios_for_solution + (k-1)*num_scenarios_per_sample
    112129      stop = start + num_scenarios_per_sample - 1
    113       print "Sample k=", k
     130      print "Computing statistics for sample k="+str(k)
    114131     
    115132      # NOTE: We'll never run this ph - code could be refactored
     
    123140      # the following is just for printing things in an organized manner.
    124141      phforGk._scenario_tree.snapshotSolutionFromInstances(phforGk._instances)
    125       phforGk._scenario_tree.pprintSolution()
    126       phforGk._scenario_tree.pprintCosts(phforGk._instances)           
     142#      phforGk._scenario_tree.pprintSolution()
     143#      phforGk._scenario_tree.pprintCosts(phforGk._instances)           
    127144     
    128145#      print "HEY JPW!!! we need to add or subtract the gap"
     
    194211      G_supk_of_hatx.append(E_f_of_Hatx - E_f_of_xstar_bound)   
    195212      Gbar += G_supk_of_hatx[k-1]
     213      sum_E_f_of_Hatx += E_f_of_Hatx
    196214   Gbar = Gbar / n_g
    197215   # second pass for variance calculation (because we like storing the G_supk)
     
    202220   print "Gbar=",Gbar
    203221   print "StdG=",math.sqrt(VarG)
     222   print "Average f(hatx)=",sum_E_f_of_Hatx / n_g
     223
     224
     225   if options.append_file is not None:
     226      output_file = open(options.append_file, "a")
     227      print >>output_file, "instancedirectory, ",options.instance_directory, ", seed, ",options.random_seed, ", N, ",scenariocount, ", hatn, ",num_scenarios_for_solution, ", n_g, ", options.n_g, ", EoffofHatx, ", sum_E_f_of_Hatx / n_g, ", gbar, ", Gbar, ", sg, ", math.sqrt(VarG), ", objforhatx, ", xhatobj, ", n,",num_scenarios_per_sample
     228      output_file.close()
     229   
    204230
    205231#==============================================   
     
    210236      scenarios_to_bundle.append(full_scenario_tree._scenarios[IndList[i]]._name)
    211237
    212    print "Scenarios to bundle:"+str(scenarios_to_bundle)
     238#   print "Scenarios to bundle:"+str(scenarios_to_bundle)
    213239
    214240   scenario_tree_for_soln = ScenarioTree(scenarioinstance=reference_instance,
     
    219245      print "***ERROR: Bundle Scenario tree is invalid****"
    220246      sys.exit(0)
    221    else:
    222       print "Scenario tree for solution is valid!"
    223 
    224247   ph = create_ph_from_scratch(options, reference_model, reference_instance, scenario_tree_for_soln)
    225248   return ph
     
    313336#=================================================================================================
    314337# JPW: Any executable code can be placed at the end of a file, and it will be executed.
    315 print "STARTING TO RUN"
    316338run()
    317 print "DONE"
Note: See TracChangeset for help on using the changeset viewer.