Changeset 2288


Ignore:
Timestamp:
Feb 8, 2010 3:32:41 PM (10 years ago)
Author:
jwatson
Message:

Significant initialization speed reductions in the WW PH extension for PySP.

File:
1 edited

Legend:

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

    r2201 r2288  
    2929   return cmp(value_b, value_a)
    3030
    31 #==================================================
    3231#==================================================
    3332class wwphextension(SingletonPlugin):
     
    266265               tree_node._fixed_var_flag = {}
    267266
    268                # next, create parameters for each variable in the corresponding tree node.
    269 
    270267               for (variable, index_template, variable_indices) in stage._variables:
    271268
    272269                  variable_name = variable.name
    273270                  variable_type = variable.domain
     271
     272                  # create the parameters for all indices, even though this might be a bit
     273                  # wasteful. in practice, this probably won't be the case, and determining
     274                  # the reduced index set would probably take longer that it is worth.
     275
     276                  new_stat_index = variable._index
     277                  new_stat_parameter_name = "NODESTAT_NUM_ITERS_CONVERGED_"+variable.name
     278                  new_stat_parameter = None
     279                  # this bit of ugliness is due to Pyomo not correctly handling the Param construction
     280                  # case when the supplied index set consists strictly of None, i.e., the source variable
     281                  # is a singleton. this case be cleaned up when the source issue in Pyomo is fixed.                     
     282                  if (len(new_stat_index) is 1) and (None in new_stat_index):
     283                     new_stat_parameter = Param(name=new_stat_parameter_name)
     284                  else:
     285                     new_stat_parameter = Param(new_stat_index,name=new_stat_parameter_name)
     286                  for newindex in new_stat_index:
     287                     new_stat_parameter[newindex] = 0
     288                  tree_node._num_iters_converged[variable.name] = new_stat_parameter
     289                     
     290                  # need to know to what we have most recently converged
     291                  new_conv_index = variable._index
     292                  new_conv_parameter_name = "NODESTAT_LAST_CONVERGED_VAL_"+variable.name
     293                  new_conv_parameter = None
     294                  if (len(new_conv_index) is 1) and (None in new_conv_index):
     295                     new_conv_parameter = Param(name=new_conv_parameter_name)
     296                  else:
     297                     new_conv_parameter = Param(new_conv_index,name=new_conv_parameter_name)
     298                  for newindex in new_conv_index:
     299                     new_conv_parameter[newindex] = 0.5 # not an int, so harmless
     300                  tree_node._last_converged_val[variable.name] = new_conv_parameter
     301                     
     302                  # need to know to what has been fixed
     303                  new_fix_index = variable._index
     304                  new_fix_parameter_name = "NODESTAT_FIXED_FLAG_VAL_"+variable.name
     305                  new_fix_parameter = None
     306                  if (len(new_fix_index) is 1) and (None in new_fix_index):
     307                     new_fix_parameter = Param(name=new_fix_parameter_name)
     308                  else:
     309                     new_fix_parameter = Param(new_fix_index,name=new_fix_parameter_name)
     310                  for newindex in new_fix_index:
     311                     new_fix_parameter[newindex] = False
     312                  tree_node._fixed_var_flag[variable.name] = new_fix_parameter
     313
     314                  # now make the w hash value storage array
     315                  new_hash_index = variable._index
     316                  new_hash_parameter_name = "W_HASH_STORAGE_"+variable.name
     317                  new_hash_parameter = None
     318                  if (len(new_hash_index) is 1) and (None in new_hash_index):
     319                     new_hash_parameter = Param(ph._iteration_index_set, name=new_hash_parameter_name)
     320                  else:
     321                     new_hash_parameter = Param(new_hash_index, ph._iteration_index_set, name=new_hash_parameter_name)
     322                  for newindex in new_hash_index:
     323                     for i in range(0, ph._max_iterations+1):
     324                        # the following if-then block is a complete hack, due to the
     325                        # fact that we can't index by None if the Param is unary.
     326                        if new_hash_parameter.dim() == 1:
     327                           new_hash_parameter[i] = 0
     328                        else:
     329                           new_hash_parameter[newindex,i] = 0
     330                  tree_node._w_hash[variable.name] = new_hash_parameter
    274331                 
     332                  # JPW has no idea why the following code block is here, or if it is necessary.
    275333                  for index in variable_indices:
    276334
     
    278336                     #       variable values in the last stage of a stochastic program will *not*
    279337                     #       have a defined _stage attribute.
    280 #                     print "dlw debug create stage association ",variable_name, index, stage._name                     
    281338                     variable[index]._stage = stage
    282339
    283                      new_stat_index = variable._index
    284                      new_stat_parameter_name = "NODESTAT_NUM_ITERS_CONVERGED_"+variable.name
    285                      new_stat_parameter = None
    286                      # this bit of ugliness is due to Pyomo not correctly handling the Param construction
    287                      # case when the supplied index set consists strictly of None, i.e., the source variable
    288                      # is a singleton. this case be cleaned up when the source issue in Pyomo is fixed.                     
    289                      if (len(new_stat_index) is 1) and (None in new_stat_index):
    290                         new_stat_parameter = Param(name=new_stat_parameter_name)
    291                      else:
    292                         new_stat_parameter = Param(new_stat_index,name=new_stat_parameter_name)
    293                      for newindex in new_stat_index:
    294                         new_stat_parameter[newindex] = 0
    295                      tree_node._num_iters_converged[variable.name] = new_stat_parameter
    296                      
    297                      # need to know to what we have most recently converged
    298                      new_conv_index = variable._index
    299                      new_conv_parameter_name = "NODESTAT_LAST_CONVERGED_VAL_"+variable.name
    300                      new_conv_parameter = None
    301                      if (len(new_conv_index) is 1) and (None in new_conv_index):
    302                         new_conv_parameter = Param(name=new_conv_parameter_name)
    303                      else:
    304                         new_conv_parameter = Param(new_conv_index,name=new_conv_parameter_name)
    305                      for newindex in new_conv_index:
    306                         new_conv_parameter[newindex] = 0.5 # not an int, so harmless
    307                      tree_node._last_converged_val[variable.name] = new_conv_parameter
    308                      
    309                      # need to know to what has been fixed
    310                      new_fix_index = variable._index
    311                      new_fix_parameter_name = "NODESTAT_FIXED_FLAG_VAL_"+variable.name
    312                      new_fix_parameter = None
    313                      if (len(new_fix_index) is 1) and (None in new_fix_index):
    314                         new_fix_parameter = Param(name=new_fix_parameter_name)
    315                      else:
    316                         new_fix_parameter = Param(new_fix_index,name=new_fix_parameter_name)
    317                      for newindex in new_fix_index:
    318                         new_fix_parameter[newindex] = False
    319                      tree_node._fixed_var_flag[variable.name] = new_fix_parameter
    320                      
    321                      # now make the w hash value storage array
    322                      new_hash_index = variable._index
    323                      new_hash_parameter_name = "W_HASH_STORAGE_"+variable.name
    324                      new_hash_parameter = None
    325                      if (len(new_hash_index) is 1) and (None in new_hash_index):
    326                         new_hash_parameter = Param(ph._iteration_index_set, name=new_hash_parameter_name)
    327                      else:
    328                         new_hash_parameter = Param(new_hash_index, ph._iteration_index_set, name=new_hash_parameter_name)
    329                      for newindex in new_hash_index:
    330                         for i in range(0, ph._max_iterations+1):
    331                            # the following if-then block is a complete hack, due to the
    332                            # fact that we can't index by None if the Param is unary.
    333                            if new_hash_parameter.dim() == 1:
    334                               new_hash_parameter[i] = 0
    335                            else:
    336                               new_hash_parameter[newindex,i] = 0
    337                      tree_node._w_hash[variable.name] = new_hash_parameter
    338340
    339341      # store the total variable counts for future reporting.
Note: See TracChangeset for help on using the changeset viewer.