Changeset 5367


Ignore:
Timestamp:
11/16/11 18:39:08 (2 years ago)
Author:
jwatson
Message:

Fixing bug in update_weights_for_scenario (runph). It works fine for two-stage problems, but the weight update computation was bogus for three-stage or greater problems.

File:
1 edited

Legend:

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

    r5366 r5367  
    15181518    def update_weights_for_scenario(self, instance): 
    15191519 
    1520         # because the weight updates rely on the xbars, and the xbars are node-based, 
    1521         # I'm looping over the tree nodes and pushing weights into the corresponding scenarios. 
    15221520        start_time = time.time() 
    15231521 
    1524         for stage in self._scenario_tree._stages: 
    1525  
    1526             if stage != self._scenario_tree._stages[-1]: # no blending over the final stage, so no weights to worry about. 
    1527  
    1528                 # TBD - LOOP OVER TREE NODES FOR INPUT SCENARIO ONLY 
    1529                 for tree_node in stage._tree_nodes: 
    1530  
    1531                     for variable_name, (variable, index_template) in stage._variables.iteritems(): 
    1532  
    1533                         blend_parameter_name = "PHBLEND_"+variable_name 
    1534                         weight_parameter_name = "PHWEIGHT_"+variable_name 
    1535                         rho_parameter_name = "PHRHO_"+variable_name 
    1536  
    1537                         variable_indices = tree_node._variable_indices[variable_name] 
    1538  
    1539                         for index in variable_indices: 
    1540  
    1541                             tree_node_average = value(tree_node._averages[variable.name][index]) 
    1542  
    1543                             if getattr(instance,variable.name)[index].status != VarStatus.unused: 
    1544  
    1545                                 weight_parameter = getattr(instance, weight_parameter_name) 
    1546  
    1547                                 # we are currently not updating weights if blending is disabled for a variable. 
    1548                                 # this is done on the premise that unless you are actively trying to move 
    1549                                 # the variable toward the mean, the weights will blow up and be huge by the 
    1550                                 # time that blending is activated. 
    1551                                 variable_blend_indicator = getattr(instance, blend_parameter_name)[index].value 
    1552  
    1553                                 # get the weight and rho parameters for this variable/index combination. 
    1554                                 rho_value = getattr(instance, rho_parameter_name)[index].value 
    1555                                 current_variable_weight = weight_parameter[index].value 
    1556  
    1557                                 # if I'm maximizing, invert value prior to adding (hack to implement negatives). 
    1558                                 # probably fixed in Pyomo at this point - I just haven't checked in a long while. 
    1559                                 if self._is_minimizing is False: 
    1560                                     current_variable_weight = (-current_variable_weight) 
    1561                                 current_variable_value = getattr(instance, variable_name)[index]() 
    1562                                 new_variable_weight = current_variable_weight + variable_blend_indicator * rho_value * (current_variable_value - tree_node_average) 
    1563                                 # I have the correct updated value, so now invert if maximizing. 
    1564                                 if self._is_minimizing is False: 
    1565                                     new_variable_weight = (-new_variable_weight) 
    1566                                 weight_parameter[index].value = new_variable_weight 
     1522        scenario = self._scenario_tree.get_scenario(instance.name) 
     1523 
     1524        for tree_node in scenario._node_list[:-1]: 
     1525 
     1526            for variable_name, variable_indices in tree_node._variable_indices.iteritems(): 
     1527 
     1528                blend_parameter_name = "PHBLEND_"+variable_name 
     1529                weight_parameter_name = "PHWEIGHT_"+variable_name 
     1530                rho_parameter_name = "PHRHO_"+variable_name 
     1531 
     1532                blend_parameter = getattr(instance, blend_parameter_name) 
     1533                weight_parameter = getattr(instance, weight_parameter_name) 
     1534                rho_parameter = getattr(instance, rho_parameter_name) 
     1535   
     1536                tree_node_averages = tree_node._averages[variable_name] 
     1537 
     1538                instance_variable = getattr(instance, variable_name) 
     1539 
     1540                for index in variable_indices: 
     1541 
     1542                    tree_node_average = tree_node_averages[index] # these are float values 
     1543 
     1544                    if instance_variable[index].status != VarStatus.unused: 
     1545 
     1546                        # we are currently not updating weights if blending is disabled for a variable. 
     1547                        # this is done on the premise that unless you are actively trying to move 
     1548                        # the variable toward the mean, the weights will blow up and be huge by the 
     1549                        # time that blending is activated. 
     1550                        variable_blend_indicator = blend_parameter[index].value 
     1551 
     1552                        # get the weight and rho parameters for this variable/index combination. 
     1553                        rho_value = rho_parameter[index].value 
     1554                        current_variable_weight = weight_parameter[index].value 
     1555 
     1556                        # if I'm maximizing, invert value prior to adding (hack to implement negatives). 
     1557                        # probably fixed in Pyomo at this point - I just haven't checked in a long while. 
     1558                        if self._is_minimizing is False: 
     1559                            current_variable_weight = (-current_variable_weight) 
     1560                        current_variable_value = instance_variable[index]() 
     1561                        new_variable_weight = current_variable_weight + variable_blend_indicator * rho_value * (current_variable_value - tree_node_average) 
     1562                        # I have the correct updated value, so now invert if maximizing. 
     1563                        if self._is_minimizing is False: 
     1564                            new_variable_weight = (-new_variable_weight) 
     1565                        weight_parameter[index].value = new_variable_weight 
    15671566 
    15681567        end_time = time.time() 
Note: See TracChangeset for help on using the changeset viewer.