Changeset 2403


Ignore:
Timestamp:
Feb 25, 2010 11:42:02 AM (10 years ago)
Author:
jwatson
Message:

Added a utility method to compute the aggregate cost for a scenario, by looping over the stage variables and querying instance variable values.

File:
1 edited

Legend:

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

    r2375 r2403  
    355355   def get_scenario(self, name):
    356356      return self._scenario_map[name]
     357
     358   #
     359   # compute the scenario cost for the input instance, i.e.,
     360   # the sum of all stage cost variables.
     361   #
     362   def compute_scenario_cost(self, instance):
     363      aggregate_cost = 0.0
     364      for stage in self._stages:
     365         instance_cost_variable = instance.active_components(Var)[stage._cost_variable[0].name][stage._cost_variable[1]]()
     366         print "\tStage=%20s     Cost=%10.4f" % (stage._name, instance_cost_variable)
     367         aggregate_cost += instance_cost_variable
     368      return aggregate_cost
    357369
    358370   #
     
    649661            print "\t\t" + tree_node._name
    650662
    651          aggregate_cost = 0.0
    652          for stage in self._stages:
    653             instance_cost_variable = instance.active_components(Var)[stage._cost_variable[0].name][stage._cost_variable[1]]()
    654             print "\tStage=%20s     Cost=%10.4f" % (stage._name, instance_cost_variable)
    655             aggregate_cost += instance_cost_variable
     663         aggregate_cost = self.compute_scenario_cost(instance)
    656664         print "\tTotal scenario cost=%10.4f" % aggregate_cost
    657665         print ""
Note: See TracChangeset for help on using the changeset viewer.