Changeset 2446


Ignore:
Timestamp:
Mar 22, 2010 9:18:10 PM (9 years ago)
Author:
jwatson
Message:

Added --output-scenario-tree-solution option to PySP runph script, which will:
1) Create a solution from the node averages in a scenario tree -and-
2) Output the full solution in scenario tree format (which includes the leaf nodes).

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

Legend:

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

    r2442 r2446  
    372372      self._output_solver_results = False
    373373      self._output_times = False
     374      self._output_scenario_tree_solution = False
    374375
    375376      # PH run-time variables
     
    498499            self._breakpoint_strategy = kwds[key]
    499500         elif key == "checkpoint_interval":
    500             self._checkpoint_interval = kwds[key]           
     501            self._checkpoint_interval = kwds[key]
     502         elif key == "output_scenario_tree_solution":
     503            self._output_scenario_tree_solution = kwds[key]                       
    501504         else:
    502505            print "Unknown option=" + key + " specified in call to PH constructor"
     
    674677         print "Creating variable statistic (min/avg/max) parameter vectors for scenario tree nodes"
    675678
    676       for stage in self._scenario_tree._stages[:-1]:
     679      # do this for all stages, simply for completeness, i.e., to create a fully populated scenario tree.
     680      for stage in self._scenario_tree._stages:
    677681
    678682         # first, gather all unique variables referenced in this stage
     
    882886      start_time = time.time()
    883887     
    884       for stage in self._scenario_tree._stages[:-1]: # no blending over the final stage
     888      # compute statistics over all stages, even the last. this is necessary in order to
     889      # successfully snapshot a scenario tree solution from the average values.
     890      for stage in self._scenario_tree._stages:
    885891         
    886892         for tree_node in stage._tree_nodes:
     
    925931                     for scenario in tree_node._scenarios:
    926932                        instance = self._instances[scenario._name]
    927                         avg_parameter = getattr(instance, avg_parameter_name)
    928                         avg_parameter[index] = avg / node_probability
     933                        try:
     934                           avg_parameter = getattr(instance, avg_parameter_name)
     935                           avg_parameter[index] = avg / node_probability
     936                        except:
     937                           pass
    929938
    930939      end_time = time.time()
     
    12771286
    12781287      # update the fixed variable statistics - the plugins might have done something.
    1279       (self._total_fixed_discrete_vars,self._total_fixed_continuous_vars) = self.compute_fixed_variable_counts()                       
     1288      (self._total_fixed_discrete_vars,self._total_fixed_continuous_vars) = self.compute_fixed_variable_counts()
     1289
     1290      self._solve_end_time = time.time()     
    12801291
    12811292      print "PH complete"
     
    12931304      self._scenario_tree.pprintCosts(self._instances)
    12941305
    1295       self._solve_end_time = time.time()
     1306      if self._output_scenario_tree_solution is True:
     1307         self._scenario_tree.snapshotSolutionFromAverages()
     1308         print "Final solution (scenario tree format):"
     1309         self._scenario_tree.pprintSolution()
    12961310
    12971311      if (self._verbose is True) and (self._output_times is True):
  • coopr.pysp/trunk/coopr/pysp/phinit.py

    r2418 r2446  
    5353                     action="store_true",
    5454                     dest="report_solutions",
     55                     default=False)
     56   parser.add_option("--output-scenario-tree-solution",
     57                     help="Report the full solution (even leaves) in scenario tree format upon termination. Values represent averages, so convergence is not an issue. Default is False.",
     58                     action="store_true",
     59                     dest="output_scenario_tree_solution",
    5560                     default=False)
    5661   parser.add_option("--report-weights",
     
    473478                           solver=options.solver_type, \
    474479                           solver_manager=options.solver_manager_type, \
     480                           output_scenario_tree_solution=options.output_scenario_tree_solution, \
    475481                           scenario_solver_options=options.scenario_solver_options, \
    476482                           scenario_mipgap=options.scenario_mipgap, \
  • coopr.pysp/trunk/coopr/pysp/scenariotree.py

    r2445 r2446  
     1
    12#  _________________________________________________________________________
    23#
     
    3940      # NOTE: the averages are probability_weighted - the min/max
    4041      #       values are not.
     42      # NOTE: the parameter names are basically irrelevant, and the
     43      #       convention is assumed to be enforced by whoever populates
     44      #       these parameters.
    4145      self._averages = {}
    4246      self._minimums = {}
     
    4751      # objects in the map are actual pyomo Var instances; keys are
    4852      # variable names.
    49       self._solution = {}
     53      self._solutions = {}
    5054
    5155      # for each variable referenced in the stage, clone the variable
     
    7983            new_variable[index].activate()
    8084
    81          self._solution[new_variable_name] = new_variable
     85         self._solutions[new_variable_name] = new_variable
     86
     87   #
     88   # copies the paraemter values values from the _averages attribute
     89   # into the _solutions attribute - only for active variable values.
     90   #
     91
     92   def snapshotSolutionFromAverages(self):
     93
     94      for variable_name, variable in self._solutions.items():
     95
     96         # try and grab the corresponding averages parameter - if it
     97         # doesn't exist, throw an exception.
     98         average_parameter = None
     99         try:
     100            average_parameter = self._averages[variable_name]
     101         except:
     102            raise RuntimeError, "No averages parameter present on tree node="+self._name+" for variable="+variable_name
     103           
     104         for index in variable._index:
     105            if variable[index].active is True:
     106               variable[index] = average_parameter[index]()
    82107
    83108   #
     
    565590     
    566591      return True
     592
     593   #
     594   # copies the parameter values stored in any tree node _averages attribute
     595   # into any tree node _solutions attribute - only for active variable values.
     596   #
     597
     598   def snapshotSolutionFromAverages(self):
     599
     600      for tree_node in self._tree_nodes:
     601
     602         tree_node.snapshotSolutionFromAverages()
    567603
    568604   #
     
    647683
    648684   #
     685   # a utility function to pretty-print the solution associated with a scenario tree
     686   #
     687
     688   def pprintSolution(self):
     689
     690      print "----------------------------------------------------"         
     691      print "Tree Nodes:"
     692      print ""
     693      for tree_node_name in sorted(self._tree_node_map.keys()):
     694         tree_node = self._tree_node_map[tree_node_name]
     695         print "\tName=" + tree_node_name
     696         if tree_node._stage is not None:
     697            print "\tStage=" + tree_node._stage._name
     698         else:
     699            print "\t Stage=None"
     700         if tree_node._parent is not None:
     701            print "\tParent=" + tree_node._parent._name
     702         else:
     703            print "\tParent=" + "None"
     704         print "\tVariables: "
     705         for (variable, index_template, indices) in tree_node._stage._variables:
     706            solution_variable = tree_node._solutions[variable.name]
     707            if (len(indices) == 1) and (indices[0] == None):
     708               # if this is a singleton variable, then it should necessarily be active -
     709               # otherwise, it wouldn't be referenced in the stage!!!
     710               print "\t\t"+variable.name+"="+str(solution_variable[index]())
     711            else:
     712               for index in indices:
     713                  if solution_variable[index].active is True:
     714                     print "\t\t"+variable.name+indexToString(index)+"="+str(solution_variable[index]())
     715         print ""           
     716
     717   #
    649718   # a utility function to pretty-print the cost information associated with a scenario tree
    650719   #
Note: See TracChangeset for help on using the changeset viewer.