Ignore:
Timestamp:
Nov 5, 2010 1:29:42 PM (11 years ago)
Author:
jwatson
Message:

Various updates to support heteogeneous index sets in PH for different nodes in the scenario tree - more work / testing remains.

File:
1 edited

Legend:

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

    r3106 r3217  
    2626   # variable/match-template/variable-index triple.
    2727   #
    28    def _update_solution_map(self, variable, match_template, variable_indices):
     28   def _update_solution_map(self, variable, match_template):
     29
     30      variable_indices = self._variable_indices[variable.name]
    2931
    3032      # don't bother copying bounds for variables, as the values stored
     
    6971      # this is something that we might revisit if space/performance
    7072      # is an issue (space is the most likely issue)
    71       for variable, match_template, variable_indices in self._stage._variables:
    72          self._update_solution_map(variable, match_template, variable_indices)
     73      for variable, match_template in self._stage._variables:
     74         self._update_solution_map(variable, match_template)
    7375
    7476      self._solution_map_initialized = True
     
    8587      self._conditional_probability = conditional_probability # conditional on parent
    8688      self._scenarios = [] # a collection of all Scenarios passing through this node in the tree
     89      self._variable_indices = {} # a map from a variable name to the indices blended at this node.
    8790
    8891      # general use statistics for the variables at each node.
     
    112115      if initialize_solution is True:
    113116         self._initialize_solution_map()
     117
     118   #
     119   # given a set of scenario instances, compute the set of indices being blended
     120   # for each variable at this node.
     121   #
     122
     123   def defineVariableIndexSets(self, scenario_instances):
     124
     125      # find a representative scenario instance belonging to this node. the
     126      # first scenario is as good as any.
     127      scenario_instance = scenario_instances[self._scenarios[0]._name]
     128
     129      for reference_variable, match_template in self._stage._variables:
     130
     131         # the stage variable simply references the variable object in the
     132         # reference scenario instance - we need to grab the variable in the
     133         # scenario instance, as the index set might be different.
     134         variable_name = reference_variable.name
     135
     136         instance_variable = getattr(scenario_instance, variable_name)
     137
     138         match_indices = extractVariableIndices(instance_variable, match_template)
     139
     140         self._variable_indices[variable_name] = match_indices
    114141
    115142   #
     
    195222      self._tree_nodes = []
    196223
    197       # a collection of triples consisting of (1) a reference to a Pyomo model Var object, (2) the original match
    198       # template string (for output purposes), and (3) a *list* of the corresponding indices. the variables are
    199       # references to those objects belonging to the Pyomo reference scenario instance associated with the parent
    200       # ScenarioTree of this Stage.
    201       # NOTE: if the variable index is none, it is assumed that the entire variable is blended.
     224      # a collection of pairs consisting of (1) a reference to a Pyomo model Var object (in the reference scenario instance) and
     225      # (2) the original match template string (for output purposes). the specific indices that match belong to the tree node.
    202226      self._variables = []
    203227
     
    209233   # add a new variable to the stage, which will include updating the solution maps for each associated ScenarioTreeNode.
    210234   #
    211    def add_variable(self, variable, match_template, indices):
    212 
    213       self._variables.append((variable, match_template, indices))
    214 
    215       for tree_node in self._tree_nodes:
    216          tree_node._update_solution_map(variable, match_template, indices)
     235   def add_variable(self, variable, match_template):
     236
     237      self._variables.append((variable, match_template))
     238
     239      for tree_node in self._tree_nodes:
     240         tree_node._update_solution_map(variable, match_template)
    217241
    218242class Scenario(object):
     
    263287               variable = self._reference_instance.active_components(Var)[variable_name]
    264288
    265                # extract all "real", i.e., fully specified, indices matching the index template.
    266                match_indices = extractVariableIndices(variable, index_template)
    267 
    268                # there is a possibility that no indices match the input template.
    269                # if so, let the user know about it.
    270                if len(match_indices) == 0:
    271                   raise RuntimeError, "No indices match template="+str(index_template)+" for variable="+variable_name+" ; encountered in scenario tree specification for model="+self._reference_instance.name
    272 
    273                stage._variables.append((variable, index_template, match_indices))
     289               stage._variables.append((variable, index_template))
    274290
    275291            else:
     
    293309               match_indices.append(None)
    294310
    295                stage._variables.append((variable, "", match_indices))
     311               stage._variables.append((variable, ""))
    296312
    297313      for stage_id in stage_cost_variable_ids.keys():
     
    506522
    507523   #
     524   # given a set of scenario instances, compute the set of variable indices being blended at each node.
     525   # this can't be done until the scenario instances are available, as different scenarios can have
     526   # different index sets.
     527   #
     528
     529   def defineVariableIndexSets(self, scenario_instances):
     530
     531      for tree_node in self._tree_nodes:
     532
     533         tree_node.defineVariableIndexSets(scenario_instances)
     534
     535   #
    508536   # is the indicated scenario in the tree?
    509537   #
     
    717745
    718746      for stage in self._stages:
    719          # stage_var is a VarValue - the rest are strings and list of indices, respectively.
    720          for (stage_var, match_template, match_indices) in stage._variables:
     747         for (stage_var, match_template) in stage._variables:
    721748            if (variable.name == stage_var.name) and (index in match_indices):
    722749               return stage
     
    819846            print "\t\t" + tree_node._name
    820847         print "\tVariables: "
    821          for (variable, index_template, indices) in stage._variables:
    822             if (len(indices) == 1) and (indices[0] == None):
    823                print "\t\t" + variable.name
    824             else:
    825                print "\t\t",variable.name,":",index_template
     848         for (variable, index_template) in stage._variables:
     849            print "\t\t",variable.name,":",index_template
    826850         print "\tCost Variable: "
    827851         if stage._cost_variable[1] is None:
     
    867891            print "\tParent=" + "None"
    868892         print "\tVariables: "
    869          for (variable, index_template, indices) in tree_node._stage._variables:
     893         for (variable, index_template) in tree_node._stage._variables:
     894            indices = tree_node._variable_indices[variable.name]
    870895            solution_variable = tree_node._solutions[variable.name]
    871896            if (len(indices) == 1) and (indices[0] == None):
Note: See TracChangeset for help on using the changeset viewer.