예제 #1
0
    def set_value(self, value, idx=NOTSET):
        #
        # If this param has units, then we need to check the incoming
        # value and see if it is "units compatible".  We only need to
        # check here in set_value, because all united Params are
        # required to be mutable.
        #
        _comp = self.parent_component()
        if type(value) in native_types:
            # TODO: warn/error: check if this Param has units: assigning
            # a dimensionless value to a united param should be an error
            pass
        elif _comp._units is not None:
            _src_magnitude = expr_value(value)
            _src_units = units.get_units(value)
            value = units.convert_value(num_value=_src_magnitude,
                                        from_units=_src_units,
                                        to_units=_comp._units)

        old_value, self._value = self._value, value
        try:
            _comp._validate_value(idx, value, data=self)
        except:
            self._value = old_value
            raise
예제 #2
0
파일: var.py 프로젝트: jsiirola/pyomo
    def set_value(self, val, valid=False):
        """
        Set the value of this numeric object, after
        validating its value. If the 'valid' flag is True,
        then the validation step is skipped.
        """
        if not valid and val is not None:
            # TODO: warn/error: check if this Var has units: assigning
            # a dimensionless value to a united variable 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)

            if val not in self.domain:
                raise ValueError("Numeric value `%s` (%s) is not in "
                                 "domain %s for Var %s" %
                                 (val, type(val), self.domain, self.name))

        self.value = val
        self.stale = False
예제 #3
0
파일: var.py 프로젝트: jsiirola/pyomo
 def value(self, val):
     """Set the value for this variable."""
     if type(val) in native_numeric_types:
         # TODO: warn/error: check if this Var has units: assigning
         # a dimensionless value to a united variable should be an error
         pass
     elif val is not None and 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)
     self._value = val
예제 #4
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)
예제 #5
0
 def setlb(self, val):
     """
     Set the lower bound for this variable after validating that
     the value is fixed (or None).
     """
     # Note: is_fixed(None) returns True
     if not is_fixed(val):
         raise ValueError(
             "Non-fixed input of type '%s' supplied as variable lower "
             "bound - legal types must be fixed expressions or variables."
             % (type(val),))
     if type(val) in native_numeric_types or val is None:
         # TODO: warn/error: check if this Var has units: assigning
         # a dimensionless value to a united variable should be an error
         pass
     else:
         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)
     self._lb = val