예제 #1
0
 def __init__(self, agent, mta):
     self.messages = []
     self.agent = weakref_ref(agent)
     self.mta = weakref_ref(mta)
     self.events = agent.getEvents()
     for event in self.events:
         mta.registerMailingList(self, event)
예제 #2
0
파일: symbol_map.py 프로젝트: Pyomo/pyomo
 def __setstate__(self, state):
     self.byObject = dict(
         (id(obj), key) for key, obj  in state['bySymbol'] )
     self.bySymbol = dict(
         (key, weakref_ref(obj)) for key, obj in state['bySymbol'] )
     self.aliases = dict(
         (key, weakref_ref(obj)) for key, obj in state['aliases'] )
예제 #3
0
파일: refs.py 프로젝트: AlexUlrich/digsby
    def __init__(self, method, cb = None):
        from util.introspect import funcinfo
        assert hasattr(method, 'im_self'), 'no im_self: %s' % funcinfo(method)

        self.object = weakref_ref(method.im_self, cb)
        self.func   = weakref_ref(method.im_func)
        self.cls    = weakref_ref(method.im_class)
예제 #4
0
파일: arc.py 프로젝트: Pyomo/pyomo
    def set_value(self, vals):
        """Set the port attributes on this arc"""
        # the following allows m.a = Arc(directed=True); m.a = (m.p, m.q)
        # and m.a will be directed
        d = self._directed if self._directed is not None else \
            self.parent_component()._init_directed

        vals = _iterable_to_dict(vals, d, self.name)

        source = vals.pop("source", None)
        destination = vals.pop("destination", None)
        ports = vals.pop("ports", None)
        directed = vals.pop("directed", None)

        if len(vals):
            raise ValueError(
                "set_value passed unrecognized keywords in val:\n\t" +
                "\n\t".join("%s = %s" % (k, v) for k, v in iteritems(vals)))

        if directed is not None:
            if source is None and destination is None:
                if directed and ports is not None:
                    # implicitly directed ports tuple, transfer to src and dest
                    try:
                        source, destination = ports
                        ports = None
                    except:
                        raise ValueError(
                            "Failed to unpack 'ports' argument of arc '%s'. "
                            "Argument must be a 2-member tuple or list."
                            % self.name)
            elif not directed:
                # throw an error if they gave an inconsistent directed value
                raise ValueError(
                    "Passed False value for 'directed' for arc '%s', but "
                    "specified source or destination." % self.name)

        self._validate_ports(source, destination, ports)

        if self.ports is not None:
            # we are reassigning this arc's values, clean up port lists
            weakref_self = weakref_ref(self)
            for port in self.ports:
                port._arcs.remove(weakref_self)
            if self._directed:
                self.source._dests.remove(weakref_self)
                self.destination._sources.remove(weakref_self)

        self._ports = tuple(ports) if ports is not None \
            else (source, destination)
        self._directed = source is not None
        weakref_self = weakref_ref(self)
        for port in self._ports:
            port._arcs.append(weakref_self)
        if self._directed:
            source._dests.append(weakref_self)
            destination._sources.append(weakref_self)
예제 #5
0
파일: refs.py 프로젝트: AlexUlrich/digsby
    def __init__(self, obj, method, cb = None):
        self.object = weakref_ref(obj, cb)
        self.func   = weakref_ref(method)

        try:
            unbound_cbs = obj._unbound_cbs
        except AttributeError:
            obj._unbound_cbs = unbound_cbs = {}

        unbound_cbs[self] = method
예제 #6
0
 def __init__(self, project, name, mta=None, application=None):
     if not mta:
         mta = project.mta()
     Agent.__init__(self, name, mta)
     self.project = weakref_ref(project)
     if not application:
         application = project.application()
     self.application = weakref_ref(application)
     if project is not self:
         self.score_weight = 1.0
         self.register()
예제 #7
0
파일: PyomoModel.py 프로젝트: Pyomo/pyomo
 def __setstate__(self, state):
     self._metadata = state['_metadata']
     self._entry = {}
     for name, data in iteritems(state['_entry']):
         tmp = self._entry[name] = {}
         for obj, entry in data:
             tmp[ id(obj) ] = ( weakref_ref(obj), entry )
예제 #8
0
 def __setstate__(self, state):
     """
     This method must be defined to support unpickling because this class
     owns weakrefs for '_component'.
     """
     #
     # FIXME: We shouldn't have to check for weakref.ref here, but if
     # we don't the model cloning appears to fail (in the Benders
     # example)
     #
     if state['_component'] is not None and \
             type(state['_component']) is not weakref_ref:
         state['_component'] = weakref_ref(state['_component'])
     #
     # Note: our model for setstate is for derived classes to modify
     # the state dictionary as control passes up the inheritance
     # hierarchy (using super() calls).  All assignment of state ->
     # object attributes is handled at the last class before 'object'
     # (which may -- or may not (thanks to MRO) -- be here.
     #
     _base = super(ComponentData,self)
     if hasattr(_base, '__setstate__'):
         return _base.__setstate__(state)
     else:
         for key, val in iteritems(state):
             # Note: per the Python data model docs, we explicitly
             # set the attribute using object.__setattr__() instead
             # of setting self.__dict__[key] = val.
             object.__setattr__(self, key, val)
