Changeset 5781


Ignore:
Timestamp:
May 16, 2012 5:04:04 PM (7 years ago)
Author:
gabeh
Message:

Piecewise: Fixing memory leak caused by keeping dictionaries on the Piecewise class that reference the model components it creates. The easiest fix I can think of is to use weakrefs as the dictionary values.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • coopr.pyomo/trunk/coopr/pyomo/base/piecewise.py

    r5759 r5781  
    11
    22__all__ = ['Piecewise']
    3 
     3import weakref
    44from component import Component
    55from constraint import Constraint
     
    471471        Deactivates all model constraints created by this class.
    472472        """
    473         [con.deactivate() for con in self._constraints_dict.itervalues()]
     473        [con().deactivate() for con in self._constraints_dict.itervalues()]
    474474        self.active = False
    475475   
     
    478478        Activates all model constraints created by this class.
    479479        """
    480         [con.activate() for con in self._constraints_dict.itervalues()]
     480        [con().activate() for con in self._constraints_dict.itervalues()]
    481481        self.active = True
    482482   
     
    717717                raise AssertionError, "Error: "+self.name+" trying to overwrite model attribute: "+constraint_name
    718718            setattr(self._model(),constraint_name,_object_)
    719             self._constraints_dict[constraint_name] = getattr(self._model(), constraint_name)
    720             return None
    721         elif isinstance(_object_,_VarArray):
     719            self._constraints_dict[constraint_name] = weakref.ref(getattr(self._model(), constraint_name))
     720            return constraint_name
     721        elif isinstance(_object_,Var):
    722722            var_name = self.name+'_'+name
    723723            if hasattr(self._model(),var_name):
    724724                raise AssertionError, "Error: "+self.name+" trying to overwrite model attribute: "+var_name
    725725            setattr(self._model(),var_name,_object_)
    726             self._vars_dict.update([(v.name,v) for (n,v) in getattr(self._model(), var_name).iteritems()])
    727             return var_name
    728         elif isinstance(_object_,_VarElement):
    729             var_name = self.name+'_'+name
    730             if hasattr(self._model(),var_name):
    731                 raise AssertionError, "Error: "+self.name+" trying to overwrite model attribute: "+var_name
    732             setattr(self._model(),var_name,_object_)
    733             self._vars_dict[var_name] = getattr(self._model(), var_name)
     726            self._vars_dict[var_name] = weakref.ref(getattr(self._model(), var_name))
    734727            return var_name
    735728        elif isinstance(_object_,Set):
     
    738731                raise AssertionError, "Error: "+self.name+" trying to overwrite model attribute: "+set_name
    739732            setattr(self._model(),set_name,_object_)
    740             self._sets_dict[set_name] = getattr(self._model(), set_name)
     733            self._sets_dict[set_name] = weakref.ref(getattr(self._model(), set_name))
    741734            return set_name
    742735   
     
    747740        Key, Value pairs are (var.name, var)
    748741        """
    749         return self._vars_dict
     742        return dict((name,var_ref()) for (name,var_ref) in self._vars_dict.iteritems())
    750743   
    751744    def constraints(self):
     
    754747        Key, Value pairs are (constraint.name, constraint)
    755748        """
    756         return self._constraints_dict
     749        return dict((name,con_ref()) for (name,con_ref) in self._constraints_dict.iteritems())
    757750   
    758751    def sets(self):
     
    761754        Key, Value pairs are (set.name, set)
    762755        """
    763         return self._sets_dict
     756        return dict((name,set_ref()) for (name,set_ref) in self._sets_dict.iteritems())
    764757   
    765758    def components(self):
     
    768761        Key, Value pairs are (component.name, component)
    769762        """
    770         tmp_dict = {}
    771         tmp_dict.update(self._vars_dict)
    772         tmp_dict.update(self._sets_dict)
    773         tmp_dict.update(self._constraints_dict)
     763        tmp_dict = dict((name,var_ref()) for (name,var_ref) in self._vars_dict.iteritems())
     764        tmp_dict.update((name,con_ref()) for (name,con_ref) in self._constraints_dict.iteritems())
     765        tmp_dict.update((name,set_ref()) for (name,set_ref) in self._sets_dict.iteritems())
    774766        return tmp_dict
    775767
     
    16231615        return M_final
    16241616    ########
     1617   
     1618    def __del__(self):
     1619        self._constraints_dict = None
     1620        self._vars_dict = None
     1621        self._sets_dict = None
Note: See TracChangeset for help on using the changeset viewer.