Changeset 3074


Ignore:
Timestamp:
Oct 4, 2010 9:45:14 PM (11 years ago)
Author:
jwatson
Message:

Added routines to the PySP scenario tree object to return the stage for a constraint and variable.

File:
1 edited

Legend:

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

    r3073 r3074  
    704704
    705705         tree_node.snapshotSolutionFromInstances(scenario_instance_map)
     706
     707   #
     708   # a utility to determine the stage to which the input variable belongs.
     709   # this is horribly inefficient, lacking an inverse map. fortunately,
     710   # it isn't really called that often (yet). stage membership is determined
     711   # by comparing the input variable name with the reference instance
     712   # variable name (which is what the scenario tree refers to) and the
     713   # associated indices.
     714   #
     715
     716   def variableStage(self, variable, index):
     717     
     718      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:
     721            if (variable.name == stage_var.name) and (index in match_indices):
     722               return stage
     723
     724         # IMPT: this is a temporary hack - the real fix is to force users to
     725         # have every variable assigned to some stage in the model, either
     726         # automatically or explicitly.
     727         if (variable.name == stage._cost_variable[0].name):
     728            return stage
     729
     730      raise RuntimeError, "The variable="+str(variable.name)+", index="+str(index)+" does not belong to any stage in the scenario tree"
     731
     732   #
     733   # a utility to determine the stage to which the input constraint "belongs".
     734   # a constraint belongs to the latest stage in which referenced variables
     735   # in the constraint appears in that stage.
     736   # input is a constraint is of type "Constraint", and an index of that
     737   # constraint - which might be None in the case of singleton constraints.
     738   # currently doesn't deal with SOS constraints, for no real good reason.
     739   # returns an instance of a Stage object.
     740   # IMPT: this method works on the canonical representation ("repn" attribute)
     741   #       of a constraint. this implies that pre-processing of the instance
     742   #       has been performed.
     743   # NOTE: there is still the issue of whether the contained variables really
     744   #       belong to the same model, but that is a different issue we won't
     745   #       address right now (e.g., what does it mean for a constraint in an
     746   #       extensive form binding instance to belong to a stage?).
     747   #
     748
     749   def constraintStage(self, constraint, index):
     750
     751      largest_stage_index = -1
     752      largest_stage = None
     753     
     754      canonical_repn = constraint[index].repn
     755      for degree, terms in canonical_repn.items():
     756         if degree != -1:
     757            for var_key, coefficient in terms.items():
     758               var_value = canonical_repn[-1][var_key.keys()[0]]
     759               var_stage = self.variableStage(var_value.var, var_value.index)
     760               var_stage_index = self._stages.index(var_stage)
     761
     762               if var_stage_index > largest_stage_index:
     763                  largest_stage_index = var_stage_index
     764                  largest_stage = var_stage
     765
     766      return largest_stage
    706767
    707768   #
     
    896957         print ""
    897958      print "----------------------------------------------------"
     959
     960      print "****TESTING****"
     961      constraints = self._reference_instance.active_components(Constraint)
     962      for name, constraint in constraints.items():
     963         print "NAME=",name
     964         for index in constraint:
     965            print "INDEX=",index
     966            stage = self.constraintStage(constraint,index)
     967            print "STAGE=",stage._name
Note: See TracChangeset for help on using the changeset viewer.