Changeset 2452


Ignore:
Timestamp:
Mar 24, 2010 11:46:07 PM (9 years ago)
Author:
jwatson
Message:

Based on new extensive form solution reader, added solve options to efwriter script and pretty-prints of the resulting solution in scenario tree format.

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

Legend:

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

    r2449 r2452  
    500500#
    501501# the main extensive-form writer routine - including read of scenarios/etc.
     502# returns a triple consisting of the scenario tree, master binding instance, and scenario instance map
    502503#
    503504
     
    538539     
    539540      print "***ERROR: Failed to load scenario reference model from file="+reference_model_filename
    540       return
     541      return None, None, None
    541542
    542543   try:
     
    548549     
    549550      print "***ERROR: Failed to load scenario reference instance data from file="+reference_scenario_filename
    550       return            
     551      return None, None, None            
    551552
    552553   #
     
    668669   print ""
    669670
     671   return scenario_tree, binding_instance, scenario_instances
     672
    670673def create_and_write_ef(scenario_tree, scenario_instances, output_filename):
    671674
  • coopr.pysp/trunk/coopr/pysp/ef_writer_script.py

    r2439 r2452  
    2323from coopr.pysp.ef import *
    2424
     25from coopr.opt.base import SolverFactory
     26from coopr.opt.parallel import SolverManagerFactory
     27
    2528#
    2629# utility method to construct an option parser for ef writer arguments
     
    7073                     type="string",
    7174                     default="efout.lp")
     75   parser.add_option("--solve",
     76                     help="Following write of the extensive form model, solve it.",
     77                     action="store_true",
     78                     dest="solve_ef",
     79                     default=False)
     80   parser.add_option("--solver",
     81                     help="The type of solver used to solve scenario sub-problems. Default is cplex.",
     82                     action="store",
     83                     dest="solver_type",
     84                     type="string",
     85                     default="cplex")
     86   parser.add_option("--solver-manager",
     87                     help="The type of solver manager used to coordinate scenario sub-problem solves. Default is serial.",
     88                     action="store",
     89                     dest="solver_manager_type",
     90                     type="string",
     91                     default="serial")
     92   parser.add_option("--solver-options",
     93                     help="Solver options for the extension form problem",
     94                     action="append",
     95                     dest="solver_options",
     96                     type="string",
     97                     default=[])
     98   parser.add_option("--mipgap",
     99                     help="Specifies the mipgap for the EF solve",
     100                     action="store",
     101                     dest="mipgap",
     102                     type="float",
     103                     default=None)   
     104   parser.add_option("--output-solver-log",
     105                     help="Output solver log during the extensive form solve",
     106                     action="store_true",
     107                     dest="output_solver_log",
     108                     default=False)   
    72109   parser.add_option("--profile",
    73110                     help="Enable profiling of Python code.  The value of this option is the number of functions that are summarized.",
     
    98135      risk_alpha = options.risk_alpha
    99136
    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,
    104                          generate_weighted_cvar, cvar_weight, risk_alpha)
     137   scenario_tree, binding_instance, scenario_instances = write_ef_from_scratch(os.path.expanduser(options.model_directory),
     138                                                                               os.path.expanduser(options.instance_directory),
     139                                                                               os.path.expanduser(options.output_file),
     140                                                                               options.verbose,
     141                                                                               generate_weighted_cvar, cvar_weight, risk_alpha)
     142
     143   if options.solve_ef is True:
     144
     145      ef_solver = SolverFactory(options.solver_type)
     146      if ef_solver is None:
     147         raise ValueError, "Failed to create solver of type="+options.solver_type+" for use in extensive form solve"
     148      if len(options.solver_options) > 0:
     149         print "Initializing ef solver with options="+str(options.solver_options)         
     150         ef_solver.set_options("".join(options.solver_options))
     151      if options.mipgap is not None:
     152         if (options.mipgap < 0.0) or (options.mipgap > 1.0):
     153            raise ValueError, "Value of the mipgap parameter for the EF solve must be on the unit interval; value specified=" + `options.mipgap`
     154         else:
     155            ef_solver.mipgap = options.mipgap
     156
     157      ef_solver_manager = SolverManagerFactory(options.solver_manager_type)
     158      if ef_solver is None:
     159         raise ValueError, "Failed to create solver manager of type="+options.solver_type+" for use in extensive form solve"
     160
     161      print "Queuing extensive form solve"
     162      ef_action_handle = ef_solver_manager.queue(os.path.expanduser(options.output_file), opt=ef_solver, warmstart=False, tee=options.output_solver_log)
     163      print "Waiting for extensive form solve"
     164      ef_results = ef_solver_manager.wait_for(ef_action_handle)
     165      load_ef_solution(ef_results, binding_instance, scenario_instances)
     166      scenario_tree.snapshotSolutionFromInstances(scenario_instances)
     167      print ""
     168      print "Extensive form solution:"
     169      scenario_tree.pprintSolution()
     170      print ""
     171      print "Extensive form costs:"
     172      scenario_tree.pprintCosts(scenario_instances)
    105173
    106174def run(args=None):
Note: See TracChangeset for help on using the changeset viewer.