예제 #9
0
파일: var.py 프로젝트: Juanlu001/pyomo
 def __init__(self, domain=Reals, component=None):
     #
     # These lines represent in-lining of the
     # following constructors:
     #   - _VarData
     #   - ComponentData
     #   - NumericValue
     self._component = weakref_ref(component) if (component is not None) \
                       else None
     self._value = None
     #
     # The type of the lower and upper bound attributes can either
     # be atomic numeric types in Python, expressions, etc.
     # Basically, they can be anything that passes an "is_fixed" test.
     #
     self._lb = None
     self._ub = None
     self._domain = None
     self.fixed = False
     self.stale = True
     # don't call the property setter here because
     # the SimplVar constructor will fail
     if hasattr(domain, 'bounds'):
         self._domain = domain
     else:
         raise ValueError(
             "%s is not a valid domain. Variable domains must be an "
             "instance of one of %s an object that declares a method "
             "for bounds (like a Pyomo Set). Examples: NonNegativeReals, "
             "Integers, Binary" % (domain, (RealSet, IntegerSet, BooleanSet)))
예제 #10
0
 def getSymbol(self, obj, labeler=None, *args):
     """
     Return the symbol for an object.  If it has not already been cached
     in the symbol map, then create it.
     """
     obj_id = id(obj)
     if obj_id in self.byObject:
         return self.byObject[obj_id]
     #
     # Create a new symbol, performing an error check if it is a duplicate
     #
     if labeler is None:
         raise RuntimeError("Object %s is not in the symbol map. "
                            "Cannot create a new symbol without "
                            "a labeler." % obj.cname(True))
     symb = labeler(obj)
     if symb in self.bySymbol:
         if self.bySymbol[symb]() is not obj:
             raise RuntimeError(
                 "Duplicate symbol '%s' already associated with "
                 "component '%s' (conflicting component: '%s')"
                 % (symb, self.bySymbol[symb]().cname(True), obj.cname(True)) )
     self.bySymbol[symb] = weakref_ref(obj)
     self.byObject[obj_id] = symb
     return symb
예제 #11
0
파일: var.py 프로젝트: Juanlu001/pyomo
    def construct(self, data=None):
        """Construct this component."""

        if __debug__ and logger.isEnabledFor(logging.DEBUG):   #pragma:nocover
            try:
                name = str(self.cname(True))
            except:
                # Some Var components don't have a name yet, so just use the type
                name = type(self)
            if logger.isEnabledFor(logging.DEBUG):
                logger.debug(
                    "Constructing Variable, name=%s, from data=%s"
                    % (name, str(data)))

        if self._constructed:
            return
        self._constructed=True

        #
        # Construct _VarData objects for all index values
        #
        if not self.is_indexed():
            self._data[None] = self
            self._initialize_members([None])
        elif self._dense:
            # This loop is optimized for speed with pypy.
            # Calling dict.update((...) for ...) is roughly
            # 30% slower
            self_weakref = weakref_ref(self)
            for ndx in self._index:
                cdata = _GeneralVarData(Reals, component=None)
                cdata._component = self_weakref
                self._data[ndx] = cdata
            self._initialize_members(self._index)
예제 #12
0
 def __setitem__(self, i, item):
     if isinstance(item, self._interface_datatype):
         if item._component is None:
             item._component = weakref_ref(self)
             if hasattr(self, "_active"):
                 self._active |= getattr(item, '_active', True)
             # release the current component (assuming we don't get
             # an index error)
             # * see __delitem__ for explanation
             self._data[i]._component = None
             self._data[i] = item
             return
         # see note about allowing components to live in more than
         # one container
         raise ValueError(
             "Invalid component object assignment to ComponentList "
             "%s at index %s. A parent component has already been "
             "assigned the object: %s"
             % (self.cname(True),
                i,
                item.parent_component().cname(True)))
     # see note about implicit assignment and update
     raise TypeError(
         "ComponentList must be assigned objects "
         "of type %s. Invalid type for key %s: %s"
         % (self._interface_datatype.__name__,
            i,
            type(item)))
예제 #13
0
 def __init__(self, process):
     type = process.stdout
     if type not in ('null', 'file'):
         raise ValueError('Invalid stdout type: %r' % type)
     SessionAgent.__init__(self, process.project().session, "stdout file")
     self.type = type
     self.process = weakref_ref(process)
예제 #14
0
    def __getitem__(self, x):
        if isinstance(x,(tuple,list)):
            x = tuple(x)
        else:
            x = (x,)
        try:
            return self._children[x]
        except KeyError:
            Klass = self._value.__class__
            if Klass in _ItemWrapper:
                WKlass = _ItemWrapper[Klass]
            else:
                _ItemWrapper[Klass] = WKlass = self.wKlassFactory(Klass)

            child = WKlass()
            
            for i in filter(lambda x,K=list(child.__dict__.keys()): x in K,list(child._attrMap.keys())):
                del child.__dict__[i]
            child.__dict__.update(dict(
                                    __propholder_parent__ = weakref_ref(self),
                                    __propholder_index__ = x[:-1])
                                    )

            self._children[x] = child
            return child
예제 #15
0
파일: symbol_map.py 프로젝트: Pyomo/pyomo
 def getSymbol(self, obj, labeler=None, *args):
     """
     Return the symbol for an object.  If it has not already been cached
     in the symbol map, then create it.
     """
     obj_id = id(obj)
     if obj_id in self.byObject:
         return self.byObject[obj_id]
     #
     # Create a new symbol, performing an error check if it is a duplicate
     #
     if labeler:
         symb = labeler(obj)
     elif self.default_labeler:
         symb = self.default_labeler(obj)
     else:
         symb = str(obj)
     if symb in self.bySymbol:
         if self.bySymbol[symb]() is not obj:
             raise RuntimeError(
                 "Duplicate symbol '%s' already associated with "
                 "component '%s' (conflicting component: '%s')"
                 % (symb, self.bySymbol[symb]().name, obj.name) )
     self.bySymbol[symb] = weakref_ref(obj)
     self.byObject[obj_id] = symb
     return symb
