SOSConstraint

(class from pyomo.core.base.sos)

class pyomo.core.base.sos.SOSConstraint(*args, **kwds)[source]

Bases: ActiveIndexedComponent

Implements constraints for special ordered sets (SOS).

Parameters:
  • sos (int) – The type of SOS.

  • var (pyomo.environ.Var) – The group of variables from which the SOS(s) will be created.

  • index (pyomo.environ.Set, list or dict, optional) – A data structure with the indexes for the variables that are to be members of the SOS(s). The indexes can be provided as a pyomo Set: either indexed, if the SOS is indexed; or non-indexed, otherwise. Alternatively, the indexes can be provided as a list, for a non-indexed SOS, or as a dict, for indexed SOS(s).

  • weights (pyomo.environ.Param or dict, optional) – A data structure with the weights for each member of the SOS(s). These can be provided as pyomo Param or as a dict. If not provided, the weights will be determined automatically using the var index set.

  • rule (optional) – A method returning a 2-tuple with lists of variables and the respective weights in the same order, or a list of variables whose weights are then determined from their position within the list or, alternatively, pyomo.environ.Constraint.Skip if the constraint should be not be included in the model/instance. This parameter cannot be used in combination with var, index or weights.

Examples

1 - An SOS of type N made up of all members of a pyomo Var component:

>>> # import pyomo
>>> import pyomo.environ as pyo
>>> # declare the model
>>> model = pyo.AbstractModel()
>>> # define the SOS type
>>> N = 1 # 2, 3, ...
>>> # the set that indexes the variables
>>> model.A = pyo.Set()
>>> # the variables under consideration
>>> model.x = pyo.Var(model.A)
>>> # the sos constraint
>>> model.mysos = pyo.SOSConstraint(var=model.x, sos=N)

2 - An SOS of type N made up of all members of a pyomo Var component, each with a specific weight:

>>> # declare the model
>>> model = pyo.AbstractModel()
>>> # define the SOS type
>>> N = 1 # 2, 3, ...
>>> # the set that indexes the variables
>>> model.A = pyo.Set()
>>> # the variables under consideration
>>> model.x = pyo.Var(model.A)
>>> # the weights for each variable used in the sos constraints
>>> model.mysosweights = pyo.Param(model.A)
>>> # the sos constraint
>>> model.mysos = pyo.SOSConstraint(
...     var=model.x,
...     sos=N,
...     weights=model.mysosweights
...     )

3 - An SOS of type N made up of selected members of a Var component:

>>> # declare the model
>>> model = pyo.AbstractModel()
>>> # define the SOS type
>>> N = 1 # 2, 3, ...
>>> # the set that indexes the variables
>>> model.A = pyo.Set()
>>> # the variables under consideration
>>> model.x = pyo.Var(model.A)
>>> # the set that indexes the variables actually used in the constraint
>>> model.B = pyo.Set(within=model.A)
>>> # the sos constraint
>>> model.mysos = pyo.SOSConstraint(var=model.x, sos=N, index=model.B)

4 - An SOS of type N made up of selected members of a Var component, each with a specific weight:

>>> # declare the model
>>> model = pyo.AbstractModel()
>>> # define the SOS type
>>> N = 1 # 2, 3, ...
>>> # the set that indexes the variables
>>> model.A = pyo.Set()
>>> # the variables under consideration
>>> model.x = pyo.Var(model.A)
>>> # the set that indexes the variables actually used in the constraint
>>> model.B = pyo.Set(within=model.A)
>>> # the weights for each variable used in the sos constraints
>>> model.mysosweights = pyo.Param(model.B)
>>> # the sos constraint
>>> model.mysos = pyo.SOSConstraint(
...     var=model.x,
...     sos=N,
...     index=model.B,
...     weights=model.mysosweights
...     )

5 - A set of SOS(s) of type N made up of members of a pyomo Var component:

>>> # declare the model
>>> model = pyo.AbstractModel()
>>> # define the SOS type
>>> N = 1 # 2, 3, ...
>>> # the set that indexes the variables
>>> model.A = pyo.Set()
>>> # the variables under consideration
>>> model.x = pyo.Var(model.A)
>>> # the set indexing the sos constraints
>>> model.B = pyo.Set()
>>> # the sets containing the variable indexes for each constraint
>>> model.mysosvarindexset = pyo.Set(model.B)
>>> # the sos constraints
>>> model.mysos = pyo.SOSConstraint(
...     model.B,
...     var=model.x,
...     sos=N,
...     index=model.mysosvarindexset
...     )

6 - A set of SOS(s) of type N made up of members of a pyomo Var component, each with a specific weight:

>>> # declare the model
>>> model = pyo.AbstractModel()
>>> # define the SOS type
>>> N = 1 # 2, 3, ...
>>> # the set that indexes the variables
>>> model.A = pyo.Set()
>>> # the variables under consideration
>>> model.x = pyo.Var(model.A)
>>> # the set indexing the sos constraints
>>> model.B = pyo.Set()
>>> # the sets containing the variable indexes for each constraint
>>> model.mysosvarindexset = pyo.Set(model.B)
>>> # the set that indexes the variables used in the sos constraints
>>> model.C = pyo.Set(within=model.A)
>>> # the weights for each variable used in the sos constraints
>>> model.mysosweights = pyo.Param(model.C)
>>> # the sos constraints
>>> model.mysos = pyo.SOSConstraint(
...     model.B,
...     var=model.x,
...     sos=N,
...     index=model.mysosvarindexset,
...     weights=model.mysosweights,
...     )

