AML Library Reference
The following modeling components make up the core of the Pyomo Algebraic Modeling Language (AML). These classes are all available through the pyomo.environ namespace.
ConcreteModel (*args, **kwds) |
A concrete optimization model that does not defer construction of components. |
AbstractModel (*args, **kwds) |
An abstract optimization model that defers construction of components. |
Block (*args, **kwds) |
Blocks are indexed components that contain other components (including blocks). |
Set (*args, **kwds) |
A component used to index other Pyomo components. |
RangeSet (*args, **kwds) |
A set object that represents a set of numeric values |
Param (*args, **kwds) |
A parameter value, which may be defined over an index. |
Var (*args, **kwargs) |
A numeric variable, which may be defined over an index. |
Objective (*args, **kwds) |
This modeling component defines an objective expression. |
Constraint (*args, **kwds) |
This modeling component defines a constraint expression using a rule function. |
ExternalFunction (*args, **kwargs) |
Interface to an external (non-algebraic) function. |
Reference (reference[, ctype]) |
Creates a component that references other components |
AML Component Documentation
-
class
pyomo.environ.
ConcreteModel
(*args, **kwds)[source] Bases:
Model
A concrete optimization model that does not defer construction of components.
-
activate
() Set the active attribute to True
-
property
active
Return the active attribute
-
active_blocks
(*args, **kwargs) DEPRECATED.
Deprecated since version 4.1.10486: The active_blocks method is deprecated. Use the Block.block_data_objects() method.
-
active_component_data
(*args, **kwargs) DEPRECATED.
Deprecated since version 4.1.10486: The active_component_data method is deprecated. Use the Block.component_data_objects() method.
-
active_components
(*args, **kwargs) DEPRECATED.
Deprecated since version 4.1.10486: The active_components method is deprecated. Use the Block.component_objects() method.
-
add_component
(name, val) Add a component ‘name’ to the block.
This method assumes that the attribute is not in the model.
-
all_blocks
(*args, **kwargs) DEPRECATED.
Deprecated since version 4.1.10486: The all_blocks method is deprecated. Use the Block.block_data_objects() method.
-
all_component_data
(*args, **kwargs) DEPRECATED.
Deprecated since version 4.1.10486: The all_component_data method is deprecated. Use the Block.component_data_objects() method.
-
all_components
(*args, **kwargs) DEPRECATED.
Deprecated since version 4.1.10486: The all_components method is deprecated. Use the Block.component_objects() method.
-
block_data_objects
(active=None, sort=False, descend_into=True, descent_order=None) Generator returning this block and any matching sub-blocks.
This is roughly equivalent to
- iter(block for block in itertools.chain(
- [self], self.component_data_objects(descend_into, …)) if block.active == active
)
Notes
The self block is always returned, regardless of the types indicated by descend_into.
The active flag is enforced on all blocks, including self.
Parameters: - active (None or bool) – If not None, filter components by the active flag
- sort (None or bool or SortComponents) – Iterate over the components in a specified sorted order
- descend_into (None or type or iterable) – Specifies the component types (ctypes) to return and to descend into. If True or None, defaults to (Block,). If False, only self is returned.
- descent_order (None or TraversalStrategy) – The strategy used to walk the block hierarchy. Defaults to TraversalStrategy.PrefixDepthFirstSearch.
-
clear
() Clear the data in this component
-
clear_suffix_value
(suffix_or_name, expand=True) Set the suffix value for this component data
-
clone
(memo=None) TODO
-
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.
-
collect_ctypes
(active=None, descend_into=True) Count all component types stored on or under this block.
Parameters: - active (True/None) – Set to True to indicate that only active components should be counted. The default value of None indicates that all components (including those that have been deactivated) should be counted.
- descend_into (bool) – Indicates whether or not component types should be counted on sub-blocks. Default is True.
Returns: A set of component types.
-
component
(name_or_object) Return a child component of this block.
If passed a string, this will return the child component registered by that name. If passed a component, this will return that component IFF the component is a child of this block. Returns None on lookup failure.
-
component_data_iterindex
(ctype=None, active=None, sort=False, descend_into=True, descent_order=None) Return a generator that returns a tuple for each component data object in a block. By default, this generator recursively descends into sub-blocks. The tuple is
((component name, index value), _ComponentData)
-
component_data_objects
(ctype=None, active=None, sort=False, descend_into=True, descent_order=None) Return a generator that iterates through the component data objects for all components in a block. By default, this generator recursively descends into sub-blocks.
-
component_map
(ctype=None, active=None, sort=False) Returns a PseudoMap of the components in this block.
Parameters: - ctype (None or type or iterable) –
Specifies the component types (ctypes) to include in the resulting PseudoMap
None All components type A single component type iterable All component types in the iterable - active (None or bool) –
Filter components by the active flag
None Return all components True Return only active components False Return only inactive components - sort (bool) –
Iterate over the components in a sorted order
True Iterate using Block.alphabetizeComponentAndIndex False Iterate using Block.declarationOrder
- ctype (None or type or iterable) –
-
component_objects
(ctype=None, active=None, sort=False, descend_into=True, descent_order=None) Return a generator that iterates through the component objects in a block. By default, the generator recursively descends into sub-blocks.
-
compute_statistics
(active=True) Compute model statistics
-
construct
(data=None) Initialize the block
-
contains_component
(ctype) Return True if the component type is in _ctypes and … TODO.
-
create_instance
(filename=None, data=None, name=None, namespace=None, namespaces=None, profile_memory=0, report_timing=False, **kwds) Create a concrete instance of an abstract model, possibly using data read in from a file.
Parameters: - filename (str, optional) – The name of a Pyomo Data File that will be used to load data into the model.
- data (dict, optional) – A dictionary containing initialization data for the model to be used if there is no filename
- name (str, optional) – The name given to the model.
- namespace (str, optional) – A namespace used to select data.
- namespaces (list, optional) – A list of namespaces used to select data.
- profile_memory (int, optional) – A number that indicates the profiling level.
- report_timing (bool, optional) – Report timing statistics during construction.
-
property
ctype
Return the class type for this component
-
deactivate
() Set the active attribute to False
-
del_component
(name_or_object) Delete a component from this block.
-
dim
() Return the dimension of the index
-
display
(filename=None, ostream=None, prefix='') Display values in the block
-
find_component
(label_or_component) Returns a component in the block given a name.
Parameters: label_or_component (str, Component, or ComponentUID) – The name of the component to find in this block. String or Component arguments are first converted to ComponentUID. Returns: Component on the block identified by the ComponentUID. If a matching component is not found, None is returned. Return type: Component
-
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) Return a string with the component name and index
-
id_index_map
() Return an dictionary id->index for all ComponentData instances.
-
index
() Returns the index of this ComponentData instance relative to the parent component index set. None is returned if this instance does not have a parent component, or if - for some unknown reason - this instance does not belong to the parent component’s index set.
-
index_set
() Return the index set
-
is_component_type
() Return True if this class is a Pyomo component
-
is_constructed
() A boolean indicating whether or not all active components of the input model have been properly 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 value, variable, or expression.
-
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
(ordered=False) Return an iterator of (index,data) component data tuples
Parameters: ordered (bool) – If True, then the items are returned in a deterministic order. If the underlying indexing set is ordered then that ordering is used. Otherwise, the items are sorted using sorted_robust()
.
-
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
(ordered=False) 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: ordered (bool) – If True, then the keys are returned in a deterministic order. If the underlying indexing set is ordered then that ordering is used. Otherwise, the keys are sorted using sorted_robust()
.
-
load
(arg, namespaces=[None], profile_memory=0) Load the model with data from a file, dictionary or DataPortal object.
-
property
local_name
Get the component name only within the context of the immediate parent container.
-
model
() Return the model of the component that owns this data.
-
property
name
Get the fully qualifed component name.
-
parent_block
() Return the parent of the component that owns this data.
-
parent_component
() Returns the component associated with this object.
-
pprint
(ostream=None, verbose=False, prefix='') Print component information
-
preprocess
(preprocessor=None) DEPRECATED.
Deprecated since version 6.0: The Model.preprocess() method is deprecated and no longer performs any actions
-
reclassify_component_type
(name_or_object, new_ctype, preserve_declaration_order=True) TODO
-
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
(val) Set the value of a scalar component.
-
to_dense_data
() TODO
-
transfer_attributes_from
(src) Transfer user-defined attributes from src to this block
This transfers all components and user-defined attributes from the block or dictionary src and places them on this Block. Components are transferred in declaration order.
If a Component on src is also declared on this block as either a Component or attribute, the local Component or attribute is replaced by the incoming component. If an attribute name on src matches a Component declared on this block, then the incoming attribute is passed to the local Component’s set_value() method. Attribute names appearing in this block’s _Block_reserved_words set will not be transferred (although Components will be).
Parameters: src (_BlockData or dict) – The Block or mapping that contains the new attributes to assign to this block.
-
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.
-
valid_problem_types
() This method allows the pyomo.opt convert function to work with a Model object.
-
values
(ordered=False) Return an iterator of the component data objects
Parameters: ordered (bool) – If True, then the values are returned in a deterministic order. If the underlying indexing set is ordered then that ordering is used. Otherwise, the component keys are sorted using sorted_robust()
and the values are returned in that order.
-
write
(filename=None, format=None, solver_capability=None, io_options={}) Write the model to a file, with a given format.
-
-
class
pyomo.environ.
AbstractModel
(*args, **kwds)[source] Bases:
Model
An abstract optimization model that defers construction of components.
-
activate
() Set the active attribute to True
-
property
active
Return the active attribute
-
active_blocks
(*args, **kwargs) DEPRECATED.
Deprecated since version 4.1.10486: The active_blocks method is deprecated. Use the Block.block_data_objects() method.
-
active_component_data
(*args, **kwargs) DEPRECATED.
Deprecated since version 4.1.10486: The active_component_data method is deprecated. Use the Block.component_data_objects() method.
-
active_components
(*args, **kwargs) DEPRECATED.
Deprecated since version 4.1.10486: The active_components method is deprecated. Use the Block.component_objects() method.
-
add_component
(name, val) Add a component ‘name’ to the block.
This method assumes that the attribute is not in the model.
-
all_blocks
(*args, **kwargs) DEPRECATED.
Deprecated since version 4.1.10486: The all_blocks method is deprecated. Use the Block.block_data_objects() method.
-
all_component_data
(*args, **kwargs) DEPRECATED.
Deprecated since version 4.1.10486: The all_component_data method is deprecated. Use the Block.component_data_objects() method.
-
all_components
(*args, **kwargs) DEPRECATED.
Deprecated since version 4.1.10486: The all_components method is deprecated. Use the Block.component_objects() method.
-
block_data_objects
(active=None, sort=False, descend_into=True, descent_order=None) Generator returning this block and any matching sub-blocks.
This is roughly equivalent to
- iter(block for block in itertools.chain(
- [self], self.component_data_objects(descend_into, …)) if block.active == active
)
Notes
The self block is always returned, regardless of the types indicated by descend_into.
The active flag is enforced on all blocks, including self.
Parameters: - active (None or bool) – If not None, filter components by the active flag
- sort (None or bool or SortComponents) – Iterate over the components in a specified sorted order
- descend_into (None or type or iterable) – Specifies the component types (ctypes) to return and to descend into. If True or None, defaults to (Block,). If False, only self is returned.
- descent_order (None or TraversalStrategy) – The strategy used to walk the block hierarchy. Defaults to TraversalStrategy.PrefixDepthFirstSearch.
-
clear
() Clear the data in this component
-
clear_suffix_value
(suffix_or_name, expand=True) Set the suffix value for this component data
-
clone
(memo=None) TODO
-
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.
-
collect_ctypes
(active=None, descend_into=True) Count all component types stored on or under this block.
Parameters: - active (True/None) – Set to True to indicate that only active components should be counted. The default value of None indicates that all components (including those that have been deactivated) should be counted.
- descend_into (bool) – Indicates whether or not component types should be counted on sub-blocks. Default is True.
Returns: A set of component types.
-
component
(name_or_object) Return a child component of this block.
If passed a string, this will return the child component registered by that name. If passed a component, this will return that component IFF the component is a child of this block. Returns None on lookup failure.
-
component_data_iterindex
(ctype=None, active=None, sort=False, descend_into=True, descent_order=None) Return a generator that returns a tuple for each component data object in a block. By default, this generator recursively descends into sub-blocks. The tuple is
((component name, index value), _ComponentData)
-
component_data_objects
(ctype=None, active=None, sort=False, descend_into=True, descent_order=None) Return a generator that iterates through the component data objects for all components in a block. By default, this generator recursively descends into sub-blocks.
-
component_map
(ctype=None, active=None, sort=False) Returns a PseudoMap of the components in this block.
Parameters: - ctype (None or type or iterable) –
Specifies the component types (ctypes) to include in the resulting PseudoMap
None All components type A single component type iterable All component types in the iterable - active (None or bool) –
Filter components by the active flag
None Return all components True Return only active components False Return only inactive components - sort (bool) –
Iterate over the components in a sorted order
True Iterate using Block.alphabetizeComponentAndIndex False Iterate using Block.declarationOrder
- ctype (None or type or iterable) –
-
component_objects
(ctype=None, active=None, sort=False, descend_into=True, descent_order=None) Return a generator that iterates through the component objects in a block. By default, the generator recursively descends into sub-blocks.
-
compute_statistics
(active=True) Compute model statistics
-
construct
(data=None) Initialize the block
-
contains_component
(ctype) Return True if the component type is in _ctypes and … TODO.
-
create_instance
(filename=None, data=None, name=None, namespace=None, namespaces=None, profile_memory=0, report_timing=False, **kwds) Create a concrete instance of an abstract model, possibly using data read in from a file.
Parameters: - filename (str, optional) – The name of a Pyomo Data File that will be used to load data into the model.
- data (dict, optional) – A dictionary containing initialization data for the model to be used if there is no filename
- name (str, optional) – The name given to the model.
- namespace (str, optional) – A namespace used to select data.
- namespaces (list, optional) – A list of namespaces used to select data.
- profile_memory (int, optional) – A number that indicates the profiling level.
- report_timing (bool, optional) – Report timing statistics during construction.
-
property
ctype
Return the class type for this component
-
deactivate
() Set the active attribute to False
-
del_component
(name_or_object) Delete a component from this block.
-
dim
() Return the dimension of the index
-
display
(filename=None, ostream=None, prefix='') Display values in the block
-
find_component
(label_or_component) Returns a component in the block given a name.
Parameters: label_or_component (str, Component, or ComponentUID) – The name of the component to find in this block. String or Component arguments are first converted to ComponentUID. Returns: Component on the block identified by the ComponentUID. If a matching component is not found, None is returned. Return type: Component
-
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) Return a string with the component name and index
-
id_index_map
() Return an dictionary id->index for all ComponentData instances.
-
index
() Returns the index of this ComponentData instance relative to the parent component index set. None is returned if this instance does not have a parent component, or if - for some unknown reason - this instance does not belong to the parent component’s index set.
-
index_set
() Return the index set
-
is_component_type
() Return True if this class is a Pyomo component
-
is_constructed
() A boolean indicating whether or not all active components of the input model have been properly 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 value, variable, or expression.
-
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
(ordered=False) Return an iterator of (index,data) component data tuples
Parameters: ordered (bool) – If True, then the items are returned in a deterministic order. If the underlying indexing set is ordered then that ordering is used. Otherwise, the items are sorted using sorted_robust()
.
-
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
(ordered=False) 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: ordered (bool) – If True, then the keys are returned in a deterministic order. If the underlying indexing set is ordered then that ordering is used. Otherwise, the keys are sorted using sorted_robust()
.
-
load
(arg, namespaces=[None], profile_memory=0) Load the model with data from a file, dictionary or DataPortal object.
-
property
local_name
Get the component name only within the context of the immediate parent container.
-
model
() Return the model of the component that owns this data.
-
property
name
Get the fully qualifed component name.
-
parent_block
() Return the parent of the component that owns this data.
-
parent_component
() Returns the component associated with this object.
-
pprint
(ostream=None, verbose=False, prefix='') Print component information
-
preprocess
(preprocessor=None) DEPRECATED.
Deprecated since version 6.0: The Model.preprocess() method is deprecated and no longer performs any actions
-
reclassify_component_type
(name_or_object, new_ctype, preserve_declaration_order=True) TODO
-
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
(val) Set the value of a scalar component.
-
to_dense_data
() TODO
-
transfer_attributes_from
(src) Transfer user-defined attributes from src to this block
This transfers all components and user-defined attributes from the block or dictionary src and places them on this Block. Components are transferred in declaration order.
If a Component on src is also declared on this block as either a Component or attribute, the local Component or attribute is replaced by the incoming component. If an attribute name on src matches a Component declared on this block, then the incoming attribute is passed to the local Component’s set_value() method. Attribute names appearing in this block’s _Block_reserved_words set will not be transferred (although Components will be).
Parameters: src (_BlockData or dict) – The Block or mapping that contains the new attributes to assign to this block.
-
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.
-
valid_problem_types
() This method allows the pyomo.opt convert function to work with a Model object.
-
values
(ordered=False) Return an iterator of the component data objects
Parameters: ordered (bool) – If True, then the values are returned in a deterministic order. If the underlying indexing set is ordered then that ordering is used. Otherwise, the component keys are sorted using sorted_robust()
and the values are returned in that order.
-
write
(filename=None, format=None, solver_capability=None, io_options={}) Write the model to a file, with a given format.
-
-
class
pyomo.environ.
Block
(*args, **kwds)[source] Bases:
ActiveIndexedComponent
Blocks are indexed components that contain other components (including blocks). Blocks have a global attribute that defines whether construction is deferred. This applies to all components that they contain except blocks. Blocks contained by other blocks use their local attribute to determine whether construction is deferred.
-
activate
() Set the active attribute to True
-
property
active
Return the active attribute
-
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.
-
property
ctype
Return the class type for this component
-
deactivate
() Set the active attribute to False
-
dim
() Return the dimension of the index
-
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.
Parameters:
-
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 value, variable, or expression.
-
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
(ordered=False) Return an iterator of (index,data) component data tuples
Parameters: ordered (bool) – If True, then the items are returned in a deterministic order. If the underlying indexing set is ordered then that ordering is used. Otherwise, the items are sorted using sorted_robust()
.
-
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
(ordered=False) 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: ordered (bool) – If True, then the keys are returned in a deterministic order. If the underlying indexing set is ordered then that ordering is used. Otherwise, the keys are sorted using sorted_robust()
.
-
property
local_name
Get the component name only within the context of the immediate parent container.
-
model
() Returns the model associated with this object.
-
property
name
Get the fully qualifed component name.
-
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
(ordered=False) Return an iterator of the component data objects
Parameters: ordered (bool) – If True, then the values are returned in a deterministic order. If the underlying indexing set is ordered then that ordering is used. Otherwise, the component keys are sorted using sorted_robust()
and the values are returned in that order.
-
-
class
pyomo.environ.
Constraint
(*args, **kwds)[source] Bases:
ActiveIndexedComponent
This modeling component defines a constraint expression using a rule function.
- Constructor arguments:
- expr
- A Pyomo expression for this constraint
- rule
- A function that is used to construct constraint expressions
- name
- A name for this component
- doc
- A text string describing this component
- Public class attributes:
- doc
- A text string describing this component
- name
- A name for this component
- active
- A boolean that is true if this component will be used to construct a model instance
- rule
- The rule used to initialize the constraint(s)
- Private class attributes:
- _constructed
- A boolean that is true if this component has been constructed
- _data
- A dictionary from the index set to component data objects
- _index
- The set of valid indices
- _implicit_subsets
- A tuple of set objects that represents the index set
- _model
- A weakref to the model that owns this component
- _parent
- A weakref to the parent block that owns this component
- _type
- The class type for the derived subclass
-
activate
() Set the active attribute to True
-
property
active
Return the active attribute
-
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.
-
property
ctype
Return the class type for this component
-
deactivate
() Set the active attribute to False
-
dim
() Return the dimension of the index
-
display
(prefix='', ostream=None)[source] Print component state information
This duplicates logic in Component.pprint()
-
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.
Parameters:
-
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 value, variable, or expression.
-
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
(ordered=False) Return an iterator of (index,data) component data tuples
Parameters: ordered (bool) – If True, then the items are returned in a deterministic order. If the underlying indexing set is ordered then that ordering is used. Otherwise, the items are sorted using sorted_robust()
.
-
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
(ordered=False) 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: ordered (bool) – If True, then the keys are returned in a deterministic order. If the underlying indexing set is ordered then that ordering is used. Otherwise, the keys are sorted using sorted_robust()
.
-
property
local_name
Get the component name only within the context of the immediate parent container.
-
model
() Returns the model associated with this object.
-
property
name
Get the fully qualifed component name.
-
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
(ordered=False) Return an iterator of the component data objects
Parameters: ordered (bool) – If True, then the values are returned in a deterministic order. If the underlying indexing set is ordered then that ordering is used. Otherwise, the component keys are sorted using sorted_robust()
and the values are returned in that order.
-
class
pyomo.environ.
ExternalFunction
(*args, **kwargs)[source] Bases:
Component
Interface to an external (non-algebraic) function.
ExternalFunction
provides an interface for declaring general user-provided functions, and then embedding calls to the external functions within Pyomo expressions.Note
Just because you can express a Pyomo model with external functions does not mean that the resulting model is solvable. In particular, linear solvers do not accept external functions. The AMPL Solver Library (ASL) interface does support external functions for general nonlinear solvers compiled against it, but only allows functions in compiled libraries through the
AMPLExternalFunction
interface.-
__init__
(*args, **kwargs)[source] Construct a reference to an external function.
There are two fundamental interfaces supported by
ExternalFunction
: Python callback functions and AMPL external functions.Python callback functions (
PythonCallbackFunction
interface)Python callback functions can be specified one of two ways:
- FGH interface:
A single external function call with a signature matching theevaluate_fgh()
method.- Independent functions:
One to three functions that can evaluate the function value, gradient of the function [partial derivatives] with respect to its inputs, and the Hessian of the function [partial second derivatives]. The
function
interface expects a function matching the prototype:def function(*args): float
The
gradient
andhessian
interface expect functions matching the prototype:def gradient_or_hessian(args, fixed=None): List[float]
Where
args
is a tuple of function arguments andfixed
is either None or a list of values equal in length toargs
indicating which arguments are currently fixed (True
) or variable (False
).ASL function libraries (
AMPLExternalFunction
interface)Pyomo can also call functions compiled as part of an AMPL External Function library (see the User-defined functions section in the Hooking your solver to AMPL report). Links to these functions are declared by creating an
ExternalFunction
and passing the compiled library name (or path) to thelibrary
keyword and the name of the function to thefunction
keyword.
-
property
active
Return the active attribute
-
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.
-
construct
(data=None) API definition for constructing components
-
property
ctype
Return the class type for this component
-
evaluate
(args)[source] Return the value of the function given the specified arguments
Parameters: args (Iterable) – Iterable containing the arguments to pass to the external function. Non-native type elements will be converted to a native value using the value()
function.Returns: The return value of the function evaluated at args Return type: float
-
evaluate_fgh
(args, fixed=None, fgh=2)[source] Evaluate the function and gradients given the specified arguments
This evaluates the function given the specified arguments returning a 3-tuple of (function value [f], list of first partial derivatives [g], and the upper triangle of the Hessian matrix [h]).
Parameters: - args (Iterable) – Iterable containing the arguments to pass to the external
function. Non-native type elements will be converted to a
native value using the
value()
function. - fixed (Optional[List[bool]]) – List of values indicating if the corresponding argument value is fixed. Any fixed indices are guaranteed to return 0 for first and second derivatives, regardless of what is computed by the external function.
- fgh ({0, 1, 2}) –
What evaluations to return:
- 0: just return function evaluation
- 1: return function and first derivatives
- 2: return function, first derivatives, and hessian matrix
Any return values not requested will be None.
Returns: - f (float) – The return value of the function evaluated at args
- g (List[float] or None) – The list of first partial derivatives
- h (List[float] or None) – The upper-triangle of the Hessian matrix (second partial derivatives), stored column-wise. Element \(H_{i,j}\) (with \(0 <= i <= j < N\) are mapped using \(h[i + j*(j + 1)/2] == H_{i,j}\).
- args (Iterable) – Iterable containing the arguments to pass to the external
function. Non-native type elements will be converted to a
native value using the
-
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.
Parameters:
-
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 value, variable, or expression.
-
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 object is a reference.
-
is_variable_type
() Return False unless this class is a variable object
-
property
local_name
Get the component name only within the context of the immediate parent container.
-
model
() Returns the model associated with this object.
-
property
name
Get the fully qualifed component name.
-
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
-
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.
-
-
class
pyomo.environ.
Objective
(*args, **kwds)[source] Bases:
ActiveIndexedComponent
This modeling component defines an objective expression.
Note that this is a subclass of NumericValue to allow objectives to be used as part of expressions.
- Constructor arguments:
- expr
- A Pyomo expression for this objective
- rule
- A function that is used to construct objective expressions
- sense
- Indicate whether minimizing (the default) or maximizing
- name
- A name for this component
- doc
- A text string describing this component
- Public class attributes:
- doc
- A text string describing this component
- name
- A name for this component
- active
- A boolean that is true if this component will be used to construct a model instance
- rule
- The rule used to initialize the objective(s)
- sense
- The objective sense
- Private class attributes:
- _constructed
- A boolean that is true if this component has been constructed
- _data
- A dictionary from the index set to component data objects
- _index
- The set of valid indices
- _implicit_subsets
- A tuple of set objects that represents the index set
- _model
- A weakref to the model that owns this component
- _parent
- A weakref to the parent block that owns this component
- _type
- The class type for the derived subclass
-
activate
() Set the active attribute to True
-
property
active
Return the active attribute
-
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.
-
property
ctype
Return the class type for this component
-
deactivate
() Set the active attribute to False
-
dim
() Return the dimension of the index
-
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.
Parameters:
-
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 value, variable, or expression.
-
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
(ordered=False) Return an iterator of (index,data) component data tuples
Parameters: ordered (bool) – If True, then the items are returned in a deterministic order. If the underlying indexing set is ordered then that ordering is used. Otherwise, the items are sorted using sorted_robust()
.
-
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
(ordered=False) 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: ordered (bool) – If True, then the keys are returned in a deterministic order. If the underlying indexing set is ordered then that ordering is used. Otherwise, the keys are sorted using sorted_robust()
.
-
property
local_name
Get the component name only within the context of the immediate parent container.
-
model
() Returns the model associated with this object.
-
property
name
Get the fully qualifed component name.
-
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
(ordered=False) Return an iterator of the component data objects
Parameters: ordered (bool) – If True, then the values are returned in a deterministic order. If the underlying indexing set is ordered then that ordering is used. Otherwise, the component keys are sorted using sorted_robust()
and the values are returned in that order.
-
class
pyomo.environ.
Param
(*args, **kwds)[source] Bases:
IndexedComponent
,IndexedComponent_NDArrayMixin
A parameter value, which may be defined over an index.
- Constructor Arguments:
- domain
- A set that defines the type of values that each parameter must be.
- within
- A set that defines the type of values that each parameter must be.
- validate
- A rule for validating this parameter w.r.t. data that exists in the model
- default
- A scalar, rule, or dictionary that defines default values for this parameter
- initialize
- A dictionary or rule for setting up this parameter with existing model data
- unit: pyomo unit expression
- An expression containing the units for the parameter
- mutable: boolean
- Flag indicating if the value of the parameter may change between calls to a solver. Defaults to False
- name
- Name for this component.
- doc
- Text describing this component.
-
class
NoValue
[source] Bases:
object
A dummy type that is pickle-safe that we can use as the default value for Params to indicate that no valid value is present.
-
property
active
Return the active attribute
-
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.
-
construct
(data=None)[source] Initialize this component.
A parameter is constructed using the initial data or the data loaded from an external source. We first set all the values based on self._rule, and then allow the data dictionary to overwrite anything.
Note that we allow an undefined Param value to be constructed. We throw an exception if a user tries to use an uninitialized Param.
-
property
ctype
Return the class type for this component
-
default
()[source] Return the value of the parameter default.
- Possible values:
- Param.NoValue
- No default value is provided.
- Numeric
- A constant value that is the default value for all undefined parameters.
- Function
- f(model, i) returns the value for the default value for parameter i
-
dim
() Return the dimension of the index
-
extract_values
()[source] A utility to extract all index-value pairs defined for this parameter, returned as a dictionary.
This method is useful in contexts where key iteration and repeated __getitem__ calls are too expensive to extract the contents of a parameter.
-
extract_values_sparse
()[source] A utility to extract all index-value pairs defined with non-default values, returned as a dictionary.
This method is useful in contexts where key iteration and repeated __getitem__ calls are too expensive to extract the contents of a parameter.
-
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.
Parameters:
-
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 value, variable, or expression.
-
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
(ordered=False) Return an iterator of (index,data) component data tuples
Parameters: ordered (bool) – If True, then the items are returned in a deterministic order. If the underlying indexing set is ordered then that ordering is used. Otherwise, the items are sorted using sorted_robust()
.
-
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
(ordered=False) 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: ordered (bool) – If True, then the keys are returned in a deterministic order. If the underlying indexing set is ordered then that ordering is used. Otherwise, the keys are sorted using sorted_robust()
.
-
property
local_name
Get the component name only within the context of the immediate parent container.
-
model
() Returns the model associated with this object.
-
property
name
Get the fully qualifed component name.
-
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_default
(val)[source] Perform error checks and then set the default value for this parameter.
NOTE: this test will not validate the value of function return values.
-
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.
-
store_values
(new_values, check=True)[source] A utility to update a Param with a dictionary or scalar.
If check=True, then both the index and value are checked through the __getitem__ method. Using check=False should only be used by developers!
-
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
(ordered=False) Return an iterator of the component data objects
Parameters: ordered (bool) – If True, then the values are returned in a deterministic order. If the underlying indexing set is ordered then that ordering is used. Otherwise, the component keys are sorted using sorted_robust()
and the values are returned in that order.
-
class
pyomo.environ.
RangeSet
(*args, **kwds)[source] Bases:
Component
A set object that represents a set of numeric values
RangeSet objects are based around NumericRange objects, which include support for non-finite ranges (both continuous and unbounded). Similarly, boutique ranges (like semi-continuous domains) can be represented, e.g.:
>>> from pyomo.core.base.range import NumericRange >>> from pyomo.environ import RangeSet >>> print(RangeSet(ranges=(NumericRange(0,0,0), NumericRange(1,100,0)))) ([0] | [1..100])
The RangeSet object continues to support the notation for specifying discrete ranges using “[first=1], last, [step=1]” values:
>>> r = RangeSet(3) >>> print(r) [1:3] >>> print(list(r)) [1, 2, 3] >>> r = RangeSet(2, 5) >>> print(r) [2:5] >>> print(list(r)) [2, 3, 4, 5] >>> r = RangeSet(2, 5, 2) >>> print(r) [2:4:2] >>> print(list(r)) [2, 4] >>> r = RangeSet(2.5, 4, 0.5) >>> print(r) ([2.5] | [3.0] | [3.5] | [4.0]) >>> print(list(r)) [2.5, 3.0, 3.5, 4.0]
By implementing RangeSet using NumericRanges, the global Sets (like Reals, Integers, PositiveReals, etc.) are trivial instances of a RangeSet and support all Set operations.
Parameters: - *args (int | float | None) – The range defined by ([start=1], end, [step=1]). If only a single positional parameter, end is supplied, then the RangeSet will be the integers starting at 1 up through and including end. Providing two positional arguments, x and y, will result in a range starting at x up to and including y, incrementing by 1. Providing a 3-tuple enables the specification of a step other than 1.
- finite (bool, optional) – This sets if this range is finite (discrete and bounded) or infinite
- ranges (iterable, optional) – The list of range objects that compose this RangeSet
- bounds (tuple, optional) – The lower and upper bounds of values that are admissible in this RangeSet
- filter (function, optional) – Function (rule) that returns True if the specified value is in the RangeSet or False if it is not.
- validate (function, optional) – Data validation function (rule). The function will be called for every data member of the set, and if it returns False, a ValueError will be raised.
- name (str, optional) – Name for this component.
- doc (str, optional) – Text describing this component.
-
property
active
Return the active attribute
-
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.
-
property
ctype
Return the class type for this component
-
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.
Parameters:
-
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 value, variable, or expression.
-
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 object is a reference.
-
is_variable_type
() Return False unless this class is a variable object
-
property
local_name
Get the component name only within the context of the immediate parent container.
-
model
() Returns the model associated with this object.
-
property
name
Get the fully qualifed component name.
-
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
-
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.
-
pyomo.environ.
Reference
(reference, ctype=NOTSET)[source] Creates a component that references other components
Reference
generates a reference component; that is, an indexed component that does not contain data, but instead references data stored in other components as defined by a component slice. The ctype parameter sets theComponent.type()
of the resulting indexed component. If the ctype parameter is not set and all data identified by the slice (at construction time) share a commonComponent.type()
, then that type is assumed. If either the ctype parameter isNone
or the data has more than one ctype, the resulting indexed component will have a ctype ofIndexedComponent
.If the indices associated with wildcards in the component slice all refer to the same
Set
objects for all data identifed by the slice, then the resulting indexed component will be indexed by the product of those sets. However, if all data do not share common set objects, or only a subset of indices in a multidimentional set appear as wildcards, then the resulting indexed component will be indexed by aSetOf
containing a_ReferenceSet
for the slice.Parameters: - reference (
IndexedComponent_slice
) – component slice that defines the data to include in the Reference component - ctype (
type
[optional]) – the type used to create the resulting indexed component. If not specified, the data’s ctype will be used (if all data share a common ctype). If multiple data ctypes are found or type isNone
, thenIndexedComponent
will be used.
Examples
>>> from pyomo.environ import * >>> m = ConcreteModel() >>> @m.Block([1,2],[3,4]) ... def b(b,i,j): ... b.x = Var(bounds=(i,j)) ... >>> m.r1 = Reference(m.b[:,:].x) >>> m.r1.pprint() r1 : Size=4, Index=r1_index, ReferenceTo=b[:, :].x Key : Lower : Value : Upper : Fixed : Stale : Domain (1, 3) : 1 : None : 3 : False : True : Reals (1, 4) : 1 : None : 4 : False : True : Reals (2, 3) : 2 : None : 3 : False : True : Reals (2, 4) : 2 : None : 4 : False : True : Reals
Reference components may also refer to subsets of the original data:
>>> m.r2 = Reference(m.b[:,3].x) >>> m.r2.pprint() r2 : Size=2, Index=b_index_0, ReferenceTo=b[:, 3].x Key : Lower : Value : Upper : Fixed : Stale : Domain 1 : 1 : None : 3 : False : True : Reals 2 : 2 : None : 3 : False : True : Reals
Reference components may have wildcards at multiple levels of the model hierarchy:
>>> m = ConcreteModel() >>> @m.Block([1,2]) ... def b(b,i): ... b.x = Var([3,4], bounds=(i,None)) ... >>> m.r3 = Reference(m.b[:].x[:]) >>> m.r3.pprint() r3 : Size=4, Index=r3_index, ReferenceTo=b[:].x[:] Key : Lower : Value : Upper : Fixed : Stale : Domain (1, 3) : 1 : None : None : False : True : Reals (1, 4) : 1 : None : None : False : True : Reals (2, 3) : 2 : None : None : False : True : Reals (2, 4) : 2 : None : None : False : True : Reals
The resulting reference component may be used just like any other component. Changes to the stored data will be reflected in the original objects:
>>> m.r3[1,4] = 10 >>> m.b[1].x.pprint() x : Size=2, Index=b[1].x_index Key : Lower : Value : Upper : Fixed : Stale : Domain 3 : 1 : None : None : False : True : Reals 4 : 1 : 10 : None : False : False : Reals
- reference (
-
class
pyomo.environ.
Set
(*args, **kwds)[source] Bases:
IndexedComponent
A component used to index other Pyomo components.
This class provides a Pyomo component that is API-compatible with Python set objects, with additional features, including:
- Member validation and filtering. The user can declare domains and provide callback functions to validate set members and to filter (ignore) potential members.
- Set expressions. Operations on Set objects (&,|,*,-,^) produce Set expressions taht preserve their references to the original Set objects so that updating the argument Sets implicitly updates the Set operator instance.
- Support for set operations with RangeSet instances (both finite and non-finite ranges).
Parameters: - initialize (initializer(iterable), optional) – The initial values to store in the Set when it is
constructed. Values passed to
initialize
may be overridden bydata
passed to theconstruct()
method. - dimen (initializer(int), optional) – Specify the Set’s arity (the required tuple length for all members of the Set), or None if no arity is enforced
- ordered (bool or Set.InsertionOrder or Set.SortedOrder or function) –
Specifies whether the set is ordered. Possible values are:
False
Unordered True
Ordered by insertion order Set.InsertionOrder
Ordered by insertion order [default] Set.SortedOrder
Ordered by sort order <function>
Ordered with this comparison function - within (initialiser(set), optional) – A set that defines the valid values that can be contained in this set
- domain (initializer(set), optional) – A set that defines the valid values that can be contained in this set
- bounds (initializer(tuple), optional) – A tuple that specifies the bounds for valid Set values (accepts 1-, 2-, or 3-tuple RangeSet arguments)
- filter (initializer(rule), optional) –
A rule for determining membership in this set. This has the functional form:
f: Block, *data -> bool
and returns True if the data belongs in the set. Set will quietly ignore any values where filter returns False.
- validate (initializer(rule), optional) –
A rule for validating membership in this set. This has the functional form:
f: Block, *data -> bool
and returns True if the data belongs in the set. Set will raise a
ValueError
for any values where validate returns False. - name (str, optional) – The name of the set
- doc (str, optional) – A text string describing this component
Notes
Note
domain=
,within=
, andbounds=
all provide restrictions on the valid set values. If more than one is specified, Set values will be restricted to the intersection ofdomain
,within
, andbounds
.-
property
active
Return the active attribute
-
check_values
()[source] DEPRECATED.
Verify that the values in this set are valid.Deprecated since version 5.7: check_values() is deprecated: Sets only contain valid members
-
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.
-
property
ctype
Return the class type for this component
-
dim
() Return the dimension of the index
-
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.
Parameters:
-
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 value, variable, or expression.
-
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
(ordered=False) Return an iterator of (index,data) component data tuples
Parameters: ordered (bool) – If True, then the items are returned in a deterministic order. If the underlying indexing set is ordered then that ordering is used. Otherwise, the items are sorted using sorted_robust()
.
-
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
(ordered=False) 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: ordered (bool) – If True, then the keys are returned in a deterministic order. If the underlying indexing set is ordered then that ordering is used. Otherwise, the keys are sorted using sorted_robust()
.
-
property
local_name
Get the component name only within the context of the immediate parent container.
-
model
() Returns the model associated with this object.
-
property
name
Get the fully qualifed component name.
-
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
(ordered=False) Return an iterator of the component data objects
Parameters: ordered (bool) – If True, then the values are returned in a deterministic order. If the underlying indexing set is ordered then that ordering is used. Otherwise, the component keys are sorted using sorted_robust()
and the values are returned in that order.
-
class
pyomo.environ.
Var
(*args, **kwargs)[source] Bases:
IndexedComponent
,IndexedComponent_NDArrayMixin
A numeric variable, which may be defined over an index.
Parameters: - domain (Set or function, optional) – A Set that defines valid
values for the variable (e.g.,
Reals
,NonNegativeReals
,Binary
), or a rule that returns Sets. Defaults toReals
. - within (Set or function, optional) – An alias for
domain
. - bounds (tuple or function, optional) – A tuple of
(lower, upper)
bounds for the variable, or a rule that returns tuples. Defaults to(None, None)
. - initialize (float or function, optional) – The initial value for the variable, or a rule that returns initial values.
- rule (float or function, optional) – An alias for
initialize
. - dense (bool, optional) – Instantiate all elements from
index_set()
when constructing the Var (True) or just the variables returned byinitialize
/rule
(False). Defaults toTrue
. - units (pyomo units expression, optional) – Set the units corresponding to the entries in this variable.
- name (str, optional) – Name for this component.
- doc (str, optional) – Text describing this component.
-
property
active
Return the active attribute
-
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.
-
property
ctype
Return the class type for this component
-
dim
() Return the dimension of the index
-
extract_values
(include_fixed_values=True) Return a dictionary of index-value pairs.
-
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.
Parameters:
-
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 value, variable, or expression.
-
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
(ordered=False) Return an iterator of (index,data) component data tuples
Parameters: ordered (bool) – If True, then the items are returned in a deterministic order. If the underlying indexing set is ordered then that ordering is used. Otherwise, the items are sorted using sorted_robust()
.
-
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
(ordered=False) 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: ordered (bool) – If True, then the keys are returned in a deterministic order. If the underlying indexing set is ordered then that ordering is used. Otherwise, the keys are sorted using sorted_robust()
.
-
property
local_name
Get the component name only within the context of the immediate parent container.
-
model
() Returns the model associated with this object.
-
property
name
Get the fully qualifed component name.
-
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.
-
set_values
(new_values, skip_validation=False)[source] Set the values of a dictionary.
The default behavior is to validate the values in the dictionary.
-
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
(ordered=False) Return an iterator of the component data objects
Parameters: ordered (bool) – If True, then the values are returned in a deterministic order. If the underlying indexing set is ordered then that ordering is used. Otherwise, the component keys are sorted using sorted_robust()
and the values are returned in that order.
- domain (Set or function, optional) – A Set that defines valid
values for the variable (e.g.,