예제 #16
0
    def __init__(self, application):
        self.application = weakref_ref(application)
        self.timestamp_format = '%(asctime)s: %(message)s'

        # Choose log levels
        if application.options.debug:
            stdout_level = INFO
            file_level = DEBUG
        elif application.options.verbose:
            stdout_level = WARNING
            file_level = INFO
        elif not application.options.quiet:
            stdout_level = ERROR
            file_level = WARNING
        else:
            stdout_level = ERROR
            file_level = INFO

        self.logger = getLogger()

        # fusil.log file
        self.filename = LOG_FILENAME
        self.file_handler = self.addFileHandler(self.filename, file_level)

        # Create stdout logger
        handler = StreamHandler(stdout)
        self.addHandler(handler, stdout_level)
예제 #17
0
 def __setitem__(self, key, val):
     if isinstance(val, self._interface_datatype):
         if val._component is None:
             val._component = weakref_ref(self)
             self._active |= getattr(val, '_active', True)
             if key in self._data:
                 # release the current component
                 # * see __delitem__ for explanation
                 self._data[key]._component = None
             self._data[key] = val
             return
         # see note about allowing components to live in more than
         # one container
         raise ValueError(
             "Invalid component object assignment to ComponentDict "
             "%s at key %s. A parent component has already been "
             "assigned the object: %s"
             % (self.cname(True),
                key,
                val.parent_component().cname(True)))
     # see note about implicit assignment and update
     raise TypeError(
         "ComponentDict must be assigned objects "
         "of type %s. Invalid type for key %s: %s"
         % (self._interface_datatype.__name__,
            key,
            type(val)))
예제 #18
0
파일: var.py 프로젝트: Juanlu001/pyomo
 def __init__(self, component=None):
     #
     # These lines represent in-lining of the
     # following constructors:
     #   - ComponentData
     #   - NumericValue
     self._component = weakref_ref(component) if (component is not None) \
                       else None
예제 #19
0
파일: symbol_map.py 프로젝트: Pyomo/pyomo
    def addSymbols(self, obj_symbol_tuples):
        """
        Add (object, symbol) tuples from an iterable object.

        This method assumes that symbol names will not conflict.
        """
        tuples = list((obj, symb) for obj,symb in obj_symbol_tuples)
        self.byObject.update((id(obj_), symb_) for obj_,symb_ in tuples)
        self.bySymbol.update((symb_, weakref_ref(obj_)) for obj_,symb_ in tuples)
예제 #20
0
파일: component.py 프로젝트: Pyomo/pyomo
 def __init__(self, component):
     #
     # ComponentData objects are typically *private* objects for
     # indexed / sparse indexed components.  As such, the (derived)
     # class needs to make sure that the owning component is *always*
     # passed as the owner (and that owner is never None).  Not validating
     # this assumption is significantly faster.
     #
     self._component = weakref_ref(component)
예제 #21
0
파일: field.py 프로젝트: BirdAPI/Sick-Beard
 def getSubIStream(self):
     if hasattr(self, "_sub_istream"):
         stream = self._sub_istream()
     else:
         stream = None
     if stream is None:
         stream = self._createInputStream()
         self._sub_istream = weakref_ref(stream)
     return stream
예제 #22
0
 def __init__(self, session, name, project=None):
     if project:
         mta = project.mta()
     else:
         mta = session.mta()
         project = session.project()
     self.session = weakref_ref(session)
     ProjectAgent.__init__(self, project, name, mta=mta)
     self.activate()
예제 #23
0
 def __init__(self, session, server, socket, address, family):
     self.server = weakref_ref(server)
     self.socket = socket
     self.address = address
     self.family = family
     name = "net_client:" + formatAddress(self.family, self.address, short=True)
     SessionAgent.__init__(self, session, name)
     self.tx_bytes = 0
     self.rx_bytes = 0
예제 #24
0
파일: constraint.py 프로젝트: qtothec/pyomo
 def __init__(self, component=None):
     #
     # These lines represent in-lining of the
     # following constructors:
     #   - _ConstraintData,
     #   - ActiveComponentData
     #   - ComponentData
     self._component = weakref_ref(component) if (component is not None) \
                       else None
     self._active = True
예제 #25
0
파일: param.py 프로젝트: qtothec/pyomo
 def __init__(self, owner, value):
     #
     # The following is equivalent to calling
     # the base ComponentData constructor.
     #
     self._component = weakref_ref(owner)
     #
     # The following is equivalent to calling the
     # base NumericValue constructor.
     #
     self.value = value
예제 #26
0
파일: opt.py 프로젝트: jdedecca/PyPSA
def empty_model(model):
    logger.debug("Storing pyomo model to disk")
    rules = {}
    for obj in model.component_objects(ctype=Constraint):
        if obj.rule is not None:
            rules[obj.name] = obj.rule
            obj.rule = None

    bounds = {}
    for obj in model.component_objects(ctype=Var):
        if obj._bounds_init_rule is not None:
            bounds[obj.name] = obj._bounds_init_rule
            obj._bounds_init_rule = None

    smap_id, symbol_map = (next(iteritems(model.solutions.symbol_map))
               if model.solutions.symbol_map
               else (None, None))
    if smap_id is not None:
        for m in ('bySymbol', 'aliases'):
            setattr(symbol_map, m,
                    {n: ComponentUID(obj())
                     for n, obj in iteritems(getattr(symbol_map, m))})

    fd, fn = tempfile.mkstemp()
    with os.fdopen(fd, 'wb') as f:
        pickle.dump(model.__getstate__(), f, -1)

    model.__dict__.clear()
    logger.debug("Stored pyomo model to disk")

    gc.collect()
    yield

    logger.debug("Reloading pyomo model")
    with open(fn, 'rb') as f:
        state = pickle.load(f)
    os.remove(fn)
    model.__setstate__(state)

    for n, rule in iteritems(rules):
        getattr(model, n).rule = rule

    for n, bound in iteritems(bounds):
        getattr(model, n)._bounds_init_rule = bound

    if smap_id is not None:
        for m in ('bySymbol', 'aliases'):
            setattr(symbol_map, m,
                    {n: weakref_ref(cuid.find_component(model))
                     for n, cuid in iteritems(getattr(symbol_map, m))})
        symbol_map.byObject = {id(obj()): symb
                               for symb, obj in iteritems(symbol_map.bySymbol)}
        model.solutions.symbol_map[smap_id] = symbol_map
    logger.debug("Reloaded pyomo model")
