Changeset 2256


Ignore:
Timestamp:
Feb 4, 2010 12:18:11 AM (10 years ago)
Author:
jwatson
Message:

Major speed improvements in the EF writer by avoiding Python deep-copes - saves a few orders of magnitude of run-time.

File:
1 edited

Legend:

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

    r2254 r2256  
    222222      for (stage_variable, index_template, stage_variable_indices) in stage._variables:
    223223
    224          print "Creating master variable and blending constraints for decision variable=", stage_variable, ", indices=", stage_variable_indices
     224         print "Creating master variable and blending constraints for decision variable=", stage_variable, ", indices=",stage_variable_indices
    225225
    226226         for tree_node in stage._tree_nodes:
     
    237237                  master_variable = getattr(master_binding_instance, master_variable_name)
    238238               except:
    239                   # the deepcopy is probably too expensive (and unnecessary) computationally -
    240                   # easier to just use the constructor with the stage variable index/bounds/etc.
    241                   # NOTE: need to re-assign the master variables for each _varval - they probably
    242                   #       point to a bogus model.
    243                   new_master_variable = copy.deepcopy(stage_variable)
    244                   new_master_variable.name = master_variable_name
     239                  new_master_variable_index = stage_variable._index
     240                  new_master_variable = None
     241                  if (len(new_master_variable_index) is 1) and (None in new_master_variable_index):
     242                     new_master_variable = Var(name=stage_variable.name)
     243                  else:
     244                     new_master_variable = Var(new_master_variable_index, name=stage_variable.name)
     245                  new_master_variable.construct()
    245246                  new_master_variable._model = master_binding_instance
    246247                  setattr(master_binding_instance, master_variable_name, new_master_variable)
     248
     249                  # TBD - TECHNICALLY, WE NEED TO COPY BOUNDS - BUT WE REALLY DON'T, AS THEY ARE ON THE PER-INSTNACE VARS!
    247250
    248251                  master_variable = new_master_variable
     
    313316      for tree_node in stage._tree_nodes:
    314317
     318         # TBD - the following is bad - check to see if it's already there (I suspect some of them are!!!)         
     319
     320         # this is undoubtedly wasteful, in that a cost variable
     321         # for each tree node is created with *all* indices.         
    315322         new_cost_variable_name = tree_node._name + "_" + cost_variable.name
    316 
    317          # TBD - the following is bad - check to see if it's already there (I suspect some of them are!!!)
    318 
    319          # this is undoubtedly wasteful, in that a cost variable
    320          # for each tree node is created with *all* indices.
    321          new_cost_variable = copy.deepcopy(cost_variable)
    322          new_cost_variable.name = new_cost_variable_name
     323         new_cost_variable_index = cost_variable._index
     324         new_cost_variable = None
     325         if (len(new_cost_variable_index) is 1) and (None in new_cost_variable_index):
     326            new_cost_variable = Var(name=new_cost_variable_name)
     327         else:
     328            new_cost_variable = Var(new_cost_variable_index, new_cost_variable_name)
     329         new_cost_variable.construct()
    323330         new_cost_variable._model = master_binding_instance
    324          setattr(master_binding_instance, new_cost_variable_name, new_cost_variable)
     331         setattr(master_binding_instance, new_cost_variable_name, new_cost_variable)         
    325332
    326333         # the following is necessary, specifically to get the name - deepcopy won't reset these attributes.
     
    644651                  master_variable = getattr(master_binding_instance, master_variable_name)
    645652               except:
    646                   # the deepcopy is probably too expensive (and unnecessary) computationally -
    647                   # easier to just use the constructor with the stage variable index/bounds/etc.
    648                   # NOTE: need to re-assign the master variables for each _varval - they probably
    649                   #       point to a bogus model.
    650                   new_master_variable = copy.deepcopy(stage_variable)
    651                   new_master_variable.name = master_variable_name
     653                  new_master_variable_index = stage_variable._index
     654                  new_master_variable = None
     655                  if (len(new_master_variable_index) is 1) and (None in new_master_variable_index):
     656                     new_master_variable = Var(name=stage_variable.name)
     657                  else:
     658                     new_master_variable = Var(new_master_variable_index, name=stage_variable.name)
     659                  new_master_variable.construct()
    652660                  new_master_variable._model = master_binding_instance
    653661                  setattr(master_binding_instance, master_variable_name, new_master_variable)
     662
     663                  # TBD - TECHNICALLY, WE NEED TO COPY BOUNDS - BUT WE REALLY DON'T, AS THEY ARE ON THE PER-INSTNACE VARS!
    654664
    655665                  master_variable = new_master_variable
     
    711721         # this is undoubtedly wasteful, in that a cost variable
    712722         # for each tree node is created with *all* indices.
    713          new_cost_variable = copy.deepcopy(cost_variable)
    714          new_cost_variable.name = new_cost_variable_name
     723         new_cost_variable_name = tree_node._name + "_" + cost_variable.name
     724         new_cost_variable_index = cost_variable._index
     725         new_cost_variable = None
     726         if (len(new_cost_variable_index) is 1) and (None in new_cost_variable_index):
     727            new_cost_variable = Var(name=new_cost_variable_name)
     728         else:
     729            new_cost_variable = Var(new_cost_variable_index, new_cost_variable_name)
     730         new_cost_variable.construct()
    715731         new_cost_variable._model = master_binding_instance
    716          setattr(master_binding_instance, new_cost_variable_name, new_cost_variable)
     732         setattr(master_binding_instance, new_cost_variable_name, new_cost_variable)                 
    717733
    718734         # the following is necessary, specifically to get the name - deepcopy won't reset these attributes.
Note: See TracChangeset for help on using the changeset viewer.