Beispiel #1
0
    def set_value(self, val, skip_validation=False):
        """Set the current variable value.

        Set the value of this variable.  The incoming value is converted
        to a numeric value (i.e., expressions are evaluated).  If the
        variable has units, the incoming value is converted to the
        correct units before storing the value.  The final value is
        checked against both the variable domain and bounds, and an
        exception is raised if the value is not valid.  Domain and
        bounds checking can be bypassed by setting the ``skip_validation``
        argument to :const:`True`.

        """
        # Special case: setting a variable to None "clears" the variable.
        if val is None:
            self._value = None
            self._stale = 0  # True
            return
        # TODO: generate a warning/error:
        #
        # Check if this Var has units: assigning dimensionless
        # values to a variable with units should be an error
        if type(val) not in native_numeric_types:
            if self.parent_component()._units is not None:
                _src_magnitude = value(val)
                _src_units = units.get_units(val)
                val = units.convert_value(
                    num_value=_src_magnitude,
                    from_units=_src_units,
                    to_units=self.parent_component()._units)
            else:
                val = value(val)

        if not skip_validation:
            if val not in self.domain:
                logger.warning(
                    "Setting Var '%s' to a value `%s` (%s) not in domain %s." %
                    (self.name, val, type(val).__name__, self.domain),
                    extra={'id': 'W1001'},
                )
            elif (self._lb is not None
                  and val < value(self._lb)) or (self._ub is not None
                                                 and val > value(self._ub)):
                logger.warning(
                    "Setting Var '%s' to a numeric value `%s` "
                    "outside the bounds %s." % (self.name, val, self.bounds),
                    extra={'id': 'W1002'},
                )

        self._value = val
        self._stale = StaleFlagManager.get_flag(self._stale)
Beispiel #2
0
    def __setstate__(self, state):
        """Restore a picked state into this instance.

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

        """
        if state.pop('_stale', True):
            state['_stale'] = 0
        else:
            state['_stale'] = StaleFlagManager.get_flag(0)
        super().__setstate__(state)
        if self._associated_binary is not None and \
           type(self._associated_binary) is not \
           _DeprecatedImplicitAssociatedBinaryVariable:
            self._associated_binary = weakref_ref(self._associated_binary)
Beispiel #3
0
 def set_value(self, val, skip_validation=False):
     """
     Set the value of this numeric object, after
     validating its value. If the 'valid' flag is True,
     then the validation step is skipped.
     """
     # Note that it is basically as fast to check the type as it is
     # to check the skip_validation flag.  Considering that we expect
     # the flag to always be False, we will just ignore it in the
     # name of efficiency.
     if val.__class__ not in _logical_var_types:
         if not skip_validation:
             logger.warning("implicitly casting '%s' value %s to bool" %
                            (self.name, val))
         val = bool(val)
     self._value = val
     self._stale = StaleFlagManager.get_flag(self._stale)
Beispiel #4
0
 def stale(self, val):
     if val:
         self._stale = 0 # True
     else:
         self._stale = StaleFlagManager.get_flag(0)
Beispiel #5
0
 def __setstate__(self, state):
     if state.pop('_stale', True):
         state['_stale'] = 0
     else:
         state['_stale'] = StaleFlagManager.get_flag(0)
     super().__setstate__(state)
Beispiel #6
0
 def stale(self, stale):
     if stale:
         self._stale = 0
     else:
         self._stale = StaleFlagManager.get_flag(0)
Beispiel #7
0
 def value(self, value):
     self._value = value
     self._stale = StaleFlagManager.get_flag(self._stale)