예제 #27
0
    def __init__(self, expr, component=None):
        #
        # These lines represent in-lining of the
        # following constructors:
        #   - _ExpressionData
        #   - ComponentData
        #   - NumericValue
        self._component = weakref_ref(component) if (component is not None) \
                          else None

        self._expr = as_numeric(expr) if (expr is not None) else None
예제 #28
0
파일: param.py 프로젝트: Pyomo/pyomo
 def __init__(self, component):
     #
     # The following is equivalent to calling
     # the base ComponentData constructor.
     #
     self._component = weakref_ref(component)
     #
     # The following is equivalent to calling the
     # base NumericValue constructor.
     #
     self._value = _NotValid
예제 #29
0
 def setupMTA(self, mta, logger=None):
     if mta:
         if logger:
             self.logger = logger
         else:
             self.logger = mta.logger
         self.mta = weakref_ref(mta)
         self.mailbox = Mailbox(self, mta)
     else:
         self.logger = None
         self.mta = None
         self.mailbox = None
예제 #30
0
파일: objective.py 프로젝트: qtothec/pyomo
    def __init__(self, expr, sense=minimize, component=None):
        _GeneralExpressionDataImpl.__init__(self, expr)
        # Inlining ActiveComponentData.__init__
        self._component = weakref_ref(component) if (component is not None) \
                          else None
        self._active = True
        self._sense = sense

        if (self._sense != minimize) and \
           (self._sense != maximize):
            raise ValueError("Objective sense must be set to one of "
                             "'minimize' (%s) or 'maximize' (%s). Invalid "
                             "value: %s'" % (minimize, maximize, sense))
예제 #31
0
    def __init__(self, application):
        ProjectAgent.__init__(self, self, "project", mta=application.mta())
        self.application = weakref_ref(application)
        self.agents = AgentList()
        if RUNNING_LINUX:
            if application.options.fast:
                self.system_calm = SystemCalm(0.75, 0.2)
            elif not application.options.slow:
                self.system_calm = SystemCalm(0.50, 0.5)
            else:
                self.system_calm = SystemCalm(0.30, 3.0)
        else:
            self.warning("SystemCalm class is not available")
            self.system_calm = None

        # Configuration
        self.max_session = application.options.session
        self.success_score = 0.50  # minimum score for a successful session
        self.error_score = -0.50  # maximum score for a session failure
        self.max_success = application.options.success

        # Session
        self.session = None
        self.session_index = 0
        self.session_timeout = None  # in second

        # Statistics
        self.session_executed = 0
        self.session_total_duration = 0
        self.total_duration = None

        # Add Application agents, order is important: MTA have to be the first agent
        for agent in application.agents:
            self.registerAgent(agent)
        self.registerAgent(self)

        # Create aggressivity agent
        self.aggressivity = AggressivityAgent(self)

        # Initial aggresssivity value
        if application.options.aggressivity is not None:
            self.aggressivity.setValue(application.options.aggressivity / 100)
            self.error("Initial aggressivity: %s" % self.aggressivity)
예제 #32
0
 def createSymbol(self, obj, labeler=None, *args):
     """
     Create a symbol for an object with a given labeler.  No
     error checking is done to ensure that the generated symbol
     name is unique.
     """
     #if args:
     #    symb = labeler(obj, *args)
     #else:
     #    symb = labeler(obj)
     if labeler:
         symb = labeler(obj)
     elif self.default_labeler:
         symb = self.default_labeler(obj)
     else:
         symb = str(obj)
     self.byObject[id(obj)] = symb
     self.bySymbol[symb] = weakref_ref(obj)
     return symb
예제 #33
0
 def insert(self, i, item):
     if isinstance(item, self._interface_datatype):
         if item._component is None:
             item._component = weakref_ref(self)
             if hasattr(self, "_active"):
                 self._active |= getattr(item, '_active', True)
             self._data.insert(i, item)
             return
         # see note about allowing components to live in more than
         # one container
         raise ValueError(
             "Invalid component object assignment to ComponentList "
             "%s at index %s. A parent component has already been "
             "assigned the object: %s" %
             (self.name, i, item.parent_component().name))
     # see note about implicit assignment and update
     raise TypeError("ComponentList must be assigned objects "
                     "of type %s. Invalid type for key %s: %s" %
                     (self._interface_datatype.__name__, i, type(item)))
