Ignore:
Timestamp:
Mar 22, 2010 9:18:10 PM (10 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).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.