7 - A simple SOS of type N created using the rule parameter:

>>> # declare the model
>>> model = pyo.AbstractModel()
>>> # define the SOS type
>>> N = 1 # 2, 3, ...
>>> # the set that indexes the variables
>>> model.A = pyo.Set()
>>> # the variables under consideration
>>> model.x = pyo.Var(model.A, domain=pyo.NonNegativeReals)
>>> # the rule method creating the constraint
>>> def rule_mysos(m):
...     var_list = [m.x[a] for a in m.x]
...     weight_list = [i+1 for i in range(len(var_list))]
...     return (var_list, weight_list)
>>> # the sos constraint(s)
>>> model.mysos = pyo.SOSConstraint(rule=rule_mysos, sos=N)

8 - A simple SOS of type N created using the rule parameter, in which the weights are determined automatically:

>>> # declare the model
>>> model = pyo.AbstractModel()
>>> # define the SOS type
>>> N = 1 # 2, 3, ...
>>> # the set that indexes the variables
>>> model.A = pyo.Set()
>>> # the variables under consideration
>>> model.x = pyo.Var(model.A, domain=pyo.NonNegativeReals)
>>> # the rule method creating the constraint
>>> def rule_mysos(m):
...     return [m.x[a] for a in m.x]
>>> # the sos constraint(s)
>>> model.mysos = pyo.SOSConstraint(rule=rule_mysos, sos=N)

9 - A set of SOS(s) of type N involving members of distinct pyomo Var components, each with a specific weight. This requires the rule parameter:

>>> # declare the model
>>> model = pyo.AbstractModel()
>>> # define the SOS type
>>> N = 1 # 2, 3, ...
>>> # the set that indexes the x variables
>>> model.A = pyo.Set()
>>> # the set that indexes the y variables
>>> model.B = pyo.Set()
>>> # the set that indexes the SOS constraints
>>> model.C = pyo.Set()
>>> # the x variables, which will be used in the constraints
>>> model.x = pyo.Var(model.A, domain=pyo.NonNegativeReals)
>>> # the y variables, which will be used in the constraints
>>> model.y = pyo.Var(model.B, domain=pyo.NonNegativeReals)
>>> # the x variable indices for each constraint
>>> model.mysosindex_x = pyo.Set(model.C)
>>> # the y variable indices for each constraint
>>> model.mysosindex_y = pyo.Set(model.C)
>>> # the weights for the x variable indices
>>> model.mysosweights_x = pyo.Param(model.A)
>>> # the weights for the y variable indices
>>> model.mysosweights_y = pyo.Param(model.B)
>>> # the rule method with which each constraint c is built
>>> def rule_mysos(m, c):
...     var_list = [m.x[a] for a in m.mysosindex_x[c]]
...     var_list.extend([m.y[b] for b in m.mysosindex_y[c]])
...     weight_list = [m.mysosweights_x[a] for a in m.mysosindex_x[c]]
...     weight_list.extend([m.mysosweights_y[b] for b in m.mysosindex_y[c]])
...     return (var_list, weight_list)
>>> # the sos constraint(s)
>>> model.mysos = pyo.SOSConstraint(
...     model.C,
...     rule=rule_mysos,
...     sos=N
...     )
__init__(*args, **kwargs)[source]

Constructor

Methods

__init__(*args, **kwargs)

Constructor

activate()

Set the active attribute to True

add(index, variables[, weights])

Add a component data for the specified index.

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([data])

Construct this component

deactivate()

Set the active attribute to False

dim()

Return the dimension of the index

display([ostream, verbose, prefix])

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.

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])

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.

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, ordered])

Return an iterator of (index,data) component data tuples

iteritems()

DEPRECATED.

iterkeys()

DEPRECATED.

itervalues()

DEPRECATED.

keys([sort, ordered])

Return an iterator over the component data keys

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, verbose, prefix])

TODO

reconstruct([data])

REMOVED: reconstruct() was removed in Pyomo 6.0.

root_block()

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.

to_dense_data()

TODO

type()

DEPRECATED.

valid_model_component()

Return True if this can be used as a model component.

values([sort, ordered])

Return an iterator of the component data objects

Attributes

Skip

active

Return the active attribute

ctype

Return the class type for this component

local_name

Get the component name only within the context of the immediate parent container.

name

Get the fully qualified component name.

Member Documentation

activate()

Set the active attribute to True

add(index, variables, weights=None)[source]

Add a component data for the specified index.

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]

Construct 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:
  • fully_qualified (bool) – Generate full name from nested block names

  • relative_to (Block) – Generate fully_qualified names relative to the specified block.

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(), and items() 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='')[source]

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(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.