예제 #34
0
    def __setstate__(self, state):
        """Restore a picked state into this instance.

        Note: adapted from class ComponentData in pyomo.core.base.component

        """
        if state['_associated_binary'] is not None and type(
                state['_associated_binary']) is not weakref_ref:
            state['_associated_binary'] = weakref_ref(
                state['_associated_binary'])

        _base = super(_GeneralBooleanVarData, self)
        if hasattr(_base, '__setstate__'):
            _base.__setstate__(state)
        else:
            for key, val in iteritems(state):
                # Note: per the Python data model docs, we explicitly
                # set the attribute using object.__setattr__() instead
                # of setting self.__dict__[key] = val.
                object.__setattr__(self, key, val)
 def __init__(self,**kwds):
     self.strokeWidth = kwds.pop('strokeWidth',1)
     self.strokeColor = kwds.pop('strokeColor',colors.black)
     self.strokeDashArray = kwds.pop('strokeDashArray',None)
     self.crossWidth = kwds.pop('crossWidth',5)
     self.crossLo = kwds.pop('crossLo',None)
     self.crossHi = kwds.pop('crossHi',None)
     self.boxWidth = kwds.pop('boxWidth',None)
     self.boxFillColor = kwds.pop('boxFillColor',None)
     self.boxStrokeColor =kwds.pop('boxStrokeColor',NotSetOr._not_set) 
     self.boxStrokeWidth =kwds.pop('boxStrokeWidth',NotSetOr._not_set) 
     self.boxStrokeDashArray =kwds.pop('boxStrokeDashArray',NotSetOr._not_set) 
     self.boxLo = kwds.pop('boxLo',None)
     self.boxMid = kwds.pop('boxMid',None)
     self.boxHi = kwds.pop('boxHi',None)
     self.boxSides = kwds.pop('boxSides',True)
     self.position = kwds.pop('position',None)
     self.candleKind = kwds.pop('candleKind','vertical')
     self.axes = kwds.pop('axes',['categoryAxis','valueAxis'])
     chart = kwds.pop('chart',None)
     self.chart = weakref_ref(chart) if chart else (lambda:None)
예제 #36
0
파일: var.py 프로젝트: michaelbynum/pyomo
 def __init__(self, component=None):
     #
     # These lines represent in-lining of the
     # following constructors:
     #   - _VarData
     #   - ComponentData
     #   - NumericValue
     self._component = weakref_ref(component) if (component is not None) \
                       else None
     self._value = None
     #
     # The type of the lower and upper bound attributes can either be
     # atomic numeric types in Python, expressions, etc.  Basically,
     # they can be anything that passes an "not
     # is_potentially_variable" test.
     #
     self._lb = None
     self._ub = None
     self._domain = None
     self._fixed = False
     self._stale = 0 # True
예제 #37
0
 def __setitem__(self, i, item):
     if isinstance(item, self._interface_datatype):
         if item._component is None:
             item._component = weakref_ref(self)
             self._active |= getattr(item, '_active', True)
             # release the current component (assuming we don't get
             # an index error)
             # * see __delitem__ for explanation
             self._data[i]._component = None
             self._data[i] = item
             return
         # see note about allowing components to live in more than
         # one container
         raise ValueError(
             "Invalid component object assignment to ComponentList "
             "%s at index %s. A parent component has already been "
             "assigned the object: %s" %
             (self.cname(True), i, item.parent_component().cname(True)))
     # see note about implicit assignment and update
     raise TypeError("ComponentList must be assigned objects "
                     "of type %s. Invalid type for key %s: %s" %
                     (self._interface_datatype.__name__, i, type(item)))
예제 #38
0
    def __getitem__(self, x):
        x = tuple(x) if isinstance(x, (tuple, list)) else (x, )
        try:
            return self._children[x]
        except KeyError:
            Klass = self._value.__class__
            if Klass in _ItemWrapper:
                WKlass = _ItemWrapper[Klass]
            else:
                _ItemWrapper[Klass] = WKlass = self.wKlassFactory(Klass)

            child = WKlass()

            for i in filter(lambda x, K=list(child.__dict__.keys()): x in K,
                            list(child._attrMap.keys())):
                del child.__dict__[i]
            child.__dict__.update(
                dict(__propholder_parent__=weakref_ref(self),
                     __propholder_index__=x[:-1]))

            self._children[x] = child
            return child
예제 #39
0
    def __init__(self, instance, resolution_level=0, trace=False):
        """
        Create a weak reference for 'instance' to observe an object but which
        won't prevent its deletion (which is monitored by the finalize
        callback). The size of the object is recorded in 'snapshots' as
        (timestamp, size) tuples.
        """
        self.ref = weakref_ref(instance, self.finalize)
        self.id = id(instance)
        self.repr = ''
        self.name = str(instance.__class__)
        self.birth = _get_time()
        self.death = None
        self._resolution_level = resolution_level
        self.trace = None

        if trace:
            self._save_trace()

        initial_size = asizeof.basicsize(instance) or 0
        size = asizeof.Asized(initial_size, initial_size)
        self.snapshots = [(self.birth, size)]
예제 #40
0
    def construct(self, data=None):
        """
        Initialize this component.
        """
        if self._constructed:
            return
        timer = ConstructionTimer(self)

        if not self.is_indexed():
            self._data[None] = self
            self._initialize_members((None,))

        else:
            self_weakref = weakref_ref(self)
            for ndx in self._index:
                if ndx not in self._data:
                    self._data[ndx] = _AdjustableVarData(self)
                self._component = self_weakref
            self._initialize_members(self._index)

        self._constructed = True
        timer.report()
