Changeset 2450


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

Added code to the PySP scenario tree class to facilitate computation (snapshotting) of tree node variables directly from the composite instances.

File:
1 edited

Legend:

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

    r2446 r2450  
    1 
    21#  _________________________________________________________________________
    32#
     
    5352      self._solutions = {}
    5453
     54      # NOTE: Given the expense, this should really be optional - don't
     55      #       construct unless we're actually going to use!
    5556      # for each variable referenced in the stage, clone the variable
    5657      # for purposes of storing solutions. we are being wasteful in
     
    8687
    8788   #
    88    # copies the paraemter values values from the _averages attribute
     89   # copies the parameter values values from the _averages attribute
    8990   # into the _solutions attribute - only for active variable values.
    9091   #
     
    106107               variable[index] = average_parameter[index]()
    107108
     109   #
     110   # computes the solution values from the composite scenario instances at this tree node.
     111   # the input scenario_instance_map is a map from scenario name to instance objects.
     112   #
     113
     114   def snapshotSolutionFromInstances(self, scenario_instance_map):
     115
     116      for variable_name, variable in self._solutions.items():
     117         for index in variable._index:
     118            if variable[index].active is True:
     119               node_probability = 0.0
     120               avg = 0.0
     121               for scenario in self._scenarios:
     122                  scenario_instance = scenario_instance_map[scenario._name]
     123                  node_probability += scenario._probability
     124                  var_value = getattr(scenario_instance, variable.name)[index].value
     125                  avg += (scenario._probability * var_value)
     126               variable[index].value = avg / node_probability
     127           
    108128   #
    109129   # a utility to compute the cost of the current node plus the expected costs of child nodes.
     
    601621
    602622         tree_node.snapshotSolutionFromAverages()
     623
     624   #
     625   # computes the variable values at each tree node from the input scenario instances.
     626   #
     627
     628   def snapshotSolutionFromInstances(self, scenario_instance_map):
     629
     630      for tree_node in self._tree_nodes:
     631
     632         tree_node.snapshotSolutionFromInstances(scenario_instance_map)         
    603633
    604634   #
Note: See TracChangeset for help on using the changeset viewer.