if var.ctype is not IVariable: continue if (not allow_consistent_values_for_fixed_vars) and \ var.fixed: raise ValueError("Variable '%s' is currently fixed. " "A new value is not expected " "in solution" % (var.name)) if allow_consistent_values_for_fixed_vars and \ var.fixed and \ (math.fabs(default_variable_value - var.value) > \ comparison_tolerance_for_fixed_vars): raise ValueError( "Variable %s is currently fixed. " "A value of '%s' in solution is " "not within tolerance=%s of the current " "value of '%s'" % (var.name, default_variable_value, comparison_tolerance_for_fixed_vars, var.value)) var.value = default_variable_value var.stale = False # inserts class definitions for simple _tuple, _list, and # _dict containers into this module define_simple_containers(globals(), "block", IBlock) # populate the initial set of reserved block attributes so # that users can not overwrite them when building a model block._refresh_block_reserved_words()
class parameter(IParameter): """A placeholder for a mutable, numeric value.""" _ctype = IParameter __slots__ = ("_parent", "_storage_key", "_active", "_value", "__weakref__") def __init__(self, value=None): self._parent = None self._storage_key = None self._active = True self._value = value # # Define the IParameter abstract methods # @property def value(self): """The value of the paramater""" return self._value @value.setter def value(self, value): self._value = value # inserts class definitions for simple _tuple, _list, and # _dict containers into this module define_simple_containers(globals(), "parameter", IParameter)
from pyomo.repn.standard_repn import \ StandardRepn variables = [] coefficients = [] constant = 0 for v, c in self.terms: if v.is_expression_type(): v = v.expr if not v.fixed: variables.append(v) if compute_values: coefficients.append(value(c)) else: coefficients.append(c) else: if compute_values: constant += value(c) * v() else: constant += c * v repn = StandardRepn() repn.linear_vars = tuple(variables) repn.linear_coefs = tuple(coefficients) repn.constant = constant return repn # inserts class definitions for simple _tuple, _list, and # _dict containers into this module define_simple_containers(globals(), "constraint", IConstraint)
# Overload a few methods # def is_potentially_variable(self): """A boolean indicating whether this expression can reference variables.""" return False def polynomial_degree(self): """Always return zero because we always validate that the stored expression can never reference variables.""" return 0 @property def expr(self): return self._expr @expr.setter def expr(self, expr): if (expr is not None) and \ (not is_numeric_data(expr)): raise ValueError("Expression is not restricted to " "numeric data.") self._expr = expr # inserts class definitions for simple _tuple, _list, and # _dict containers into this module define_simple_containers(globals(), "expression", IExpression)
def variables(self): return self._variables @property def weights(self): return self._weights @property def level(self): return self._level def sos1(variables, weights=None): """A Special Ordered Set of type 1. This is an alias for sos(..., level=1)""" return sos(variables, weights=weights, level=1) def sos2(variables, weights=None): """A Special Ordered Set of type 2. This is an alias for sos(..., level=2). """ return sos(variables, weights=weights, level=2) # inserts class definitions for simple _tuple, _list, and # _dict containers into this module define_simple_containers(globals(), "sos", ISOS)
def expr(self): return self._expr @expr.setter def expr(self, expr): self._expr = as_numeric(expr) if (expr is not None) else None # # Define the IObjective abstract methods # @property def sense(self): return self._sense @sense.setter def sense(self, sense): """Set the sense (direction) of this objective.""" if (sense == minimize) or \ (sense == maximize): self._sense = sense else: raise ValueError("Objective sense must be set to one of: " "[minimize (%s), maximize (%s)]. Invalid " "value: %s'" % (minimize, maximize, sense)) # inserts class definitions for simple _tuple, _list, and # _dict containers into this module define_simple_containers(globals(), "objective", IObjective)
@property def domain_type(self): """The domain type of the variable (:class:`RealSet` or :class:`IntegerSet`)""" return self._domain_type @domain_type.setter def domain_type(self, domain_type): if domain_type not in IVariable._valid_domain_types: raise ValueError("Domain type '%s' is not valid. Must be " "one of: %s" % (self.domain_type, IVariable._valid_domain_types)) self._domain_type = domain_type def _set_domain(self, domain): """Set the domain of the variable. This method updates the :attr:`domain_type` property and overwrites the :attr:`lb` and :attr:`ub` properties with the domain bounds.""" self.domain_type, self.lb, self.ub = \ _extract_domain_type_and_bounds(None, domain, None, None) domain = property(fset=_set_domain, doc=_set_domain.__doc__) # inserts class definitions for simple _tuple, _list, and # _dict containers into this module define_simple_containers(globals(), "variable", IVariable)
# # Define the ISOS abstract methods # @property def variables(self): return self._variables @property def weights(self): return self._weights @property def level(self): return self._level def sos1(variables, weights=None): """A Special Ordered Set of type 1. This is an alias for sos(..., level=1)""" return sos(variables, weights=weights, level=1) def sos2(variables, weights=None): """A Special Ordered Set of type 2. This is an alias for sos(..., level=2). """ return sos(variables, weights=weights, level=2) # inserts class definitions for simple _tuple, _list, and # _dict containers into this module define_simple_containers(globals(), "sos", ISOS)
def domain_type(self): """The domain type of the variable (:class:`RealSet` or :class:`IntegerSet`)""" return self._domain_type @domain_type.setter def domain_type(self, domain_type): if domain_type not in IVariable._valid_domain_types: raise ValueError( "Domain type '%s' is not valid. Must be " "one of: %s" % (self.domain_type, IVariable._valid_domain_types)) self._domain_type = domain_type def _set_domain(self, domain): """Set the domain of the variable. This method updates the :attr:`domain_type` property and overwrites the :attr:`lb` and :attr:`ub` properties with the domain bounds.""" self.domain_type, self.lb, self.ub = \ _extract_domain_type_and_bounds(None, domain, None, None) domain = property(fset=_set_domain, doc=_set_domain.__doc__) # inserts class definitions for simple _tuple, _list, and # _dict containers into this module define_simple_containers(globals(), "variable", IVariable)
if (not allow_consistent_values_for_fixed_vars) and \ var.fixed: raise ValueError("Variable '%s' is currently fixed. " "A new value is not expected " "in solution" % (var.name)) if allow_consistent_values_for_fixed_vars and \ var.fixed and \ (math.fabs(default_variable_value - var.value) > \ comparison_tolerance_for_fixed_vars): raise ValueError( "Variable %s is currently fixed. " "A value of '%s' in solution is " "not within tolerance=%s of the current " "value of '%s'" % (var.name, default_variable_value, comparison_tolerance_for_fixed_vars, var.value)) var.value = default_variable_value var.stale = False # inserts class definitions for simple _tuple, _list, and # _dict containers into this module define_simple_containers(globals(), "block", IBlock) # populate the initial set of reserved block attributes so # that users can not overwrite them when building a model block._refresh_block_reserved_words()