예제 #41
0
파일: var.py 프로젝트: CanLi1/pyomo-1
    def construct(self, data=None):
        """Construct this component."""
        if __debug__ and logger.isEnabledFor(logging.DEBUG):   #pragma:nocover
            try:
                name = str(self.name)
            except:
                # Some Var components don't have a name yet, so just use
                # the type
                name = type(self)
            if logger.isEnabledFor(logging.DEBUG):
                logger.debug(
                    "Constructing Variable, name=%s, from data=%s"
                    % (name, str(data)))

        if self._constructed:
            return
        timer = ConstructionTimer(self)
        self._constructed=True

        #
        # Construct _VarData objects for all index values
        #
        if not self.is_indexed():
            self._data[None] = self
            self._initialize_members((None,))
        elif self._dense:
            # This loop is optimized for speed with pypy.
            # Calling dict.update((...) for ...) is roughly
            # 30% slower
            self_weakref = weakref_ref(self)
            for ndx in self._index:
                cdata = self._ComponentDataClass(
                    domain=self._domain_init_value, component=None)
                cdata._component = self_weakref
                self._data[ndx] = cdata
                #self._initialize_members((ndx,))
            self._initialize_members(self._index)
        timer.report()
예제 #42
0
 def __setstate__(self, state):
     """
     This method must be defined to support pickling because this class
     owns weakrefs for '_parent'.
     """
     if state['_parent'].__class__ not in _ref_types:
         state['_parent'] = weakref_ref(state['_parent'])
     #
     # Note: our model for setstate is for derived classes to modify
     # the state dictionary as control passes up the inheritance
     # hierarchy (using super() calls).  All assignment of state ->
     # object attributes is handled at the last class before 'object'
     # (which may -- or may not (thanks to MRO) -- be here.
     #
     _base = super(Component, self)
     if hasattr(_base, '__setstate__'):
         _base.__setstate__(state)
     else:
         for key, val in state.items():
             # Note: per the Python data model docs, we explicitly
             # set the attribute using object.__setattr__() instead
             # of setting self.__dict__[key] = val.
             object.__setattr__(self, key, val)
예제 #43
0
 def __setitem__(self, key, val):
     if isinstance(val, self._interface_datatype):
         if val._component is None:
             val._component = weakref_ref(self)
             if hasattr(self, "_active"):
                 self._active |= getattr(val, '_active', True)
             if key in self._data:
                 # release the current component
                 # * see __delitem__ for explanation
                 self._data[key]._component = None
             self._data[key] = val
             return
         # see note about allowing components to live in more than
         # one container
         raise ValueError(
             "Invalid component object assignment to ComponentDict "
             "%s at key %s. A parent component has already been "
             "assigned the object: %s" %
             (self.cname(True), key, val.parent_component().cname(True)))
     # see note about implicit assignment and update
     raise TypeError("ComponentDict must be assigned objects "
                     "of type %s. Invalid type for key %s: %s" %
                     (self._interface_datatype.__name__, key, type(val)))
예제 #44
0
 def alias(self, obj, name):
     """
     Create an alias for an object.  An aliases are symbols that
     do not have a one-to-one correspondence with objects.
     """
     if name in self.aliases:
         #
         # If the alias exists and the objects are the same,
         # then return.  Otherwise, raise an exception.
         #
         old_object = self.aliases[name]()
         if old_object is obj:
             return
         else:
             raise RuntimeError(
                 "Duplicate alias '%s' already associated with "
                 "component '%s' (conflicting component: '%s')"
                 % (name, "UNKNOWN" if old_object is None else old_object.name, obj.name) )
     else:
         #
         # Add the alias
         #
         self.aliases[name] = weakref_ref(obj)
예제 #45
0
    def construct(self, data=None):
        """Construct this component."""
        if is_debug_set(logger):  #pragma:nocover
            try:
                name = str(self.name)
            except:
                name = type(self)
            logger.debug("Constructing Variable, name=%s, from data=%s" %
                         (name, str(data)))

        if self._constructed:
            return
        timer = ConstructionTimer(self)
        self._constructed = True

        #
        # Construct _BooleanVarData objects for all index values
        #
        if not self.is_indexed():
            self._data[None] = self
            self._initialize_members((None, ))
        elif self._dense:
            # This loop is optimized for speed with pypy.
            # Calling dict.update((...) for ...) is roughly
            # 30% slower
            self_weakref = weakref_ref(self)
            for ndx in self._index_set:
                cdata = self._ComponentDataClass(component=None)
                cdata._component = self_weakref
                self._data[ndx] = cdata
                # NOTE: This is a special case where a key, value pair is added
                # to the _data dictionary without calling
                # _getitem_when_not_present, which is why we need to set the
                # index here.
                cdata._index = ndx
            self._initialize_members(self._index_set)
        timer.report()
예제 #46
0
파일: component.py 프로젝트: knut0815/pyomo
    def __setstate__(self, state):
        """Restore a pickled state into this instance

        Note: our model for setstate is for derived classes to modify
        the state dictionary as control passes up the inheritance
        hierarchy (using super() calls).  All assignment of state ->
        object attributes is handled at the last class before 'object'
        (which may -- or may not (thanks to MRO) -- be here.

        This method must be defined to support unpickling because this
        class owns weakrefs for '_component', which must be restored
        from the hard references used in the piclke.
        """
        #
        # FIXME: We shouldn't have to check for weakref.ref here, but if
        # we don't the model cloning appears to fail (in the Benders
        # example)
        #
        if state['_component'] is not None and \
                type(state['_component']) is not weakref_ref:
            state['_component'] = weakref_ref(state['_component'])
        #
        # Note: our model for setstate is for derived classes to modify
        # the state dictionary as control passes up the inheritance
        # hierarchy (using super() calls).  All assignment of state ->
        # object attributes is handled at the last class before 'object'
        # (which may -- or may not (thanks to MRO) -- be here.
        #
        _base = super(ComponentData, self)
        if hasattr(_base, '__setstate__'):
            _base.__setstate__(state)
        else:
            for key, val in iteritems(state):
                # Note: per the Python data model docs, we explicitly
                # set the attribute using object.__setattr__() instead
                # of setting self.__dict__[key] = val.
                object.__setattr__(self, key, val)
