Piecewise
(class from pyomo.core.base.piecewise
)
- class pyomo.core.base.piecewise.Piecewise(*args, **kwds)[source]
Bases:
Block
Adds piecewise constraints to a Pyomo model for functions of the form, y = f(x).
Examples
model.const = Piecewise(index_1,...,index_n,yvar,xvar,**Keywords) model.const = Piecewise(yvar,xvar,**Keywords)
- Parameters:
pw_pts (dict) – A dictionary of lists (keys are index set) or a single list (for the non-indexed case or when an identical set of breakpoints is used across all indices) defining the set of domain breakpoints for the piecewise linear function. ALWAYS REQUIRED
pw_repn (str) –
Indicates the type of piecewise representation to use. This can have a major impact on solver performance. Choices: (Default ‘SOS2’)
SOS2
: +Standard representation using sos2 constraints
BIGM_BIN
:BigM constraints with binary variables. Theoretically tightest M values are automatically determined.
BIGM_SOS1
:BigM constraints with sos1 variables. Theoretically tightest M values are automatically determined.
DCC
: *+Disaggregated convex combination model
DLOG
: *+Logarithmic disaggregated convex combination model
CC
: *+Convex combination model
LOG
: *+Logarithmic branching convex combination
MC
: *Multiple choice model
INC
: *+Incremental (delta) method
Note
+: Supports step functions
*: From “Mixed-Integer Models for Non-separable Piecewise Linear Optimization: Unifying framework and Extensions” (Vielma, Nemhauser 2008)
See also
Refer to the optional ‘force_pw’ keyword.
pw_constr_type (str) –
Indicates the bound type of the piecewise function. Choices:
UB
- y variable is bounded above by piecewise functionLB
- y variable is bounded below by piecewise functionEQ
- y variable is equal to the piecewise function
f_rule (f(model,i,j,...,x), {}, [], ()) –
An object that returns a numeric value that is the range value corresponding to each piecewise domain point. For functions, the first argument must be a Pyomo model. The last argument is the domain value at which the function evaluates (Not a Pyomo Var). Intermediate arguments are the corresponding indices of the Piecewise component (if any). Otherwise, the object can be a dictionary of lists/tuples (with keys the same as the indexing set) or a singe list/tuple (when no indexing set is used or when all indices use an identical piecewise function). Examples:
# A function which changes with index def f(model,j,x): if (j == 2): return x**2 + 1.0 else: return x**2 + 5.0 # A nonlinear function f = lambda model, x: return exp(x) + value(model.p) # (where model.p is a Pyomo Param) # A step function f = [0,0,1,1,2,2]
force_pw (bool) – Using the given function rule and pw_pts, a check for convexity/concavity is implemented. If (1) the function is convex and the piecewise constraints are lower bounds or if (2) the function is concave and the piecewise constraints are upper bounds then the piecewise constraints will be substituted for linear constraints. Setting ‘force_pw=True’ will force the use of the original piecewise constraints even when one of these two cases applies.
warning_tol (float, default=1e-8) – To aid in debugging, a warning is printed when consecutive slopes of piecewise segments are within <warning_tol> of each other.
warn_domain_coverage (bool, default=True) – Print a warning when the feasible region of the domain variable is not completely covered by the piecewise breakpoints.
unbounded_domain_var (bool, default=False) –
Allow an unbounded or partially bounded Pyomo Var to be used as the domain variable.
Note
This does not imply unbounded piecewise segments will be constructed. The outermost piecewise breakpoints will bound the domain variable at each index. However, the Var attributes .lb and .ub will not be modified.
Methods
__init__
(*args, **kwds)Constructor
activate
()Set the active attribute to True
add
(index[, _is_indexed])clear
()Clear the data in this component
clear_suffix_value
(suffix_or_name[, expand])Clear the suffix value for this component data
cname
(*args, **kwds)DEPRECATED.
construct
(*args, **kwds)A quick hack to call add after data has been loaded.
Set the active attribute to False
dim
()Return the dimension of the index
display
([filename, ostream, prefix])Display values in the block
get_suffix_value
(suffix_or_name[, default])Get the suffix value for this component data
getname
([fully_qualified, name_buffer, ...])Returns the component name associated with this object.
Return an dictionary id->index for all ComponentData instances.
Return the index set
Return True if this class is a Pyomo component
Return True if this class has been constructed
is_expression_type
([expression_system])Return True if this numeric value is an expression
Return true if this component is indexed
Return True if this class is a Pyomo Boolean object.
Return True if this numeric value is a named expression
Return True if this class is a Pyomo numeric object
Return False unless this class is a parameter object
Return True if this component is a reference, where "reference" is interpreted as any component that does not own its own data.
Return False unless this class is a variable object
items
([sort, ordered])Return an iterator of (index,data) component data tuples
DEPRECATED.
iterkeys
()DEPRECATED.
DEPRECATED.
keys
([sort, ordered])Return an iterator over the component data keys
model
()Returns the model associated with this object.
Returns the parent of this object.
Returns the component associated with this object.
pprint
([ostream, verbose, prefix])Print component information
reconstruct
([data])REMOVED: reconstruct() was removed in Pyomo 6.0.
register_private_data_initializer
(initializer)Return self.model()
set_suffix_value
(suffix_or_name, value[, expand])Set the suffix value for this component data
set_value
(value)Set the value of a scalar component.
TODO
type
()DEPRECATED.
Return True if this can be used as a model component.
values
([sort, ordered])Return an iterator of the component data objects
Attributes
Return the active attribute
Return the class type for this component
Get the component name only within the context of the immediate parent container.
Get the fully qualified component name.
Member Documentation
- activate()
Set the active attribute to True
- clear()
Clear the data in this component
- clear_suffix_value(suffix_or_name, expand=True)
Clear the suffix value for this component data
- cname(*args, **kwds)
DEPRECATED.
Deprecated since version 5.0: The cname() method has been renamed to getname(). The preferred method of obtaining a component name is to use the .name property, which returns the fully qualified component name. The .local_name property will return the component name only within the context of the immediate parent container.
- deactivate()
Set the active attribute to False
- dim()
Return the dimension of the index
- display(filename=None, ostream=None, prefix='')
Display values in the block
- get_suffix_value(suffix_or_name, default=None)
Get the suffix value for this component data
- getname(fully_qualified=False, name_buffer=None, relative_to=None)
Returns the component name associated with this object.
- id_index_map()
Return an dictionary id->index for all ComponentData instances.
- index_set()
Return the index set
- is_component_type()
Return True if this class is a Pyomo component
- is_constructed()
Return True if this class has been constructed
- is_expression_type(expression_system=None)
Return True if this numeric value is an expression
- is_indexed()
Return true if this component is indexed
- is_logical_type()
Return True if this class is a Pyomo Boolean object.
Boolean objects include constants, variables, or logical expressions.
- is_named_expression_type()
Return True if this numeric value is a named expression
- is_numeric_type()
Return True if this class is a Pyomo numeric object
- is_parameter_type()
Return False unless this class is a parameter object
- is_reference()
Return True if this component is a reference, where “reference” is interpreted as any component that does not own its own data.
- is_variable_type()
Return False unless this class is a variable object
- items(sort=<SortComponents.UNSORTED: 0>, ordered=NOTSET)
Return an iterator of (index,data) component data tuples
- Parameters:
sort (bool or SortComponents) – Iterate over the declared component items in a specified sorted order. See
SortComponents
for valid options and descriptions.ordered (bool) – DEPRECATED: Please use sort=SortComponents.ORDERED_INDICES. If True, then the items are returned in a deterministic order (using the underlying set’s ordered_iter().
- iteritems()
DEPRECATED.
Return a list (index,data) tuples from the dictionary
Deprecated since version 6.0: The iteritems method is deprecated. Use dict.items().
- iterkeys()
DEPRECATED.
Return a list of keys in the dictionary
Deprecated since version 6.0: The iterkeys method is deprecated. Use dict.keys().
- itervalues()
DEPRECATED.
Return a list of the component data objects in the dictionary
Deprecated since version 6.0: The itervalues method is deprecated. Use dict.values().
- keys(sort=<SortComponents.UNSORTED: 0>, ordered=NOTSET)
Return an iterator over the component data keys
This method sets the ordering of component data objects within this IndexedComponent container. For consistency,
__init__()
,values()
, anditems()
all leverage this method to ensure consistent ordering.- Parameters:
sort (bool or SortComponents) – Iterate over the declared component keys in a specified sorted order. See
SortComponents
for valid options and descriptions.ordered (bool) – DEPRECATED: Please use sort=SortComponents.ORDERED_INDICES. If True, then the keys are returned in a deterministic order (using the underlying set’s ordered_iter()).
- model()
Returns the model associated with this object.
- parent_block()
Returns the parent of this object.
- parent_component()
Returns the component associated with this object.
- pprint(ostream=None, verbose=False, prefix='')
Print component information
- reconstruct(data=None)
REMOVED: reconstruct() was removed in Pyomo 6.0.
Re-constructing model components was fragile and did not correctly update instances of the component used in other components or contexts (this was particularly problemmatic for Var, Param, and Set). Users who wish to reproduce the old behavior of reconstruct(), are comfortable manipulating non-public interfaces, and who take the time to verify that the correct thing happens to their model can approximate the old behavior of reconstruct with:
component.clear() component._constructed = False component.construct()
- root_block()
Return self.model()
- set_suffix_value(suffix_or_name, value, expand=True)
Set the suffix value for this component data
- set_value(value)
Set the value of a scalar component.
- to_dense_data()
TODO
- type()
DEPRECATED.
Return the class type for this component
Deprecated since version 5.7: Component.type() method has been replaced by the .ctype property.
- valid_model_component()
Return True if this can be used as a model component.
- values(sort=<SortComponents.UNSORTED: 0>, ordered=NOTSET)
Return an iterator of the component data objects
- Parameters:
sort (bool or SortComponents) – Iterate over the declared component values in a specified sorted order. See
SortComponents
for valid options and descriptions.ordered (bool) – DEPRECATED: Please use sort=SortComponents.ORDERED_INDICES. If True, then the values are returned in a deterministic order (using the underlying set’s ordered_iter().
- property active
Return the active attribute
- property ctype
Return the class type for this component
- property local_name
Get the component name only within the context of the immediate parent container.
- property name
Get the fully qualified component name.