Changeset 2990


Ignore:
Timestamp:
Sep 4, 2010 1:28:07 PM (11 years ago)
Author:
jwatson
Message:

Added option to PySP scripts to allow for output of the scenario tree solutions in CSV file format. Option is --solution-writer=coopr.pysp.csvsolutionwriter.py. This also required some overdue refactoring of the PySP code, plus re-work of how the post-PH extensive form solution is displayed.

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

Legend:

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

    r2405 r2990  
    2323from phsolvermanager import *
    2424from phobjective import *
     25from solutionwriter import *
    2526
    2627pyutilib.component.core.PluginGlobals.pop_env()
  • coopr.pysp/trunk/coopr/pysp/ef.py

    r2832 r2990  
    671671   return scenario_tree, binding_instance, scenario_instances
    672672
     673#
     674# does what it says, with the added functionality of returning the master binding instance.
     675#
     676
    673677def create_and_write_ef(scenario_tree, scenario_instances, output_filename):
    674678
     
    686690
    687691   print "Total execution time=%8.2f seconds" %(end_time - start_time)
     692
     693   return binding_instance
    688694
    689695#
  • coopr.pysp/trunk/coopr/pysp/ef_writer_script.py

    r2986 r2990  
    2929from coopr.opt.parallel import SolverManagerFactory
    3030
     31from pyutilib.component.core import ExtensionPoint
     32from coopr.pysp.solutionwriter import ISolutionWriterExtension
     33
    3134#
    3235# utility method to construct an option parser for ef writer arguments
     
    105108                     type="float",
    106109                     default=None)   
     110   parser.add_option("--solution-writer",
     111                     help="The plugin invoked to write the scenario tree solution. Defaults to the empty list.",
     112                     action="append",
     113                     dest="solution_writer",
     114                     type="string",
     115                     default = [])
    107116   parser.add_option("--output-solver-log",
    108117                     help="Output solver log during the extensive form solve.",
     
    142151      cvar_weight = options.cvar_weight
    143152      risk_alpha = options.risk_alpha
     153
     154   # validate the solution writer plugin exists, to avoid a lot of wasted work.
     155   for solution_writer_name in options.solution_writer:
     156      print "Trying to import solution writer="+solution_writer_name
     157      __import__(solution_writer_name)
     158      print "Module successfully loaded"
    144159
    145160   scenario_tree, binding_instance, scenario_instances = write_ef_from_scratch(os.path.expanduser(options.model_directory),
     
    184199      load_ef_solution(ef_results, binding_instance, scenario_instances)
    185200      scenario_tree.snapshotSolutionFromInstances(scenario_instances)
     201
     202      # handle output of solution from the scenario tree.
    186203      print ""
    187204      print "Extensive form solution:"
     
    190207      print "Extensive form costs:"
    191208      scenario_tree.pprintCosts(scenario_instances)
     209
     210      solution_writer_plugins = ExtensionPoint(ISolutionWriterExtension)
     211      for plugin in solution_writer_plugins:
     212         plugin.write(scenario_tree, "ef")
    192213
    193214def run(args=None):
  • coopr.pysp/trunk/coopr/pysp/ph.py

    r2801 r2990  
    13011301      print "Final number of continuous variables fixed="+str(self._total_fixed_continuous_vars)+" (total="+str(self._total_continuous_vars)+")"
    13021302
     1303      # populate the scenario tree solution from the instances - to ensure consistent state
     1304      # across the scenario tree instance and the scenario instances.
     1305      self._scenario_tree.snapshotSolutionFromInstances(self._instances)     
     1306
    13031307      print "Final variable values:"
    13041308      self.pprint(False, False, True, True, output_only_statistics=self._report_only_statistics)
  • coopr.pysp/trunk/coopr/pysp/phinit.py

    r2918 r2990  
    3939from coopr.opt.parallel import SolverManagerFactory
    4040
    41 #
    42 # utility method to construct an option parser for ph arguments, to be
    43 # supplied as an argument to the runph method.
     41from pyutilib.component.core import ExtensionPoint
     42from coopr.pysp.solutionwriter import ISolutionWriterExtension
     43
     44#
     45# utility method to construct an option parser for ph arguments,
     46# to be supplied as an argument to the runph method.
    4447#
    4548
     
    120123                     type="float",
    121124                     default=None)
     125   parser.add_option("--solution-writer",
     126                     help="The plugin invoked to write the scenario tree solution. Defaults to the empty list.",
     127                     action="append",
     128                     dest="solution_writer",
     129                     type="string",
     130                     default = [])
    122131   parser.add_option("--max-iterations",
    123132                     help="The maximal number of PH iterations. Default is 100.",
     
    364373
    365374#
    366 # Create a PH object from a checkpoint.
     375# Create a PH object from a (pickl) checkpoint. Experimental at the moment.
    367376#
    368377def create_ph_from_checkpoint(options):
     
    404413
    405414#
    406 # Create a PH object from a checkpoint.
    407 #
     415# Create a PH object from scratch.
     416#
     417
    408418def create_ph_from_scratch(options, reference_model, reference_instance, scenario_tree):
    409419
     
    540550      ph.print_time_stats()
    541551
    542    #
    543    # write the extensive form, accounting for any fixed variables.
     552   solution_writer_plugins = ExtensionPoint(ISolutionWriterExtension)
     553   for plugin in solution_writer_plugins:
     554      plugin.write(ph._scenario_tree, "ph")
     555
     556   # store the binding instance, if created, in order to load
     557   # the solution back into the scenario tree.
     558   binding_instance = None
     559
     560   #
     561   # write the extensive form, accounting (implicitly) for any fixed variables.
    544562   #
    545563   if (options.write_ef is True) or (options.solve_ef is True):
     
    547565      print "Writing EF for remainder problem"
    548566      print ""
    549       create_and_write_ef(ph._scenario_tree, ph._instances, os.path.expanduser(options.ef_output_file))
    550 
    551    #
    552    # solve the extensive form.
     567      binding_instance = create_and_write_ef(ph._scenario_tree, ph._instances, os.path.expanduser(options.ef_output_file))
     568
     569   #
     570   # solve the extensive form and load the solution back into the PH scenario tree.
     571   # contents from the PH solve will obviously be over-written!
    553572   #
    554573   if options.solve_ef is True:
     
    574593
    575594      print "Queuing extensive form solve"
    576       ef_action_handle = ef_solver_manager.queue(os.path.expanduser(options.ef_output_file), opt=ef_solver, warmstart=False, tee=options.output_ef_solver_log)
     595      ef_action_handle = ef_solver_manager.queue(os.path.expanduser(options.ef_output_file), opt=ef_solver, tee=options.output_ef_solver_log)
    577596      print "Waiting for extensive form solve"
    578597      ef_results = ef_solver_manager.wait_for(ef_action_handle)
    579       print "Extensive form solve results:"
    580       ef_results.write(num=1)
     598
     599      load_ef_solution(ef_results, binding_instance, ph._instances)
     600      ph._scenario_tree.snapshotSolutionFromInstances(ph._instances)
     601
     602      print ""
     603      print "Extensive form solution:"
     604      ph._scenario_tree.pprintSolution()
     605      print ""
     606      print "Extensive form costs:"
     607      ph._scenario_tree.pprintCosts(ph._instances)
     608
     609      solution_writer_plugins = ExtensionPoint(ISolutionWriterExtension)
     610      for plugin in solution_writer_plugins:
     611         plugin.write(ph._scenario_tree, "postphef")
    581612   
    582613#
    583 # The main PH initialization / runner routine.
     614# The main PH initialization / runner routine. Really only branches based on
     615# the construction source - a checkpoint or from scratch.
    584616#
    585617
     
    587619
    588620   ph = None
     621
     622   # validate the solution writer plugin exists, to avoid a lot of wasted work.
     623   for solution_writer_name in options.solution_writer:
     624      print "Trying to import solution writer="+solution_writer_name
     625      __import__(solution_writer_name)
     626      print "Module successfully loaded"
    589627
    590628   # if we are restoring from a checkpoint file, do so - otherwise, construct PH from scratch.
    591629   if len(options.restore_from_checkpoint) > 0:
    592 
    593630      ph = create_ph_from_checkpoint(options)
    594      
     631
    595632   else:
    596 
    597633      reference_model, reference_instance, scenario_tree, scenario_tree_instance = load_reference_and_scenario_models(options)
    598634      if reference_model is None or reference_instance is None or scenario_tree is None:
     
    605641
    606642   run_ph(options, ph)
     643
     644#
     645# the main driver routine for the runph script.
     646#
    607647
    608648def run(args=None):
     
    623663       # it to exit gracefully.
    624664       return
     665    #
     666    # Control the garbage collector - more critical than I would like at the moment.
     667    #
    625668
    626669    if options.disable_gc is True:
     
    628671    else:
    629672       gc.enable()
     673
     674    #
     675    # Run PH - precise invocation depends on whether we want profiling output.
     676    #
    630677
    631678    if options.profile > 0:
Note: See TracChangeset for help on using the changeset viewer.