예제 #47
0
 def __setitem__(self, key, val):
     if isinstance(val, self._interface_datatype):
         if val._component is None:
             val._component = weakref_ref(self)
             if hasattr(self, "_active"):
                 self._active |= getattr(val, '_active', True)
             if key in self._data:
                 # release the current component
                 # * see __delitem__ for explanation
                 self._data[key]._component = None
             self._data[key] = val
             return
         elif (key in self._data) and (self._data[key] is val):
             # a very special case that makes sense to handle
             # because the implied order should be: (1) delete
             # the object at the current index, (2) insert the
             # the new object. This performs both without any
             # actions, but it is an extremely rare case, so
             # it should go last.
             return
         # see note about allowing components to live in more than
         # one container
         raise ValueError(
             "Invalid component object assignment to ComponentDict "
             "%s at key %s. A parent component has already been "
             "assigned the object: %s"
             % (self.name,
                key,
                val.parent_component().name))
     # see note about implicit assignment and update
     raise TypeError(
         "ComponentDict must be assigned objects "
         "of type %s. Invalid type for key %s: %s"
         % (self._interface_datatype.__name__,
            key,
            type(val)))
예제 #48
0
    def construct(self, data=None):
        """
        Initialize this component.
        """
        if self._constructed:
            return
        timer = ConstructionTimer(self)

        nom = self._nominal

        if not self.is_indexed():
            self._data[None] = self

        else:
            self_weakref = weakref_ref(self)
            for ndx in self._index:
                if ndx not in self._data:
                    self._data[ndx] = _UncParamData(self)
                self._data[ndx]._nominal = nom[ndx]
                self._data[ndx]._value = nom[ndx]
                self._component = self_weakref

        self._constructed = True
        timer.report()
예제 #49
0
    def _add_xor_constraint(self, disjunction, transBlock):
        # Put XOR constraint on the transformation block

        # We never do this for just a DisjunctionData because we need
        # to know about the index set of its parent component. So if
        # we called this on a DisjunctionData, we did something wrong.
        assert isinstance(disjunction, Disjunction)

        # check if the constraint already exists
        if disjunction._algebraic_constraint is not None:
            return disjunction._algebraic_constraint()

        # add the XOR (or OR) constraints to parent block (with
        # unique name) It's indexed if this is an
        # IndexedDisjunction, not otherwise
        orC = Constraint(disjunction.index_set())
        transBlock.add_component(
            unique_component_name(transBlock,
                                  disjunction.getname(fully_qualified=True,
                                                      name_buffer=NAME_BUFFER) +\
                                  '_xor'), orC)
        disjunction._algebraic_constraint = weakref_ref(orC)

        return orC
예제 #50
0
 def __init__(self, component=None):
     self._component = weakref_ref(component) if (component is not None) \
                       else None
예제 #51
0
    def _transform_disjunct(self, obj, transBlock, bigM, arg_list, suffix_list):
        # deactivated -> either we've already transformed or user deactivated
        if not obj.active:
            if obj.indicator_var.is_fixed():
                if value(obj.indicator_var) == 0:
                    # The user cleanly deactivated the disjunct: there
                    # is nothing for us to do here.
                    return
                else:
                    raise GDP_Error(
                        "The disjunct '%s' is deactivated, but the "
                        "indicator_var is fixed to %s. This makes no sense."
                        % ( obj.name, value(obj.indicator_var) ))
            if obj._transformation_block is None:
                raise GDP_Error(
                    "The disjunct '%s' is deactivated, but the "
                    "indicator_var is not fixed and the disjunct does not "
                    "appear to have been relaxed. This makes no sense. "
                    "(If the intent is to deactivate the disjunct, fix its "
                    "indicator_var to 0.)"
                    % ( obj.name, ))

        if obj._transformation_block is not None:
            # we've transformed it, which means this is the second time it's
            # appearing in a Disjunction
            raise GDP_Error(
                    "The disjunct '%s' has been transformed, but a disjunction "
                    "it appears in has not. Putting the same disjunct in "
                    "multiple disjunctions is not supported." % obj.name)

        # add reference to original disjunct on transformation block
        relaxedDisjuncts = transBlock.relaxedDisjuncts
        relaxationBlock = relaxedDisjuncts[len(relaxedDisjuncts)]
        # we will keep a map of constraints (hashable, ha!) to a tuple to
        # indicate what their M value is and where it came from, of the form:
        # ((lower_value, lower_source, lower_key), (upper_value, upper_source,
        # upper_key)), where the first tuple is the information for the lower M,
        # the second tuple is the info for the upper M, source is the Suffix or
        # argument dictionary and None if the value was calculated, and key is
        # the key in the Suffix or argument dictionary, and None if it was
        # calculated. (Note that it is possible the lower or upper is
        # user-specified and the other is not, hence the need to store
        # information for both.)
        relaxationBlock.bigm_src = {}
        relaxationBlock.localVarReferences = Block()
        obj._transformation_block = weakref_ref(relaxationBlock)
        relaxationBlock._srcDisjunct = weakref_ref(obj)

        # This is crazy, but if the disjunction has been previously
        # relaxed, the disjunct *could* be deactivated.  This is a big
        # deal for Hull, as it uses the component_objects /
        # component_data_objects generators.  For BigM, that is OK,
        # because we never use those generators with active=True.  I am
        # only noting it here for the future when someone (me?) is
        # comparing the two relaxations.
        #
        # Transform each component within this disjunct
        self._transform_block_components(obj, obj, bigM, arg_list, suffix_list)

        # deactivate disjunct to keep the writers happy
        obj._deactivate_without_fixing_indicator()
예제 #52
0
 def associate_binary_var(self, binary_var):
     """Associate a binary _VarData to this _GeneralBooleanVarData"""
     self._associated_binary = weakref_ref(binary_var) if binary_var is not None else None
예제 #53
0
    def add_solution(self,
                     solution,
                     smap_id,
                     delete_symbol_map=True,
                     cache=None,
                     ignore_invalid_labels=False,
                     ignore_missing_symbols=True,
                     default_variable_value=None):

        instance = self._instance()

        soln = ModelSolution()
        soln._metadata['status'] = solution.status
        if not type(solution.message) is UndefinedData:
            soln._metadata['message'] = solution.message
        if not type(solution.gap) is UndefinedData:
            soln._metadata['gap'] = solution.gap

        if smap_id is None:
            #
            # Cache symbol names, which might be re-used in subsequent
            # calls to add_solution()
            #
            if cache is None:
                cache = {}
            if solution._cuid:
                #
                # Loading a solution with CUID keys
                #
                if len(cache) == 0:
                    for obj in instance.component_data_objects(Var):
                        cache[ComponentUID(obj)] = obj
                    for obj in instance.component_data_objects(Objective,
                                                               active=True):
                        cache[ComponentUID(obj)] = obj
                    for obj in instance.component_data_objects(Constraint,
                                                               active=True):
                        cache[ComponentUID(obj)] = obj

                for name in ['problem', 'objective', 'variable', 'constraint']:
                    tmp = soln._entry[name]
                    for cuid, val in getattr(solution, name).items():
                        obj = cache.get(cuid, None)
                        if obj is None:
                            if ignore_invalid_labels:
                                continue
                            raise RuntimeError(
                                "CUID %s is missing from model %s" %
                                (str(cuid), instance.name))
                        tmp[id(obj)] = (weakref_ref(obj), val)
            else:
                #
                # Loading a solution with string keys
                #
                if len(cache) == 0:
                    for obj in instance.component_data_objects(Var):
                        cache[obj.name] = obj
                    for obj in instance.component_data_objects(Objective,
                                                               active=True):
                        cache[obj.name] = obj
                    for obj in instance.component_data_objects(Constraint,
                                                               active=True):
                        cache[obj.name] = obj

                for name in ['problem', 'objective', 'variable', 'constraint']:
                    tmp = soln._entry[name]
                    for symb, val in getattr(solution, name).items():
                        obj = cache.get(symb, None)
                        if obj is None:
                            if ignore_invalid_labels:
                                continue
                            raise RuntimeError(
                                "Symbol %s is missing from model %s" %
                                (symb, instance.name))
                        tmp[id(obj)] = (weakref_ref(obj), val)
        else:
            #
            # Map solution
            #
            smap = self.symbol_map[smap_id]
            for name in ['problem', 'objective', 'variable', 'constraint']:
                tmp = soln._entry[name]
                for symb, val in getattr(solution, name).items():
                    if symb in smap.bySymbol:
                        obj = smap.bySymbol[symb]
                    elif symb in smap.aliases:
                        obj = smap.aliases[symb]
                    elif ignore_missing_symbols:
                        continue
                    else:  #pragma:nocover
                        #
                        # This should never happen ...
                        #
                        raise RuntimeError(
                            "ERROR: Symbol %s is missing from "
                            "model %s when loading with a symbol map!" %
                            (symb, instance.name))

                    tmp[id(obj())] = (obj, val)
            #
            # Wrap up
            #
            if delete_symbol_map:
                self.delete_symbol_map(smap_id)

        #
        # Collect fixed variables
        #
        tmp = soln._entry['variable']
        for vdata in instance.component_data_objects(Var):
            id_ = id(vdata)
            if vdata.fixed:
                tmp[id_] = (weakref_ref(vdata), {'Value': value(vdata)})
            elif (default_variable_value is not None) and \
                 (smap_id is not None) and \
                 (id_ in smap.byObject) and \
                 (id_ not in tmp):
                tmp[id_] = (weakref_ref(vdata), {
                    'Value': default_variable_value
                })

        self.solutions.append(soln)
        return len(self.solutions) - 1
예제 #54
0
 def __setstate__(self, state):
     for key, val in state.items():
         setattr(self, key, val)
     # Restore the instance weakref
     self._instance = weakref_ref(self._instance)
예제 #55
0
 def __init__(self, instance):
     self._instance = weakref_ref(instance)
     self.clear()
예제 #56
0
 def __setstate__(self, state):
     super(_GeneralExpressionDataImpl, self).__setstate__(state)
     if safe_mode:
         if self._parent_expr is not None:
             self._parent_expr = weakref_ref(self._parent_expr)
예제 #57
0
 def addSymbol(self, obj, symb):
     """
     Add a symbol for a given object
     """
     self.byObject[id(obj)] = symb
     self.bySymbol[symb] = weakref_ref(obj)
예제 #58
0
 def __init__(self, owner, **kwargs):
     self._owner = weakref_ref(owner)
     super().__init__(**kwargs)
     self._component = weakref_ref(self)
     self.construct()
예제 #59
0
 def __init__(self, expr, component=None):
     _GeneralExpressionDataImpl.__init__(self, expr)
     # Inlining ComponentData.__init__
     self._component = weakref_ref(component) if (component is not None) \
                       else None
예제 #60
0
 def __setstate__(self, state):
     _owner = state.pop('_owner')
     super().__setstate__(state)
     self._owner = None if _owner is None else weakref_ref(_owner)